Microsoft Windows 10带有一个可以将内容打印成PDF文件的Microsoft Print To PDF 打印机。它会提示输入下载文件名。
如何使用C#编程实现对其进行控制,避免提示PDF文件名,而是保存到我提供的某个文件夹中指定的文件名?
这是为了批量处理大量文档或其他类型的文件以进行程序化PDF打印。
如何使用C#编程实现对其进行控制,避免提示PDF文件名,而是保存到我提供的某个文件夹中指定的文件名?
这是为了批量处理大量文档或其他类型的文件以进行程序化PDF打印。
使用纯代码的方式,在不提示文件名的情况下打印PrintDocument
对象,使用Microsoft Print to PDF打印机,以下是实现方法:
// 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 打印机
@Shahin Dohan
和@joym8
?我也有同样的问题。 - nampublic void PrintFunction() {
string outputPath = @"D:\Output\Folder\FileName.pdf";
PrintDocument pd = new PrintDocument();
pd.PrintPage += PrintPageHandler;
pd.PrinterSettings.PrintToFile = true;
pd.PrinterSettings.PrintFileName = outputPath;
pd.PrintController = new StandardPrintController();
pd.Print();
}
private void PrintPageHandler(object sender, PrintPageEventArgs e) {
// put your contents here for printing
e.Graphics.DrawString("Hello World!",
new Font("Arial", 12),
Brushes.Black,
100, // x-axis
100); // y-axis
}
string directory = Path.GetDirectoryName(outputPath);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
您可以通过使用PrintOut
方法并指定第四个输出文件名参数来打印到Windows 10 PDF打印机,如下例所示:
/// <summary>
/// Convert a file to PDF using office _Document object
/// </summary>
/// <param name="InputFile">Full path and filename with extension of the file you want to convert from</param>
/// <returns></returns>
public void PrintFile(string InputFile)
{
// convert input filename to new pdf name
object OutputFileName = Path.Combine(
Path.GetDirectoryName(InputFile),
Path.GetFileNameWithoutExtension(InputFile)+".pdf"
);
// Set an object so there is less typing for values not needed
object missing = System.Reflection.Missing.Value;
// `doc` is of type `_Document`
doc.PrintOut(
ref missing, // Background
ref missing, // Append
ref missing, // Range
OutputFileName, // OutputFileName
ref missing, // From
ref missing, // To
ref missing, // Item
ref missing, // Copies
ref missing, // Pages
ref missing, // PageType
ref missing, // PrintToFile
ref missing, // Collate
ref missing, // ActivePrinterMacGX
ref missing, // ManualDuplexPrint
ref missing, // PrintZoomColumn
ref missing, // PrintZoomRow
ref missing, // PrintZoomPaperWidth
ref missing, // PrintZoomPaperHeight
);
}
OutputFile
是您想要转换的输入文档的完整路径字符串,而 doc
则是一个常规文档对象。有关 _Document.PrintOut()
的更多信息,请参见以下 MSDN 链接:
在示例中,PrintOut
会静默打印,当您通过指定的 inputFile
打印到 OutputFileName
时,它将被放置在与原始文档相同的文件夹中,但它将以 PDF 格式呈现,并带有 .pdf
扩展名。