C#直接打印PDF问题
最近这两天一直在研究直接打印PDF文件的问题。找到三种比较好的打印方式,可是各有各的问题。
方法一 : 使用PrintDocument类打印
优点 : 打印速度快
缺点 : 1. 需要安装 Adobe Reader
2. 不能手动选择打印机。即使设定了自己想要的打印机,也还是会使用默认打印机打印
3. 打印中,Adobe Reader会打开PDF文件,有时可以自动关闭,有时关闭不掉。
代码如下所示:
private static void printPDF2(string filePath)
{
PrintDocument pd = new PrintDocument();
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
pd.PrinterSettings.PrinterName = "Send To OneNote 2016";
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = true;
startInfo.FileName = filePath;
startInfo.Verb = "print";
String sysPrintName = pd.PrinterSettings.PrinterName;
String printName = "Send To OneNote 2016";
startInfo.Arguments = @"/p /h \" + filePath + "\"\"" + printName + "\"";
//startInfo.Arguments = @"/p /h \" + filePath + "\"\"" + pd.PrinterSettings.PrinterName + "\"";
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();
}
方法二 : 使用AcroRd32.exe 打印
优点: 1. 打印速度快
2. 可以选择打印机
缺点 : 会自动使用Adobe Reader程序打开文件,然后自动关掉 偶尔也有关不掉的情况
代码如下所示:
private static void printPDF3(string filePath)
{
Process p = new Process();
string appPath = @"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = appPath;
String argument = "/h /t \"D:\\Projects\\11111.pdf\" \"Send To OneNote 2016\"";
startInfo.Arguments = argument;// "/h /t \"D:\\Projects\\11111.pdf\" \"Send To OneNote 2016\"";
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = true;
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();
}
方法三 :使用Spire.PDF for .NET打印
优点: 1. 打印时,完全静默打印,不会弹出Adobe Reader程序,并打开关闭PDF文件
2. 可以选择打印机
缺点 : 打印速度慢
代码在不在家里,因此就不上了。大致如下: (网上随便找的)
using Spire.Pdf;
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\xxx.pdf", FileFormat.PDF);
Console.WriteLine(doc.UsedFonts);
PrintDocument printDoc = doc.PrintDocument;
printDoc.PrintController = new StandardPrintController();
printDoc.Print();
doc.Close();
此种打印方式,感觉是将原本很小的PDF文档,重新生成后,再送入打印机,从打印机里的大小来看,比我原来的文件大了好几倍,30页的文件,花了40秒左右才送完,前两种方式,几乎只花了几秒。
请问各位大神,有没有比较好的方式解决以上问题呢,只解决其中一种,也是感激不尽。
谢谢。
方法一 : 使用PrintDocument类打印
优点 : 打印速度快
缺点 : 1. 需要安装 Adobe Reader
2. 不能手动选择打印机。即使设定了自己想要的打印机,也还是会使用默认打印机打印
3. 打印中,Adobe Reader会打开PDF文件,有时可以自动关闭,有时关闭不掉。
代码如下所示:
private static void printPDF2(string filePath)
{
PrintDocument pd = new PrintDocument();
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
pd.PrinterSettings.PrinterName = "Send To OneNote 2016";
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = true;
startInfo.FileName = filePath;
startInfo.Verb = "print";
String sysPrintName = pd.PrinterSettings.PrinterName;
String printName = "Send To OneNote 2016";
startInfo.Arguments = @"/p /h \" + filePath + "\"\"" + printName + "\"";
//startInfo.Arguments = @"/p /h \" + filePath + "\"\"" + pd.PrinterSettings.PrinterName + "\"";
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();
}
方法二 : 使用AcroRd32.exe 打印
优点: 1. 打印速度快
2. 可以选择打印机
缺点 : 会自动使用Adobe Reader程序打开文件,然后自动关掉 偶尔也有关不掉的情况
代码如下所示:
private static void printPDF3(string filePath)
{
Process p = new Process();
string appPath = @"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = appPath;
String argument = "/h /t \"D:\\Projects\\11111.pdf\" \"Send To OneNote 2016\"";
startInfo.Arguments = argument;// "/h /t \"D:\\Projects\\11111.pdf\" \"Send To OneNote 2016\"";
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = true;
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();
}
方法三 :使用Spire.PDF for .NET打印
优点: 1. 打印时,完全静默打印,不会弹出Adobe Reader程序,并打开关闭PDF文件
2. 可以选择打印机
缺点 : 打印速度慢
代码在不在家里,因此就不上了。大致如下: (网上随便找的)
using Spire.Pdf;
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\xxx.pdf", FileFormat.PDF);
Console.WriteLine(doc.UsedFonts);
PrintDocument printDoc = doc.PrintDocument;
printDoc.PrintController = new StandardPrintController();
printDoc.Print();
doc.Close();
此种打印方式,感觉是将原本很小的PDF文档,重新生成后,再送入打印机,从打印机里的大小来看,比我原来的文件大了好几倍,30页的文件,花了40秒左右才送完,前两种方式,几乎只花了几秒。
请问各位大神,有没有比较好的方式解决以上问题呢,只解决其中一种,也是感激不尽。
谢谢。
...全文
相关推荐
C#调用打印机打印pdf第三方O2S.Components.PDFRender4NET.dll文件
然而,这个内建的类并不支持直接打印PDF文件,所以我们需要引入第三方库,如O2S.Components.PDFRender4NET.dll,它为PDF渲染和打印提供了专门的支持。
C# 使用WINAPI 直接向指定打印机发送打印数据
分单打印意味着我们需要根据业务逻辑,可能需要多次调用`WritePrinter`,每次传递不同的数据,以打印不同的订单。这可能涉及到对原始数据的处理,例如将PDF或图像文件转换成适合直接打印的格式。
C#打印PDF文件.zip
总的来说,虽然C#自身不包含直接处理PDF的内置功能,但通过结合第三方库,我们可以构建出功能强大的PDF处理程序,包括下载和打印PDF文件。
C#网页端,直接打印报告,无需预览
首先,要确保客户端已经安装有适合的打印机驱动,并且打印机是正常工作状态。其次,C#后端需要有一个机制来生成报告的打印版本。这通常意味着需要将报告内容转换为PDF或其他打印友好的格式。
FastReport以及PDF打印。Json转DataSet
接下来,我们关注PDF打印功能。FastReport支持直接将报表导出为PDF格式,然后进行打印。


