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
- using System;
- using System.IO;
- using System.Threading;
- using Microsoft.Office.Interop.Word;
- using PDFCreator;
- namespace PdfPrinter
- {
- class Program
- {
- static int Main(string[] args)
- {
- WordConstants Constants = new WordConstants();
- string PDF_Printer = "PDFCreator";
- string infile = null;
- string outfile = null;
- Console.WriteLine("Creating PDF Document");
- string currentArg = "";
- foreach (string arg in args)
- {
- if (arg.Equals("-infile:", StringComparison.OrdinalIgnoreCase) ||
- arg.Equals("-outfile:", StringComparison.OrdinalIgnoreCase) ||
- arg.Equals("-printer:", StringComparison.OrdinalIgnoreCase)
- )
- {
- currentArg = arg.ToLower();
- }
- else
- {
- switch (currentArg)
- {
- case "-infile:":
- {
- infile = arg;
- currentArg = null;
- break;
- }
- case "-outfile:":
- {
- outfile = arg;
- currentArg = null;
- break;
- }
- case "-printer:":
- {
- PDF_Printer = arg;
- currentArg = null;
- break;
- }
- default:
- {
- break;
- }
- }
- }
- }
- //Verify the input file exists
- if (string.IsNullOrEmpty(infile) ||
- !File.Exists(infile))
- {
- Console.WriteLine("Error - Input file '{0}' does not exist.", infile);
- return -1;
- }
- if (string.IsNullOrEmpty(outfile))
- {
- Console.WriteLine("Error - No output file.");
- return -1;
- }
- //Check that the output directory exists
- if (!Directory.Exists(Path.GetDirectoryName(outfile)))
- {
- Directory.CreateDirectory(Path.GetDirectoryName(outfile));
- }
- clsPDFCreator pdfCreator = null;
- try
- {
- pdfCreator = new clsPDFCreator();
- if (!pdfCreator.cStart("/NoProcessingAtStartup", false))
- {
- Console.WriteLine("Unable to start PDFCreator.");
- return -1;
- }
- pdfCreator.cWindowState = 1;
- // Save currently active printer.
- string defaultPrinter = pdfCreator.cDefaultPrinter;
- // Set parameters for saving the generating pdf automatically to a directory.
- clsPDFCreatorOptions pdfPrintOptions = pdfCreator.cOptions;
- // Use auto save functionality.
- pdfPrintOptions.UseAutosave = 1;
- // Use directory for saving the file.
- pdfPrintOptions.UseAutosaveDirectory = 1;
- // Name of the output directory.
- pdfPrintOptions.AutosaveDirectory = Path.GetDirectoryName(outfile);
- // Format in which file is to be saved. 0 if for pdf.
- pdfPrintOptions.AutosaveFormat = 0;
- // Name of the output file name.
- pdfPrintOptions.AutosaveFilename = Path.GetFileName(outfile);
- //Set the Options
- pdfCreator.cOptions = pdfPrintOptions;
- //Clear the Cache
- pdfCreator.cClearCache();
- //Stop the Printer
- pdfCreator.cPrinterStop = true;
- Console.WriteLine("Starting Word");
- // Create new instance of word application.
- ApplicationClass word = new ApplicationClass();
- // Set pdf creator as active printer. Name should be same as you gave while installation.
- try
- {
- //Set the PDF Printer Current
- word.ActivePrinter = PDF_Printer;
- Object documentName = infile;
- //Open the Document
- 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);
- Console.WriteLine("Printing PDF Document");
- //Print the document to the PDF Printer
- 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);
- // Wait untill the document is queued.
- while (pdfCreator.cCountOfPrintjobs != 1)
- {
- Thread.Sleep(100);
- }
- // Start the printing.
- pdfCreator.cPrinterStop = false;
- // Wait until the print queue is completed.
- while (pdfCreator.cCountOfPrintjobs != 0)
- {
- Thread.Sleep(100);
- }
- // Close all the opened documents.
- foreach (Document doc in word.Documents)
- {
- doc.Close(ref Constants.FalseValue, ref Constants.MissingValue, ref Constants.MissingValue);
- }
- // Stop the printer.
- pdfCreator.cPrinterStop = true;
- }
- finally
- {
- // Set back the default printer.
- word.ActivePrinter = defaultPrinter;
- word.Quit(ref Constants.FalseValue, ref Constants.MissingValue, ref Constants.MissingValue);
- }
- Console.WriteLine("PDF Document Created {0}", pdfCreator.cOutputFilename);
- }
- finally
- {
- // Shutdown the PDF Printer
- if (pdfCreator != null)
- {
- pdfCreator.cClose();
- pdfCreator = null;
- }
- }
- return 0;
- }
- }
- class WordConstants
- {
- public Object MissingValue = Type.Missing;
- public Object FalseValue = false;
- public Object background = true;
- public Object append = true;
- public Object range = Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument;
- public Object outputFileName = Type.Missing;
- public Object from = Type.Missing;
- public Object to = Type.Missing;
- public Object item = Microsoft.Office.Interop.Word.WdPrintOutItem.wdPrintDocumentContent;
- public Object copies = 1;
- public Object pages = Type.Missing;
- public Object pageType = Microsoft.Office.Interop.Word.WdPrintOutPages.wdPrintAllPages;
- public Object printToFile = false;
- public Object collate = Type.Missing;
- public Object fileName = Type.Missing;
- public Object activePrinterMacGX = Type.Missing;
- public Object manualDuplexPrint = Type.Missing;
- public Object printZoomColumn = Type.Missing;
- public Object printZoomRow = Type.Missing;
- public Object printZoomPaperWidth = Type.Missing;
- public Object printZoomPaperHeight = Type.Missing;
- }
- }
That was an inspiring post,
Thanks for sharing,
Keep up the good work
Posted by: software development in Surrey | December 22, 2009 at 04:49 PM