如何以编程方式打印到PDF文件,而不使用Windows 10附带的Microsoft Print To PDF打印机在C#中提示文件名

Microsoft Windows 10附带了一个可打印PDF文件的Microsoft Print To PDF打印机.它提示文件名下载.

我如何以编程方式控制这个从C#不提示PDF文件名,但保存到我提供的某个文件夹中的特定文件名?

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

要使用Microsoft Print to PDF打印机打印PrintDocument对象,而不提示文件名,以下是纯代码方法:
// generate a file name as the current date/time in unix timestamp format
file = (string)(DateTime.UtcNow.Subtract(new DateTime(1970,1,1))).TotalSeconds.ToString();

// the directory to store the output.
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打印机

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...