Methods for Converting Office Documents to PDF or XPS with C#

Recently, I had a great interest in how to convert Office documents to PDF or XPS. Therefore, I collected lots of materials. Now, I want to share a method I think is useful.

First, Office SaveAs PDF and XPS (an Add-in for Office 2007) needs to be installed. You can download it from the Microsoft official website.

Then, open VS. Take VS 2005 as an example. Create a new Windows application project and add the following COM components as references.

Microsoft Word 12.0 Object Library
Microsoft PowerPoint 12.0 Object Library
Microsoft Excel 12.0 Object Library

using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;

We can use bool to determine the format of generated files.

Word.WdExportFormat wd = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
Excel.XlFixedFormatType excelType = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
PowerPoint.PpSaveAsFileType ppType = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;

In this method, I use ExportAsFixedFormat for Word and Excel and SaveAs for Powerpoint.

Actually, the SaveAs method can support several formats. Therefore, we can also use SaveAs for Word. However, it cannot be used for Excel.

Convert Word to PDF

private bool Convert(string sourcePath, string targetPath, Word.WdExportFormat exportFormat)
{
    bool result;
    object paramMissing = Type.Missing;
    Word.ApplicationClass wordApplication = new Word.ApplicationClass();
    Word.Document wordDocument = null;
    try
    {
         object paramSourceDocPath = sourcePath;
         string paramExportFilePath = targetPath;
         Word.WdExportFormat paramExportFormat = exportFormat;
         bool paramOpenAfterExport = false;
         Word.WdExportOptimizeFor paramExportOptimizeFor =
         Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
         Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
          int paramStartPage = 0;
          int paramEndPage = 0;
          Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
          bool paramIncludeDocProps = true;
          bool paramKeepIRM = true;
          Word.WdExportCreateBookmarks paramCreateBookmarks =
          Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
          bool paramDocStructureTags = true;
          bool paramBitmapMissingFonts = true;
          bool paramUseISO19005_1 = false;
          wordDocument = wordApplication.Documents.Open(
                                 ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                                 ref paramMissing, ref paramMissing, ref paramMissing,
                                 ref paramMissing, ref paramMissing, ref paramMissing,
                                 ref paramMissing, ref paramMissing, ref paramMissing,
                                 ref paramMissing, ref paramMissing, ref paramMissing,
                                 ref paramMissing);

          if (wordDocument != null)
              wordDocument.ExportAsFixedFormat(paramExportFilePath,
              paramExportFormat, paramOpenAfterExport,
              paramExportOptimizeFor, paramExportRange, paramStartPage,
              paramEndPage, paramExportItem, paramIncludeDocProps,
              paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
              paramBitmapMissingFonts, paramUseISO19005_1,
              ref paramMissing);
              result = true;
    }
    finally
    {
        if (wordDocument != null)
        {
            wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
            wordDocument = null;
        }
        if (wordApplication != null)
        {
            wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
            wordApplication = null;
        }
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
    return result;
}

Convert Excel to PDF

private bool Convert(string sourcePath, string targetPath, XlFixedFormatType targetType)
{
    bool result;
    object missing = Type.Missing;
    ApplicationClass application = null;
    Workbook workBook = null;
    try
    {
        application = new ApplicationClass();
        object target = targetPath;
        object type = targetType;
        workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
                           missing, missing, missing, missing, missing, missing, missing, missing, missing);
        workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
        result = true;
    }
    catch
    {
        result = false;
    }
    finally
    {
        if (workBook != null)
        {
            workBook.Close(true, missing, missing);
            workBook = null;
        }
        if (application != null)
        {
            application.Quit();
            application = null;
        }
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
    return result;

}

By the way, I found a Free PDF Converter which can convert office files to PDF. That Free PDF Converter is also created via C#.

Convert PowerPoint to PDF

private bool Convert(string sourcePath, string targetPath, PpSaveAsFileType targetFileType)
{
        bool result;
        object missing = Type.Missing;
        ApplicationClass application = null;
        Presentation persentation = null;
        try
        {
                application = new ApplicationClass();
                persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
                persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);

                result = true;
        }
        catch
        {
                result = false;
        }
        finally
        {
                if (persentation != null)
                {
                        persentation.Close();
                        persentation = null;
                }
                if (application != null)
                {
                        application.Quit();
                        application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
        }
        return result;
}

If we want to convert Word or Excel to XPS, we can use XPS virtual printer. After installing .Net Framework 3.5, the XPS virtual printer will be installed by default. And we set it as the default printer.

Microsoft XPS Document Writer

Word to XPS

public void PrintWord(string wordfile)
{
        oWord.ApplicationClass word = new oWord.ApplicationClass();
        Type wordType = word.GetType();

        //Open Word Document

        oWord.Documents docs = word.Documents;
        Type docsType = docs.GetType();
        object objDocName = wordfile;
        oWord.Document doc = (oWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true });

        //Print and Export to Specified File
        //Use doc.PrintOut(); method. But it is complicated to call the parameters. Suggest to use Type.InvokeMember. We just set four main parameters to call PrintOut.

        Type docType = doc.GetType();
        object printFileName = wordfile + ".xps";
        docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, oWord.WdPrintOutRange.wdPrintAllDocument, printFileName });

        //Quit Word
        wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
}

Excel to XPS

public void PrintExcel(string execlfile)
{
        Excel.ApplicationClass eapp = new Excel.ApplicationClass();
        Type eType = eapp.GetType();
        Excel.Workbooks Ewb = eapp.Workbooks;
        Type elType = Ewb.GetType();
        object objelName = execlfile;
        Excel.Workbook ebook = (Excel.Workbook)elType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, Ewb, new Object[] { objelName, true, true });

        object printFileName = execlfile + ".xps";

        Object oMissing = System.Reflection.Missing.Value;
        ebook.PrintOut(oMissing, oMissing, oMissing, oMissing, oMissing, true, oMissing, printFileName);

        eType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, eapp, null);
}

In the same manner, if a PDF virtual printer is installed on the computer, for example, 5D PDF, we can set it as the default printer to convert other formats to PDF.

Also, we can put the methods in a Windows Service to do background conversion of documents.


Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles