Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm trying to create automatically PDF documents from WORD documents, using a PDF creator as a default printer. However, I don't want to use the PrintDialog everytime, I need to make it automatic by the source code ( I have thousands of WORD documents). The problem is that it seems I cannot inherit all the settings, coming with the PrintDialog and I can
create a PDF file but it's 0kb. It raises all the PrintDocument events
(BeginPrint,QueryPageSettings,PrintPage,EndPrint), but finally the PDF document is 0kb.

Does anyone met this issue?

Here is a sample with static fields:

private void DocToPdf()
 {
     PrintDocument pdDoc = new PrintDocument();
     try
     {
         pdDoc.DocumentName = @"C:\Input.doc";
         pdDoc.PrinterSettings.PrintToFile = true;
         pdDoc.PrinterSettings.PrintFileName = @"C:\Output.pdf";
         pdDoc.PrinterSettings.PrinterName = "My_printer";
         pdDoc.DefaultPageSettings = new PageSettings(new PrinterSettings());

         pdDoc.PrintPage += new PrintPageEventHandler(pdDoc_PrintPage);
         pdDoc.BeginPrint += new PrintEventHandler(pdDoc_BeginPrint);
         pdDoc.QueryPageSettings += new QueryPageSettingsEventHandler(pdDoc_QueryPageSettings);
         pdDoc.EndPrint += new PrintEventHandler(pdDoc_EndPrint);

         if (pdDoc.PrinterSettings.IsValid)
             pdDoc.Print();
     }
     catch (Exception ex)
     {
         string msg = ex.Message;
     }
 }
Posted
Updated 8-Feb-10 7:21am
v2

Are you sure that your PrintPage() method is correctly writing the data to the output device?
 
Share this answer
 
Hi Richard, thanks for the hint. Actually I solved the problem using interop functions. Your hint helped me to realize that I'm on the wrong way . :)
 
Share this answer
 

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