How to programmatically print to PDF file without prompting for filename in C# using the Microsoft Print To PDF printer that comes with Windows 10
C#PdfPrintingWindows 10C# Problem Overview
Microsoft Windows 10 comes with a Microsoft Print To PDF printer which can print something to a PDF file. It prompts for the filename to download.
How can I programmatically control this from C# to not prompt for the PDF filename but save to a specific filename in some folder that I provide?
This is for batch processing of printing a lot of documents or other types of files to a PDF programmatically.
C# Solutions
Solution 1 - C#
To print a PrintDocument
object using the Microsoft Print to PDF printer without prompting for a filename, here is the pure code way to do this:
// 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();
You can also use this method for other Save as File type printers such as Microsoft XPS Printer
Solution 2 - C#
You can print to the Windows 10 PDF printer, by using the PrintOut
method and specifying the fourth output file name parameter, as in the following example:
/// <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
);
}
The OutputFile
is a full path string of the input document you would like to convert, and the doc is a regular document object. For more info about the doc please see the following MSDN links for _Document.PrintOut()
The PrintOut
in the example results a silent print, when you print through the specified inputFile
to the OutputFileName
, which will be placed in the same folder as the original document, but it will be in PDF format with the .pdf
extension.