控制台应用程序中的 DotNetBrowser PrintHandler
DotNetBrowser PrintHandler in console application
我正在尝试将 'DotNetBrowser.Browser' 中加载的网页打印为 PDF 文档,并使用 DotNetBrowser PrintHandler 在本地保存。这在带有 WinFormsBrowserView 的 windows 表单应用程序中工作正常,但在控制台应用程序的情况下,PrintHandler 不会打印整个页面,即仅打印页面的某些部分并另存为 pdf。
下面是演示如何加载网站并将其打印为 PDF 的示例代码:
using DotNetBrowser;
using DotNetBrowser.Events;
using System;
using System.Threading;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
using (var browser = BrowserFactory.Create())
{
ManualResetEvent waitEvent = new ManualResetEvent(false);
browser.FinishLoadingFrameEvent += delegate(object sender, FinishLoadingEventArgs e)
{
// Wait until main document of the web page is loaded completely.
if (e.IsMainFrame)
{
waitEvent.Set();
}
};
browser.LoadURL("https://www.teamdev.com/dotnetbrowser");
waitEvent.WaitOne();
PrintPDF(browser, System.IO.Path.GetFullPath(@"test.pdf"));
}
}
public static void PrintPDF(Browser browser, string sFileName) {
ManualResetEvent waitEvent = new ManualResetEvent(false);
var handler = new MyPDFPrintHandler((printJob) => {
var printSettings = printJob.PrintSettings;
printSettings.PrintToPDF = true;
printSettings.PDFFilePath = sFileName;
printJob.PrintJobEvent += (s, e) =>
{
System.Diagnostics.Debug.WriteLine("Printing done: " + e.Success);
waitEvent.Set();
};
return printSettings;
});
browser.PrintHandler = handler;
browser.Print();
waitEvent.WaitOne();
}
class MyPDFPrintHandler : PrintHandler
{
Func<PrintJob, PrintSettings> func;
public MyPDFPrintHandler(Func<PrintJob, PrintSettings> func)
{
this.func = func;
}
public PrintStatus OnPrint(PrintJob printJob)
{
PrintSettings printSettings = func(printJob);
printSettings.PrintToPDF = true;
printSettings.Landscape = true;
printSettings.PrintBackgrounds = true;
return PrintStatus.CONTINUE;
}
}
}
}
如您所见,有一个同步打印 - 该方法不会 return 直到网页打印完成。
能否在您的网站上测试此代码并让我知道结果?
我终于能够通过为浏览器设置初始大小在控制台应用程序中打印 PDF。
browserView.Browser.SetSize(1280, 1024)
这就是我的代码中所缺少的。由于浏览器不显示,设置初始大小解决了pdf打印不全的问题
我正在尝试将 'DotNetBrowser.Browser' 中加载的网页打印为 PDF 文档,并使用 DotNetBrowser PrintHandler 在本地保存。这在带有 WinFormsBrowserView 的 windows 表单应用程序中工作正常,但在控制台应用程序的情况下,PrintHandler 不会打印整个页面,即仅打印页面的某些部分并另存为 pdf。
下面是演示如何加载网站并将其打印为 PDF 的示例代码:
using DotNetBrowser;
using DotNetBrowser.Events;
using System;
using System.Threading;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
using (var browser = BrowserFactory.Create())
{
ManualResetEvent waitEvent = new ManualResetEvent(false);
browser.FinishLoadingFrameEvent += delegate(object sender, FinishLoadingEventArgs e)
{
// Wait until main document of the web page is loaded completely.
if (e.IsMainFrame)
{
waitEvent.Set();
}
};
browser.LoadURL("https://www.teamdev.com/dotnetbrowser");
waitEvent.WaitOne();
PrintPDF(browser, System.IO.Path.GetFullPath(@"test.pdf"));
}
}
public static void PrintPDF(Browser browser, string sFileName) {
ManualResetEvent waitEvent = new ManualResetEvent(false);
var handler = new MyPDFPrintHandler((printJob) => {
var printSettings = printJob.PrintSettings;
printSettings.PrintToPDF = true;
printSettings.PDFFilePath = sFileName;
printJob.PrintJobEvent += (s, e) =>
{
System.Diagnostics.Debug.WriteLine("Printing done: " + e.Success);
waitEvent.Set();
};
return printSettings;
});
browser.PrintHandler = handler;
browser.Print();
waitEvent.WaitOne();
}
class MyPDFPrintHandler : PrintHandler
{
Func<PrintJob, PrintSettings> func;
public MyPDFPrintHandler(Func<PrintJob, PrintSettings> func)
{
this.func = func;
}
public PrintStatus OnPrint(PrintJob printJob)
{
PrintSettings printSettings = func(printJob);
printSettings.PrintToPDF = true;
printSettings.Landscape = true;
printSettings.PrintBackgrounds = true;
return PrintStatus.CONTINUE;
}
}
}
}
如您所见,有一个同步打印 - 该方法不会 return 直到网页打印完成。
能否在您的网站上测试此代码并让我知道结果?
我终于能够通过为浏览器设置初始大小在控制台应用程序中打印 PDF。
browserView.Browser.SetSize(1280, 1024)
这就是我的代码中所缺少的。由于浏览器不显示,设置初始大小解决了pdf打印不全的问题