如何使用Windows 10随附的Microsoft Print To PDF打印机以编程方式打印到PDF文件而不提示C#中的文件名 [英] How to programmatically print to PDF file without prompting for filename in C# using the Microsoft Print To PDF printer that comes with Windows 10

查看:502
本文介绍了如何使用Windows 10随附的Microsoft Print To PDF打印机以编程方式打印到PDF文件而不提示C#中的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Microsoft Windows 10随附了Microsoft Print To PDF打印机,可以将某些内容打印为PDF文件.它提示您下载文件名.

Microsoft Windows 10 comes with a Microsoft Print To PDF printer which can print something to a PDF file. It prompts for the filename to download.

如何从C#中以编程方式控制它,以不提示输入PDF文件名,而是保存到我提供的某些文件夹中的特定文件名?

How can I programmatically control this from C# to not prompt for the PDF filename but save to a specific filename in some folder that I provide?

这是用于以编程方式将大量文档或其他类型的文件打印到PDF的批处理.

This is for batch processing of printing a lot of documents or other types of files to a PDF programmatically.

推荐答案

要使用 Microsoft Print to PDF 打印机打印PrintDocument对象而不提示输入文件名,这是纯代码方式为此:

To print a PrintDocument object using the Microsoft Print to PDF printer without prompting for a filename, here is the pure code way to do this:

// generate a file name as the current date/time in unix timestamp format
string file = (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString();

// the directory to store the output.
string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

// initialize PrintDocument object
PrintDocument doc = new PrintDocument() {
    PrinterSettings = new PrinterSettings() {
        // set the printer to 'Microsoft Print to PDF'
        PrinterName = "Microsoft Print to PDF",

        // tell the object this document will print to file
        PrintToFile = true,

        // set the filename to whatever you like (full path)
        PrintFileName = Path.Combine(directory, file + ".pdf"),
    }
};

doc.Print();

您还可以将此方法用于其他另存为文件类型的打印机,例如 Microsoft XPS打印机

You can also use this method for other Save as File type printers such as Microsoft XPS Printer

这篇关于如何使用Windows 10随附的Microsoft Print To PDF打印机以编程方式打印到PDF文件而不提示C#中的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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