Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 447
pdfman Avatar asked Aug 08 '15 18:08

pdfman


People also ask

How do I print without opening a file?

When you want to print a document without opening it, simply drag the document file to the correct printer icon on your desktop. You can drag multiple files simultaneously and they will print, but not in any particular order.

How do I get a PDF to automatically print a folder?

Print Conductor is a simple desktop software for printing documents automatically. It is mainly based on the drag-and-drop option: select the folder you wish to print out, and all its contents will be added in the Print Conductor window. Then press Start, and all your documents will be printed at once!


1 Answers

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

like image 65
Kraang Prime Avatar answered Sep 28 '22 10:09

Kraang Prime