简体   繁体   English

C# - 如何使用 PrintDocument 以编程方式打印现有的 PDF 文件

[英]C# - How to programmatically print an existing PDF file using PrintDocument

I want to print an existing pdf file a second time with a pdf printer.我想用 pdf 打印机第二次打印现有的 pdf 文件。

I try to use PrintDocument.我尝试使用 PrintDocument。 But how do I tell PrintDocument the name of the existing document??但是我如何告诉 PrintDocument 现有文档的名称?

Thanks in advance!提前致谢!

There are several ways you can print an existing file to different printer.有多种方法可以将现有文件打印到不同的打印机。 There are several third party libraries as well.还有几个第三方库。 Some are paid and some are free.有些是付费的,有些是免费的。 However I shall explain the way I had achieved it after 2 days.但是,我将在 2 天后解释我实现它的方式。

Install the nuget package PdfiumViewer.安装 nuget 包 PdfiumViewer。 But do not install the latest version as you will have to install another package for pdfium.dll and that is hectic.但是不要安装最新版本,因为您必须为 pdfium.dll 安装另一个包,这很忙。

Install-Package PdfiumViewer -Version 2.10.0

This version comes with the pdfium.dll file so that you do not have to install it separately.此版本随附 pdfium.dll 文件,因此您无需单独安装。 Expand your solution explorer and right click on pdfium.dll inside x64 and x86 folder.展开您的解决方案资源管理器并右键单击 x64 和 x86 文件夹中的 pdfium.dll。 Go to properties and set the Copy to Output Directory = Copy always .转到属性并设置Copy to Output Directory = Copy always

在此处输入图片说明

Now that the setup is complete, you can go ahead with the code.现在设置已完成,您可以继续执行代码。

var path = @"path\file.pdf";
using (var document = PdfDocument.Load(path))
{
    using (var printDocument = document.CreatePrintDocument())
    {
        printDocument.PrinterSettings.PrintFileName = "Letter_SkidTags_Report_9ae93aa7-4359-444e-a033-eb5bf17f5ce6.pdf";
        printDocument.PrinterSettings.PrinterName = @"printerName";
        printDocument.DocumentName = "file.pdf";
        printDocument.PrinterSettings.PrintFileName = "file.pdf";
        printDocument.PrintController = new StandardPrintController();
        printDocument.Print();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM