您可以使用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 时,该OutputFileName 将被放置在与原始文档相同的文件夹中,但它将是PDF 格式带有.pdf 扩展名。