MSExcel_SaveAsPDF

Microsoft provides add-ins to the Office products to support saving into PDF or XPS file formats. These add-ins include an API to perform this as well.

The add-ins may be found at:
Save as PDF: http://www.microsoft.com/downloads/details.aspx?FamilyId=F1FC413C-6D89-4F15-991B-63B07BA5F2E5&displaylang=en
Save as XPS: http://www.microsoft.com/downloads/details.aspx?FamilyId=BCE8F991-F0A4-47A0-866B-2FD84A329E02&displaylang=en
Save as PDF or XPS (single package): http://www.microsoft.com/downloads/details.aspx?FamilyId=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en

Once installed, add a reference to the Microsoft.Office.Interop.[product]
Note: multiple product versions are available as references: version 12 = Office 2007, version 11 = Office 2003
Language:
C#
Keywords:
Office
Code Snippet

using System;
using System.IO;
using MSExcel = Microsoft.Office.Interop.Excel;

/***
* must install: addin for SaveAsPDF for Office 2007
* works with .xls and .xlsx files
* must add COM references for Excel
***/

public void MakeExcelPDF(string SourceDoc, string DestDoc)
{
    //for all missing types.
    object Unknown = Type.Missing;

    MSExcel.
Application newExcelAppn = new Microsoft.Office.Interop.Excel.Application();
    MSExcel.
Workbook newbook = null;

    string
src_xls = SourceDoc;
    object des_xls = DestDoc;

    newbook = newExcelAppn.Workbooks.Open(src_xls,
              Unknown, Unknown, Unknown, Unknown,
              Unknown, Unknown, Unknown, Unknown,
              Unknown, Unknown, Unknown, Unknown,
              Unknown, Unknown);

    //specify the format.
    //      XLFixedFormatType paramExportFormat = MSExcel.XLFixedFormatType.xlTypeXPS;
    MSExcel.XlFixedFormatType paramExportFormat = MSExcel.XlFixedFormatType.xlTypePDF;

    // Save it in the target format.
    newbook.ExportAsFixedFormat(paramExportFormat, des_xls,
                                MSExcel.
XlFixedFormatQuality.xlQualityStandard,
                                true, true, Unknown, Unknown, false, Unknown);

    newExcelAppn.Quit();
}


Created 2012-02-11
comments powered by Disqus
Login