Programmatically set the file name and path in the Microsoft printer. Print to PDF

I have a C# .net program that creates various documents. These documents must be stored in different places and with different clearly defined names.

For this, I use the System.Drawing.Printing.PrintDocument class. I choose Microsoft Print to PDF as a printer with this statement:

PrintDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF" ;

With this, I can print my document in pdf file . The user receives a file selection dialog. Then he can specify in this dialog box the name of the pdf file and where to store it.

Since the number of files is large and it is annoying and error prone to always find the correct path and name, I would like to set the correct path and file name in this dialog box programmatically.

I have already tested these attributes:

PrintDocument.PrinterSettings.PrintFileName PrintDocument.DocumentName

Writing the required path and file name to these attributes did not help. Does anyone know how to set default values โ€‹โ€‹for path and file name for Microsoft Print to PDF printer in C #?

Note. My environment : Windows 10, Visual Studio 2010, .net framework 4.5

+6
source share
3 answers

As indicated in other answers, you can force PrinterSettings.PrintToFile = true and set PrinterSettings.PrintFileName , but then your user will not receive a save popup. My solution is to continue and show the Save As dialog myself, filling it with the โ€œsuggestedโ€ file name [in my case, a text file (.txt), which I will switch to .pdf], then set PrintFileName to the result.

 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(); } } 
+1
source

It appears that PrintFilename ignored if the PrintToFile property PrintToFile not set to true . If the PrintToFile parameter PrintToFile set to true and a valid file name (full path) is specified, the filedialog in which the user selects the file name will not be displayed.

Tip. When setting the printer name in the print settings, you can check the IsValid property to see if this printer really exists. For more information about printers and finding installed printers, check this box.

0
source

I was experimenting a bit, but like you, I was also unable to pre-populate the SaveAs dialog in PrintDialog with DocumentName or PrinterSettings.PrintFileName already populated by the PrintDocument instance. This seems controversial, so maybe I missed something.

If you want, you can, however, bypass printing and print automatically without any user interaction. If you decide to do this, you must make sure in advance that the directory to which you want to add the document exists and that the document to be added does not exist.

 string existingPathName = @"C:\Users\UserName\Documents"; string notExistingFileName = @"C:\Users\UserName\Documents\TestPrinting1.pdf"; if (Directory.Exists(existingPathName) && !File.Exists(notExistingFileName)) { PrintDocument pdoc = new PrintDocument(); pdoc.PrinterSettings.PrinterName = "Microsoft Print to PDF"; pdoc.PrinterSettings.PrintFileName = notExistingFileName; pdoc.PrinterSettings.PrintToFile = true; pdoc.PrintPage += pdoc_PrintPage; pdoc.Print(); } 
0
source

All Articles