C#直接打印PDF问题
风之瘦影 2019-04-20 06:25:08 最近这两天一直在研究直接打印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秒左右才送完,前两种方式,几乎只花了几秒。
请问各位大神,有没有比较好的方式解决以上问题呢,只解决其中一种,也是感激不尽。
谢谢。