Using .Net 3.0 and Word Automation to print a DOCX file and convert to a PDF

/// <summary>
/// print DOCX file and then make a copy to a PDF file
/// – uses Office / Word Automation since styles embedded in DOCX known only to Word
/// </summary>
/// <param name=”fileToPrint”>name of template DOCX file</param>
/// <param name=”paramExportFilePath”>The order to be converted ready for printing</param>
/// <returns>boolean representing success printing the DOCX file then converting the file from DOCX to PDF</returns>

public bool printWordDoc(string fileToPrint, string paramExportFilePath)
{
    bool retval = false;
    object paramSourceDocPath = fileToPrint;
    // PDF export parameters
    WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;

    bool paramOpenAfterExport = false;
    WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;
    WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
    int paramStartPage = 0;
    int paramEndPage = 0;
    WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
    bool paramIncludeDocProps = true;
    bool paramKeepIRM = true;
    WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;
    bool paramDocStructureTags = true;
    bool paramBitmapMissingFonts = true;
    bool paramUseISO19005_1 = false;

    // general parameters
    object varFalseValue = false;
    object varTrueValue = true;

    object paramMissing = Type.Missing;
    // create word doc (requires MS Word 2007)
    ApplicationClass wordApplication = new ApplicationClass();
    Document wordDocument = null;
    try
    {
        // Open the source document (requires MS Word 2007)
        wordDocument = wordApplication.Documents.Open(ref paramSourceDocPath, ref paramMissing,
            ref varFalseValue, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing,
            ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref varFalseValue,
            ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing);

        if (wordDocument != null)
        {
            // print the document (requires MS Word 2007)
            wordDocument.Activate();
            wordDocument.PrintOut(ref varFalseValue, ref varFalseValue, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref varFalseValue, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing);

            // Export DOCX file to PDF format (requires free MS Office PDF/XPS add ons to be installed)
            wordDocument.ExportAsFixedFormat(paramExportFilePath, paramExportFormat, paramOpenAfterExport,
                paramExportOptimizeFor, paramExportRange, paramStartPage, paramEndPage, paramExportItem,

paramIncludeDocProps, paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                paramBitmapMissingFonts, paramUseISO19005_1, ref paramMissing);
            }
            retval = true;
        }
        catch (Exception ex)
        {
            // Respond to the error
            MessageBox.Show(“An error occured while trying to print and convert document\n\n”+ex.Message);
            retval = false;
        }
        finally
        {
            // Close and release the Document object.
            if (wordDocument != null)
            {
                wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                wordDocument = null;
            }
            // Quit Word and release the ApplicationClass object.
            if (wordApplication != null)
            {
                wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                wordApplication = null;
            }
            // force garbage collection
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        return retval;
}

Advertisements