使用Office.Interop.Excel将文件另存为PDF/A

3
我该如何将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)

你能分享一下你用来将Word转换成PDF的代码吗?这将有助于从Excel制作PDF。 - Gulam Husain Ansari
感谢代码,请使用我在答案中提供的解决方案代码。 - Gulam Husain Ansari
2个回答

5

在保存对话框的选项中,Excel 允许用户选择是否将文件保存为 PDF/A。此设置以 LastISO19005-1(REG_DWORD) 的形式存储在注册表中,位于 HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Common\FixedFormat 下(请使用正确版本号替换 12.0)。ExportAsFixedFormat 函数也将遵循此设置。在调用 ExportAsFixedFormat 导出文件为 PDF/A 之前,您可以将此值设置为 1。

我知道这个解决方案并不完美,因为它使用全局状态来解决局部问题。完成后,请考虑恢复前一个值。


1
这是唯一有用的答案。就像你所说,它不够美观,但我找不到其他方法了。 - Adrian Ilie

-1
使用以下方法将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

这并没有回答我的问题。它显然可以导出PDF,但我明确要求的是PDF/A。 - Adrian Ilie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接