以编程方式在Microsoft Print to PDF打印机中设置文件名和路径

问题描述:

我有一个C# .net程序,可以创建各种文档.这些文档应存储在不同的位置,并使用不同的,明确定义的名称.

I have a C# .net program that creates various documents. These documents should be stored in different locations and with different, clearly defined names.

为此,我使用System.Drawing.Printing.PrintDocument类. 我通过以下语句选择Microsoft Print to PDF作为打印机:

To do so, I use the System.Drawing.Printing.PrintDocument class. I select the Microsoft Print to PDF as printer with this statement:

PrintDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

这样做的同时,我可以在pdf file中打印文档.用户将获得一个文件选择对话框.然后,他可以在此对话框中指定pdf文件的名称及其存储位置.

While doing so I'm able to print my document in a pdf file. The user gets a file select dialog. He can then specify in this dialog box the name of the pdf file and where to store it.

由于文件数量很大,而且总是找到正确的路径和名称很烦人并且容易出错,我想以编程方式在此对话框中设置正确的路径和文件名.

As the amount of files are large and it is annoying and error-prone to find always the correct path and name, I would like to set the correct path and filename in this dialog box programmatically.

我已经测试了以下属性:

I have already tested these attributes:

PrintDocument.PrinterSettings.PrintFileName PrintDocument.DocumentName

PrintDocument.PrinterSettings.PrintFileName PrintDocument.DocumentName

将必需的路径和文件名写入这些属性没有帮助. 有人知道如何在C#中为Microsoft Print to PDF打印机设置路径和文件名的默认值吗?

Writing the required path and filename to these attributes didn't help. Does anybody know, how to set the default values for path and filename for the Microsoft Print to PDF printer in C#?

注意:我的环境: Windows 10, Visual Studio 2010, .net Framework 4.5

Note: My environment : Windows 10, Visual Studio 2010, .net framework 4.5

如其他答案所述,您可以强制PrinterSettings.PrintToFile = true并设置PrinterSettings.PrintFileName,但是这样您的用户将无法获得另存为弹出窗口.我的解决方案是继续显示自己的另存为"对话框,并用建议的"文件名填充(在本例中为我更改为.pdf的文本文件(.txt)),然后将PrintFileName设置为结果.

As noted in other answers, you can force PrinterSettings.PrintToFile = true, and set the PrinterSettings.PrintFileName, but then your user doesn't get the save as popup. My solution is to go ahead and show the Save As dialog myself, populating that with my "suggested" filename [in my case, a text file (.txt) which I change to .pdf], then set the PrintFileName to the result.

DialogResult userResp = printDialog.ShowDialog();
if (userResp == DialogResult.OK)
{
    if (printDialog.PrinterSettings.PrinterName == "Microsoft Print to PDF")
    {   // force a reasonable filename
        string basename = Path.GetFileNameWithoutExtension(myFileName);
        string directory = Path.GetDirectoryName(myFileName);
        prtDoc.PrinterSettings.PrintToFile = true;
        // confirm the user wants to use that name
        SaveFileDialog pdfSaveDialog = new SaveFileDialog();
        pdfSaveDialog.InitialDirectory = directory;
        pdfSaveDialog.FileName = basename + ".pdf";
        pdfSaveDialog.Filter = "PDF File|*.pdf";
        userResp = pdfSaveDialog.ShowDialog();
        if (userResp != DialogResult.Cancel)
            prtDoc.PrinterSettings.PrintFileName = pdfSaveDialog.FileName;
    }

    if (userResp != DialogResult.Cancel)  // in case they canceled the save as dialog
    {
        prtDoc.Print();
    }
}