PVi*_*itt 54
一种非常直接的方法是使用已安装的Adobe Reader或任何其他能够打印的PDF查看器:
Process p = new Process( );
p.StartInfo = new ProcessStartInfo( )
{
CreateNoWindow = true,
Verb = "print",
FileName = path //put the correct path here
};
p.Start( );
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用第三方组件,例如PDFView4NET
san*_*nta 24
我在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;
}
Run Code Online (Sandbox Code Playgroud)
一个人不能依赖btw方法的返回值...
Les*_*ezi 11
另一种方法,如果您只是希望以编程方式打印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");
Run Code Online (Sandbox Code Playgroud)
您也可以使用打印机IP地址而不是别名.
这假设您的打印机支持PDF直接打印,否则这只适用于PostScript和ASCII文件.此外,打印机需要安装网络接口,您需要知道它的IP地址或别名.
使用 PDFiumViewer。我搜索了很长时间,直到我想出了一个类似的解决方案,然后我发现了这段干净的代码,它不依赖于将原始文件发送到打印机(如果它们被解释为文本文件,那就不好了……)或使用Acrobat 或 Ghostscript 作为助手(两者都需要安装,这很麻烦):
PDFiumViewer 来自nuget,上面的代码示例已经完成。传入空值以使用默认打印机。
我在打印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();
Run Code Online (Sandbox Code Playgroud)
您可以使用PdfSharp创建 PDF 文档。它是一个开源的 .NET 库。
尝试打印文档时,情况变得更糟。我一直在寻找一种开源的方式来做这件事。有一些方法可以使用AcroRd32.exe来做到这一点,但这完全取决于版本,如果没有 acrobat 阅读器保持打开状态,就无法做到这一点。
我最终使用了VintaSoftImaging.NET SDK。它花费一些钱,但比替代方案便宜得多,而且它很容易解决问题。
var doc = new Vintasoft.Imaging.Print.ImagePrintDocument { DocumentName = @"C:\Test.pdf" };
doc.Print();
Run Code Online (Sandbox Code Playgroud)
那只是打印到默认打印机而不显示。有几种替代方案和选项。
小智 5
从 C# 自动打印 pdf 的最佳方法是使用打印机的“直接 pdf”。您只需将 pdf 文件复制到打印机的网络共享名即可。其余的将由打印机本身处理。
速度比任何其他方法快10倍。但是,要求打印机型号支持直接 pdf 打印并具有至少 128 MB Dram,这对于任何现代打印机来说都很容易。