X
logo
Bringing your data sources together
7 December 2009

Creating PDF Documents with PDF Creator

This sample shows how you can create a command line utility program to automatically convert Word (.doc) documents into a PDF file with the PDF Creator Library.

Code Snippet
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using Microsoft.Office.Interop.Word;
  5. using PDFCreator;
  6.  
  7. namespace PdfPrinter
  8. {
  9.     class Program
  10.     {
  11.         static int Main(string[] args)
  12.         {
  13.             WordConstants Constants = new WordConstants();
  14.  
  15.             string PDF_Printer = "PDFCreator";
  16.             
  17.             string infile = null;
  18.             string outfile = null;
  19.  
  20.             Console.WriteLine("Creating PDF Document");
  21.  
  22.             string currentArg = "";
  23.             foreach (string arg in args)
  24.             {
  25.                 if (arg.Equals("-infile:", StringComparison.OrdinalIgnoreCase) ||
  26.                     arg.Equals("-outfile:", StringComparison.OrdinalIgnoreCase) ||
  27.                     arg.Equals("-printer:", StringComparison.OrdinalIgnoreCase)
  28.                     )
  29.                 {
  30.                     currentArg = arg.ToLower();
  31.                 }
  32.                 else
  33.                 {
  34.                     switch (currentArg)
  35.                     {
  36.                         case "-infile:":
  37.                             {
  38.                                 infile = arg;
  39.                                 currentArg = null;
  40.                                 break;
  41.                             }
  42.                         case "-outfile:":
  43.                             {
  44.                                 outfile = arg;
  45.                                 currentArg = null;
  46.                                 break;
  47.                             }
  48.                         case "-printer:":
  49.                             {
  50.                                 PDF_Printer = arg;
  51.                                 currentArg = null;
  52.                                 break;
  53.                             }
  54.  
  55.                         default:
  56.                             {
  57.                                 break;
  58.                             }
  59.                     }
  60.                 }
  61.             }
  62.  
  63.             //Verify the input file exists
  64.             if (string.IsNullOrEmpty(infile) ||
  65.                 !File.Exists(infile))
  66.             {
  67.                 Console.WriteLine("Error - Input file '{0}' does not exist.", infile);
  68.                 return -1;
  69.             }
  70.  
  71.             if (string.IsNullOrEmpty(outfile))
  72.             {
  73.                 Console.WriteLine("Error - No output file.");
  74.                 return -1;            
  75.             }
  76.  
  77.             //Check that the output directory exists
  78.             if (!Directory.Exists(Path.GetDirectoryName(outfile)))
  79.             {
  80.                 Directory.CreateDirectory(Path.GetDirectoryName(outfile));
  81.             }
  82.  
  83.             clsPDFCreator pdfCreator = null;
  84.             try
  85.             {
  86.                 pdfCreator = new clsPDFCreator();
  87.  
  88.                 if (!pdfCreator.cStart("/NoProcessingAtStartup", false))
  89.                 {
  90.                     Console.WriteLine("Unable to start PDFCreator.");
  91.                     return -1;
  92.                 }
  93.  
  94.                 pdfCreator.cWindowState = 1;
  95.  
  96.                 // Save currently active printer.
  97.                 string defaultPrinter = pdfCreator.cDefaultPrinter;
  98.  
  99.                 // Set parameters for saving the generating pdf automatically to a directory.
  100.                 clsPDFCreatorOptions pdfPrintOptions = pdfCreator.cOptions;
  101.                 // Use auto save functionality.
  102.                 pdfPrintOptions.UseAutosave = 1;
  103.                 // Use directory for saving the file.
  104.                 pdfPrintOptions.UseAutosaveDirectory = 1;
  105.                 // Name of the output directory.
  106.                 pdfPrintOptions.AutosaveDirectory = Path.GetDirectoryName(outfile);
  107.                 // Format in which file is to be saved. 0 if for pdf.
  108.                 pdfPrintOptions.AutosaveFormat = 0;
  109.                 // Name of the output file name.
  110.                 pdfPrintOptions.AutosaveFilename = Path.GetFileName(outfile);
  111.                 //Set the Options
  112.                 pdfCreator.cOptions = pdfPrintOptions;
  113.                 //Clear the Cache
  114.                 pdfCreator.cClearCache();
  115.                 //Stop the Printer
  116.                 pdfCreator.cPrinterStop = true;
  117.  
  118.                 Console.WriteLine("Starting Word");
  119.                 // Create new instance of word application.
  120.                 ApplicationClass word = new ApplicationClass();
  121.                 // Set pdf creator as active printer. Name should be same as you gave while installation.
  122.                 try
  123.                 {
  124.                     //Set the PDF Printer Current
  125.                     word.ActivePrinter = PDF_Printer;
  126.  
  127.                     Object documentName = infile;
  128.                     //Open the Document
  129.                     Document document = word.Documents.Open(ref documentName, ref Constants.MissingValue, ref Constants.MissingValue, ref Constants.MissingValue, ref Constants.MissingValue, ref Constants.MissingValue, ref Constants.MissingValue, ref Constants.MissingValue, ref Constants.MissingValue, ref Constants.MissingValue, ref Constants.MissingValue, ref Constants.MissingValue, ref Constants.MissingValue, ref Constants.MissingValue, ref Constants.MissingValue, ref Constants.MissingValue);
  130.                     
  131.                     Console.WriteLine("Printing PDF Document");
  132.                     //Print the document to the PDF Printer
  133.                     word.PrintOut(ref Constants.background, ref Constants.append, ref Constants.range, ref Constants.outputFileName, ref Constants.from, ref Constants.to, ref Constants.item, ref Constants.copies, ref Constants.pages, ref Constants.pageType, ref Constants.printToFile, ref Constants.collate, ref Constants.fileName, ref Constants.activePrinterMacGX, ref Constants.manualDuplexPrint, ref Constants.printZoomColumn, ref Constants.printZoomRow, ref Constants.printZoomPaperWidth, ref Constants.printZoomPaperHeight);
  134.                     // Wait untill the document is queued.
  135.                     while (pdfCreator.cCountOfPrintjobs != 1)
  136.                     {
  137.                         Thread.Sleep(100);
  138.                     }
  139.                     // Start the printing.
  140.                     pdfCreator.cPrinterStop = false;
  141.                     // Wait until the print queue is completed.
  142.                     while (pdfCreator.cCountOfPrintjobs != 0)
  143.                     {
  144.                         Thread.Sleep(100);
  145.                     }
  146.                     // Close all the opened documents.
  147.                     foreach (Document doc in word.Documents)
  148.                     {
  149.                         doc.Close(ref Constants.FalseValue, ref Constants.MissingValue, ref Constants.MissingValue);
  150.                     }
  151.  
  152.                     // Stop the printer.
  153.                     pdfCreator.cPrinterStop = true;
  154.  
  155.                 }
  156.                 finally
  157.                 {
  158.                     // Set back the default printer.
  159.                     word.ActivePrinter = defaultPrinter;
  160.                     word.Quit(ref Constants.FalseValue, ref Constants.MissingValue, ref Constants.MissingValue);
  161.                 }                
  162.                 
  163.                 Console.WriteLine("PDF Document Created {0}", pdfCreator.cOutputFilename);
  164.  
  165.             }
  166.             finally
  167.             {
  168.                 // Shutdown the PDF Printer
  169.                 if (pdfCreator != null)
  170.                 {
  171.                     pdfCreator.cClose();
  172.                     pdfCreator = null;
  173.                 }
  174.             }
  175.  
  176.             return 0;
  177.         }
  178.     }
  179.  
  180.     class WordConstants
  181.     {
  182.         public Object MissingValue = Type.Missing;
  183.         public Object FalseValue = false;
  184.         public Object background = true;
  185.         public Object append = true;
  186.         public Object range = Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument;
  187.         public Object outputFileName = Type.Missing;
  188.         public Object from = Type.Missing;
  189.         public Object to = Type.Missing;
  190.         public Object item = Microsoft.Office.Interop.Word.WdPrintOutItem.wdPrintDocumentContent;
  191.         public Object copies = 1;
  192.         public Object pages = Type.Missing;
  193.         public Object pageType = Microsoft.Office.Interop.Word.WdPrintOutPages.wdPrintAllPages;
  194.         public Object printToFile = false;
  195.         public Object collate = Type.Missing;
  196.         public Object fileName = Type.Missing;
  197.         public Object activePrinterMacGX = Type.Missing;
  198.         public Object manualDuplexPrint = Type.Missing;
  199.         public Object printZoomColumn = Type.Missing;
  200.         public Object printZoomRow = Type.Missing;
  201.         public Object printZoomPaperWidth = Type.Missing;
  202.         public Object printZoomPaperHeight = Type.Missing;
  203.  
  204.     }
  205. }