3

I try to print a ms project file to pdf without any additional action needed.

I have tried several solutions without success.

PrintDocument doc = new PrintDocument()
{
//DocumentName = safeDir + fileName,
    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 = safeDir + fileName,
    }
};
doc.Print();

If i try this approach like showed here, I get an empty pdf file.

Manually printing to PDF works fine.

Any suggestions to solve this problem?

mjwills
  • 23,389
  • 6
  • 40
  • 63
ket0ma
  • 81
  • 2
  • 7

1 Answers1

0

My Spidey Senses tells me this is most likely caused by commas in the file name

This is a known bug when printing to PDF. Use a different driver or don't put a comma in the file name

Note : Its not only from code, its in general in certain situations.

Disclaimer : this is a complete and utter guess


Check these other links out

Bug in "Print to PDF" and "Print to XPS" in Windows 10? comma in filename results in zero-byte file

Microsoft Print to PDF not working

Found a bug in Microsoft Print to PDF

Microsoft Print to PDF in Windows 10

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • 1
    I do not have a comma in the file name. And manually printing with the same filename works fine. Even with Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\test123.pdf" as savepath does not work. file ist only 1KB big. I can open it but it is just white. But thank you for your tip. – ket0ma Feb 26 '18 at 09:54