【发布时间】:2011-07-30 18:52:36
【问题描述】:
我是 C# 新手。我在网上到处寻找有关如何打印 pdf 的教程,但找不到。
然后我想,是否可以使用itextpdf阅读它,就像这里提到的那样
Reading PDF content with itextsharp dll in VB.NET or C#
然后打印它。如果有,怎么做?
【问题讨论】:
我是 C# 新手。我在网上到处寻找有关如何打印 pdf 的教程,但找不到。
然后我想,是否可以使用itextpdf阅读它,就像这里提到的那样
Reading PDF content with itextsharp dll in VB.NET or C#
然后打印它。如果有,怎么做?
【问题讨论】:
我在 adobereader 周围写了一个小辅助方法,用于从 c# 批量打印 pdf...:
public static bool Print(string file, string printer) {
try {
Process.Start(
Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\Microsoft\Windows\CurrentVersion" +
@"\App Paths\AcroRd32.exe").GetValue("").ToString(),
string.Format("/h /t \"{0}\" \"{1}\"", file, printer));
return true;
} catch { }
return false;
}
不能依赖方法的返回值顺便说一句...
【讨论】:
我编写并发布了一个小型 Nuget 包,可用于将 PDF 文件打印到打印机驱动程序。它还可以打印到 XPS 文件或 PDF 文件。这是a link。
【讨论】:
从 C# 自动打印 pdf 的最佳方法是使用打印机的“直接 pdf”。您只需将 pdf 文件复制到打印机的网络共享名。其余的将由打印机自己处理。
速度比任何其他方法快 10 倍。但是,要求是支持直接 pdf 打印并具有至少 128 MB Dram 的打印机型号,这对于任何现代打印机来说都很容易。
【讨论】:
如果您安装了Adobe Reader,那么您应该可以将其设置为默认打印机。瞧!您可以打印为 PDF!
printDocument1.PrinterSettings.PrinterName = "Adobe PDF";
printDocument1.Print();
就这么简单!!!
【讨论】:
使用 Ultimate PDF 的直观 API,只需几行代码即可打开、导入、编辑、合并、转换 Acrobat PDF 文档。通过使用 100% 用 C# 编写的托管代码,该组件可以利用 .NET Framework 的众多内置功能来提高性能。此外,该库符合 CLS,并且它不使用任何不安全的块来满足最低权限要求。这些课程完整地记录了详细的示例代码,有助于缩短您的学习曲线。如果您的开发环境是 Visual Studio,请享受在线文档的完整集成。只需在 Visual Studio IDE 中标记或选择关键字并按 F1,即可立即显示在线文档。一个高性能且可靠的 PDF 库,可让您通过几行代码轻松地将 PDF 功能添加到您的 .NET 应用程序中。
【讨论】:
最简单的方法是创建 C# 进程并启动外部工具来打印您的 PDF 文件
private static void ExecuteRawFilePrinter() {
Process process = new Process();
process.StartInfo.FileName = "c:\\Program Files (x86)\\RawFilePrinter\\RawFilePrinter.exe";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.Arguments = string.Format("-p \"c:\\Users\\Me\\Desktop\\mypdffile.pdf\" \"gdn02ptr006\"");
process.Start();
process.WaitForExit();
}
上面的代码启动 RawFilePrinter.exe(类似于 2Printer.exe),但有更好的支持。它不是免费的,但通过捐赠,您可以在任何地方使用它并与您的应用程序一起重新分发。最新版本下载:http://bigdotsoftware.pl/rawfileprinter
【讨论】:
使用 PDFiumViewer。我搜索了很长时间,直到我想出了一个类似的解决方案,然后我发现了这段干净的代码,它不依赖于将原始文件发送到打印机(如果它们被解释为文本文件,这很糟糕..)或使用Acrobat 或 Ghostscript 作为助手(两者都需要安装,这很麻烦):
https://stackoverflow.com/a/41751184/586754
PDFiumViewer 来自 nuget,上面的代码示例已经完成。传入空值以使用默认打印机。
【讨论】:
看起来像 pdfsharp 和 migradoc 这样的常见嫌疑人无法做到这一点(pdfsharp 仅在您安装了 Acrobat (Reader) 的情况下)。
我在这里找到了
准备好复制/粘贴的代码。它使用默认打印机,据我所知,它甚至不使用任何库,直接将 pdf 字节发送到打印机。所以我假设打印机也需要支持它,我在一台使用了 10 年的打印机上测试它完美无瑕。
大多数其他方法 - 没有商业库或应用程序 - 要求您在打印设备上下文中绘制自己。可行,但需要一段时间才能弄清楚并使其在打印机上运行。
【讨论】:
我建议您尝试 2Printer 命令行工具: http://www.doc2prn.com/
打印文件夹“C:\Input”中所有 PDF 文件的命令行示例如下。您可以从 C# 代码中简单地调用它。
2Printer.exe -s "C:\Input*.PDF" -prn "Canon MP610 series Printer"
【讨论】:
我在打印 PDF 文件时遇到了同样的问题。有一个名为 Spire.Pdf 的 nuget 包,使用起来非常简单。免费版有 10 页的限制,但在我的情况下,一旦我不想依赖 Adobe Reader 并且不想安装任何其他组件,它是最好的解决方案。
https://www.nuget.org/packages/Spire.PDF/
PdfDocument pdfdocument = new PdfDocument();
pdfdocument.LoadFromFile(pdfPathAndFileName);
pdfdocument.PrinterName = "My Printer";
pdfdocument.PrintDocument.PrinterSettings.Copies = 2;
pdfdocument.PrintDocument.Print();
pdfdocument.Dispose();
【讨论】:
也可以使用嵌入式 Web 浏览器执行此操作,但请注意,因为这可能是本地文件,并且因为它实际上不是直接的浏览器并且没有 DOM,因此没有就绪状态。
这是我在 win 表单 Web 浏览器控件上制定的方法的代码:
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(@"path\to\file");
}
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
//Progress Changed fires multiple times, however after the Navigated event it is fired only once,
//and at this point it is ready to print
webBrowser1.ProgressChanged += (o, args) =>
{
webBrowser1.Print();//Note this does not print only brings up the print preview dialog
//Should be on a separate task to ensure the main thread
//can fully initialize the print dialog
Task.Factory.StartNew(() =>
{
Thread.Sleep(1000);//We need to wait before we can send enter
//This assumes that the print preview is still in focus
Action g = () =>
{
SendKeys.SendWait("{ENTER}");
};
this.Invoke(g);
});
};
}
【讨论】:
您可以使用PdfSharp 创建 PDF 文档。它是一个开源的 .NET 库。
在尝试打印文档时,情况会变得更糟。我一直在寻找一种开源的方法。使用 AcroRd32.exe 有一些方法可以做到这一点,但这完全取决于版本,如果没有 acrobat reader 保持打开状态,就无法做到。
我最终使用了VintaSoftImaging.NET SDK。它需要一些钱,但比替代方案便宜得多,而且解决问题真的很容易。
var doc = new Vintasoft.Imaging.Print.ImagePrintDocument { DocumentName = @"C:\Test.pdf" };
doc.Print();
这只是打印到默认打印机而不显示。有多种选择。
【讨论】:
acrord32.exe 进行打印。见source code。
如果您只是想以编程方式打印 PDF 文件,另一种方法是使用 LPR 命令: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/lpr.mspx?mfr=true
LPR 也适用于较新版本的 Windows(例如 Vista/7),但您需要在可选 Windows 组件中启用它。
例如:
Process.Start("LPR -S printerdnsalias -P raw C:\files\file.pdf");
您也可以使用打印机 IP 地址代替别名。
这假设您的打印机支持PDF 直接打印,否则这仅适用于 PostScript 和 ASCII 文件。此外,打印机需要安装网络接口,并且您需要知道它的 IP 地址或别名。
可以使用 Ghostscript 读取 PDF 文件并将它们打印到指定的打印机。
【讨论】:
这取决于您要打印的内容。您需要第三方 pdf 打印机应用程序,或者如果您要打印自己的数据,您可以在 Visual Studio 中使用报表查看器。它可以将报告输出到 excel 和 pdf 文件。
【讨论】:
一种非常直接的方法是使用已安装的 Adobe Reader 或任何其他能够打印的 PDF 查看器:
Process p = new Process( );
p.StartInfo = new ProcessStartInfo( )
{
CreateNoWindow = true,
Verb = "print",
FileName = path //put the correct path here
};
p.Start( );
另一种方法是使用第三方组件,例如PDFView4NET
【讨论】: