在C#中打印Pdf

问题描述 投票:29回答:15

我是c#的新手。我正在网上寻找关于如何打印pdf的教程,但找不到一个。

然后我想,是否可以使用itextpdf读取它,就像这里提到的那样

Reading PDF content with itextsharp dll in VB.NET or C#

然后打印出来。如果是这样,怎么样?

c# .net pdf printing
15个回答
52
投票

一种非常直接的方法是使用已安装的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


0
投票

这取决于您要打印的内容。您需要第三方pdf打印机应用程序,或者如果您打印自己的数据,您可以在visual studio中使用报表查看器。它可以将报告输出到excel和pdf -files。


0
投票

也可以使用嵌入式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);
            });
        };
    }

0
投票

看起来像pdfsharp和migradoc这样的常见嫌疑人无法做到这一点(仅当安装了Acrobat(Reader)时才为pdfsharp)。

我在这里找到了

https://vishalsbsinha.wordpress.com/2014/05/06/how-to-programmatically-c-net-print-a-pdf-file-directly-to-the-printer/

代码准备复制/粘贴。它使用默认打印机,从我可以看到它甚至不使用任何库,直接将pdf字节发送到打印机。所以我认为打印机也需要支持它,在一台10年历史的打印机上我测试过它完美无缺。

大多数其他方法 - 没有商​​业库或应用程序 - 要求您在打印设备上下文中绘制自己。可行,但需要一段时间来弄清楚它并使其适用于打印机。


0
投票

使用Ultimate PDF的直观API,使用几行代码打开,导入,编辑,合并,转换Acrobat PDF文档。通过使用用C#编写的100%托管代码,该组件利用.NET Framework的众多内置功能来提高性能。此外,该库符合CLS,并且它不使用任何不安全的块来实现最小的权限要求。这些课程详细记录了详细的示例代码,有助于缩短您的学习曲线。如果您的开发环境是Visual Studio,请享受在线文档的完整集成。只需标记或选择一个关键字,然后在Visual Studio IDE中按F1,即可立即显示在线文档。一个高性能且可靠的PDF库,可让您使用几行代码轻松地将PDF功能添加到.NET应用程序中。

PDF Component for NET


0
投票

如果您安装了Adobe Reader,那么您应该可以将其设置为默认打印机。还有VOILA!你可以打印到PDF!

printDocument1.PrinterSettings.PrinterName = "Adobe PDF";
printDocument1.Print();

就这么简单!!!


0
投票

从C#自动打印pdf的最佳方法是使用打印机的“直接pdf”。您只需将pdf文件复制到打印机的网络共享名称即可。其余的将由打印机本身照顾。

速度比任何其他方法快10倍。但是,要求是支持直接pdf打印的打印机型号,并且具有至少128 MB Dram,这对于任何现代打印机都很容易。


23
投票

我在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;
  }

一个人不能依赖btw方法的返回值...


10
投票

如果您只是希望以编程方式打印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地址或别名。


6
投票

我在打印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();

3
投票

您可以使用PdfSharp创建PDF文档。它是一个开源的.NET库。

当试图打印文档时,情况会变得更糟。我已经全神贯注于开源的方式。有一些方法可以使用AcroRd32.exe,但这一切都取决于版本,如果没有acrobat reader保持打开状态,它就无法完成。

我终于结束了使用VintaSoftImaging.NET SDK。它花费了一些钱,但比替代品便宜得多,它解决了这个问题非常容易。

var doc = new Vintasoft.Imaging.Print.ImagePrintDocument { DocumentName = @"C:\Test.pdf" };
doc.Print();

这只是打印到默认打印机而不显示。有几种选择和选择。


3
投票

使用PDFiumViewer。我搜索了很长时间,直到我想出了一个类似的解决方案,然后我发现这段干净的代码不依赖于将原始文件发送到打印机(如果它们被解释为文本文件那么会很糟糕)或使用Acrobat或Ghostscript作为助手(两者都需要安装,这很麻烦):

https://stackoverflow.com/a/41751184/586754

PDFiumViewer来自nuget,上面的代码示例已经完成。传入空值以使用默认打印机。


1
投票

可以使用Ghostscript读取PDF文件并将其打印到指定的打印机。


1
投票

我建议你试试2Printer命令行工具:http://www.doc2prn.com/

从文件夹“C:\ Input”打印所有PDF文件的命令行示例如下所示。您可以从C#代码中简单地调用它。

2Printer.exe -s“C:\ Input * .PDF”-prn“Canon MP610 series Printer”


1
投票

最简单的方法是创建C#Process并启动外部工具来打印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

© www.soinside.com 2019 - 2024. All rights reserved.