Export Excel workbook as a pdf in .NET

By Miguel Santos

This FAQ provides code to save Excel workbook as a pdf programmatically. This code is useful for any VSTO or Excel automation implementation.

Here is the source code in C#

         public void ExportWorkbook(Excel.Application app, string pathToExport)
            Excel.Workbook lActiveBook = app.ActiveWorkbook;
                pathToExport, Excel.XlFixedFormatQuality.xlQualityStandard,
               Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

Here is the source code in VB.NET

    Private Sub ExportWorkbook(ByVal app As Excel.Application, ByVal pathToExport As String)
        Dim lActiveBook As Excel.Workbook = app.ActiveWorkbook
        lActiveBook.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, pathToExport, Excel.XlFixedFormatQuality.xlQualityStandard)
     End Sub

You can use this code in any Excel automation such as VSTO Add-In or workbook.

Note: Notice that the above code saves normal pdf. If you require PDF/A format, you need to change the following registry key:
DWORD  - LastISO19005-1
or change the Excel 2007 Options to ISO19005-1 compliant PDF/A when saving a pdf the first time so that subsequent save operations will use the same option.

Export Excel workbook as a pdf in .NET  (4774 Views)