我有一个C# .net
创建各种文档的程序。这些文档应存储在不同的位置,并使用不同的、明确定义的名称。
为此,我使用System.Drawing.Printing.PrintDocument
班级。
我选择的是Microsoft Print to PDF
作为具有以下声明的打印机:
PrintDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
;
这样做时,我可以打印我的文档pdf file
。用户会看到一个文件选择对话框。然后,他可以在此对话框中指定 pdf 文件的名称及其存储位置。
由于文件量很大,并且始终找到正确的路径和名称很烦人且容易出错,因此我想以编程方式在此对话框中设置正确的路径和文件名。
我已经测试过这些属性:
PrintDocument.PrinterSettings.PrintFileName
PrintDocument.DocumentName
将所需的路径和文件名写入这些属性没有帮助。
有谁知道如何设置路径和文件名的默认值Microsoft Print to PDF
C# 中的打印机?
注:我的环境:
Windows 10,
视觉工作室 2010,
.net框架4.5
正如其他答案中所述,您可以强制PrinterSettings.PrintToFile = true
,并设置PrinterSettings.PrintFileName
,但是您的用户不会获得另存为弹出窗口。我的解决方案是继续自己显示“另存为”对话框,用我的“建议”文件名填充该对话框[在我的例子中,是一个文本文件 (.txt),我将其更改为 .pdf],然后设置PrintFileName
到结果。
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();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)