簡體   English   中英

使用 Office.Interop.Excel 將文件另存為 PDF/A

[英]Save file as PDF/A using Office.Interop.Excel

如何將Excel電子表格導出為 PDF/ A (ISO 19005-1)?

編輯:我要求 PDF/A,而不是普通的舊 PDF 1.5,因為它默認導出。 我什至在我原來的問題中強調了A。

我已經可以使用ExportAsFixedFormat()函數將 Word 和 PowerPoint 文檔導出為 PDF/A,因為 Word 和 PowerPoint 函數都有一個可選的UseISO19005_1參數,但 Excel 版本非常不同,並且缺少很多參數。

我似乎找不到任何使用 COM Interop 導出 PDF/ 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

但是對於 xlsx, ExportAsFixedFormat()函數接受非常不同的參數(這直接取自 Microsoft.Office.Interop.Excel 類):

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)

Excel 允許用戶在保存對話框的選項中選擇是否將文件保存為 PDF/A。 此設置作為 LastISO19005-1 (REG_DWORD) 存儲在注冊表中的 HKEY_CURRENT_USER\\Software\\Microsoft\\Office\\12.0\\Common\\FixedFormat 下。 (用正確的版本替換 12.0。)ExportAsFixedFormat 函數也遵守此設置。 您可以在調用 ExportAsFixedFormat 之前將此值設置為 1,以使 Excel 將文件導出為 PDF/A。

我知道這個解決方案並不漂亮,因為它使用全局狀態來解決局部問題。 完成后考慮恢復以前的值。

使用以下方法將 excel 轉換為 pdf 並標記為答案(如果有效)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM