Save file as PDF/A using Office.Interop.Excel

淺唱寂寞╮ 提交于 2021-02-07 09:20:31

问题


How can I export an Excel spreadsheet to PDF/A (ISO 19005-1)?

EDIT: I'm asking for PDF/A, and not plain old PDF 1.5 as it exports by default. I've even emphasized the A in my original question.

I can already export Word and PowerPoint documents to PDF/A, using the ExportAsFixedFormat() function, since the Word and PowerPoint functions both have an optional UseISO19005_1 parameter, but the Excel version is very different, and is missing lots of parameters.

I can't seem to find any way to export a PDF/A using the COM Interop.

Here's the code I use to export from a docx:

Dim ExportFormat As WdExportFormat = WdExportFormat.wdExportFormatPDF
Dim OpenAfterExport As Boolean = False
Dim OptimizeFor As WdExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint
Dim Range As WdExportRange = WdExportRange.wdExportAllDocument
Dim Item As WdExportItem = WdExportItem.wdExportDocumentWithMarkup
Dim IncludeDocProps As Boolean = True
Dim KeepIRM As Boolean = False
Dim CreateBookmarks As WdExportCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks
Dim DocStructureTags As Boolean = True
Dim BitmapMissingFonts As Boolean = True
Dim UseISO19005_1 As Boolean = False

If exportPDFA Then
    UseISO19005_1 = True
    Dim wordApp As New Word.Application()
    Dim doc As Word.Document = wordApp.Documents.Open(FileName)
    doc.ExportAsFixedFormat(pathToDestFile, ExportFormat, OpenAfterExport, OptimizeFor, Range, 0, 0, Item, IncludeDocProps, KeepIRM, CreateBookmarks, DocStructureTags, BitmapMissingFonts, UseISO19005_1)
End If

But for xlsx, the ExportAsFixedFormat() function accepts very different parameters (this was taken directly from the Microsoft.Office.Interop.Excel class):

Sub ExportAsFixedFormat(Type As XlFixedFormatType, Optional Filename As Object = Nothing, Optional Quality As Object = Nothing, Optional IncludeDocProperties As Object = Nothing, Optional IgnorePrintAreas As Object = Nothing, Optional From As Object = Nothing, Optional [To] As Object = Nothing, Optional OpenAfterPublish As Object = Nothing, Optional FixedFormatExtClassPtr As Object = Nothing)

回答1:


Excel allows the user to choose whether or not to save the file as PDF/A in the options of the save dialog. This setting is stored in the Registry as LastISO19005-1 (REG_DWORD) under HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Common\FixedFormat. (Replace 12.0 with the right version.) The ExportAsFixedFormat function also respects this setting. You can set this value to 1 before calling ExportAsFixedFormat to get Excel to export the file as PDF/A.

I know this solution is not pretty as it uses global state to address a local problem. Consider restoring the previous value when you're done.




回答2:


Use below method for converting the excel to pdf and mark as answer if it works

Public Function ExportWorkbookToPdf(ByVal workbookPath As String, ByVal outputPath As String) As Boolean

    ' If either required string is null or empty, stop and bail out
    If String.IsNullOrEmpty(workbookPath) OrElse String.IsNullOrEmpty(outputPath) Then
        Return False
    End If

    ' Create COM Objects
    Dim excelApplication As Microsoft.Office.Interop.Excel.Application
    Dim excelWorkbook As Microsoft.Office.Interop.Excel.Workbook

    ' Create new instance of Excel
    excelApplication = New Microsoft.Office.Interop.Excel.Application()

    ' Make the process invisible to the user
    excelApplication.ScreenUpdating = False

    ' Make the process silent
    excelApplication.DisplayAlerts = False

    ' Open the workbook that you wish to export to PDF
    excelWorkbook = excelApplication.Workbooks.Open(workbookPath)

    ' If the workbook failed to open, stop, clean up, and bail out
    If excelWorkbook Is Nothing Then
        excelApplication.Quit()

        excelApplication = Nothing
        excelWorkbook = Nothing

        Return False
    End If

    Dim exportSuccessful = True
    Try
        ' Call Excel's native export function (valid in Office 2007 and Office 2010, AFAIK)
        excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, outputPath)
    Catch ex As System.Exception
        ' Mark the export as failed for the return value...

        ' Do something with any exceptions here, if you wish...
        ' MessageBox.Show...        
        exportSuccessful = False
    Finally
        ' Close the workbook, quit the Excel, and clean up regardless of the results...
        excelWorkbook.Close()
        excelApplication.Quit()

        excelApplication = Nothing
        excelWorkbook = Nothing
    End Try

    ' You can use the following method to automatically open the PDF after export if you wish
    ' Make sure that the file actually exists first...
    If System.IO.File.Exists(outputPath) Then
        System.Diagnostics.Process.Start(outputPath)
    End If

    Return exportSuccessful
End Function


来源:https://stackoverflow.com/questions/44409918/save-file-as-pdf-a-using-office-interop-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!