Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I should print PDF using my C++ app in Windows 10 without opening "Save file dialog". Previously I used "Generic Postscript Printer" to save file with the given name as ".ps" and than converted it to PDF using another application. Windows 10 doesn't support "Generic Postscript Printer" but has embedded "Microsoft Print to Pdf" printer. I know how to give it file name + silent printing property using C#

// 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"),
    };
}


but can't find C++ solution. Does anybody know if it exists at all?

What I have tried:

I use StartDoc function and the printer opens the dialog for saving the file (it works well and my stream is printed fine). However, I need "silent" solution (without dialog).
Posted
Updated 24-Dec-20 18:38pm
Comments
Richard MacCutchan 6-Jun-17 5:07am    
Using C++ will be the same; just set the various fields in the Printer settings before starting to print.
juliashs 6-Jun-17 5:20am    
Thank you! I tried but I can't find appropriate reference for C++ for API to the printer
juliashs 6-Jun-17 5:41am    
Yes! Thank you very much!
The following code works fine:

DOCINFO Dinfo ;
...
Dinfo.lpszOutput = (LPCSTR)"D:\\Test1.pdf";
KarstenK 6-Jun-17 6:24am    
please write such answer as solution to mark it as answered. ;-)

The following code works fine: 

DOCINFO Dinfo ;
...
Dinfo.lpszOutput = (LPCSTR)"D:\\Test1.pdf";
 
Share this answer
 
Try 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();
 
Share this answer
 
Comments
Stefan_Lang 26-Dec-20 6:23am    
Not helping!
1. you're 3.5 years late to the question
2. There already is a solution
3. Your post doesn't answer the question.
4. Instead you posted C# code that is already mentioned in the question itself!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900