C#-如何使用PrintDocument以编程方式打印现有的PDF文件 [英] C# - How to programmatically print an existing PDF file using PrintDocument

查看:2763
本文介绍了C#-如何使用PrintDocument以编程方式打印现有的PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用pdf打印机第二次打印现有的pdf文件.

I want to print an existing pdf file a second time with a pdf printer.

我尝试使用PrintDocument.但是,如何告诉PrintDocument现有文档的名称呢?

I try to use PrintDocument. But how do I tell PrintDocument the name of the existing document??

提前谢谢!

推荐答案

有几种方法可以将现有文件打印到其他打印机.也有几个第三方库.有些是付费的,有些是免费的.但是,我将在两天后解释我实现该目标的方式.

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.

安装nuget软件包PdfiumViewer. 但是请不要安装最新版本,因为您将不得不为pdfium.dll安装另一个程序包,而这很忙.

Install the nuget package PdfiumViewer. But do not install the latest version as you will have to install another package for pdfium.dll and that is hectic.

Install-Package PdfiumViewer -Version 2.10.0

此版本随附pdfium.dll文件,因此您无需单独安装.展开您的解决方案资源管理器,然后右键单击x64和x86文件夹中的pdfium.dll.转到属性,然后设置复制到输出目录=始终复制.

This version comes with the pdfium.dll file so that you do not have to install it separately. Expand your solution explorer and right click on pdfium.dll inside x64 and x86 folder. Go to properties and set the 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();
    }
}

这篇关于C#-如何使用PrintDocument以编程方式打印现有的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆