使用 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 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)
使用以下方法将 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
Excel 允许用户在保存对话框的选项中选择是否将文件另存为 PDF/A。此设置在 HKEY_CURRENT_USER\Software\Microsoft\Office.0\Common\FixedFormat 下作为 LastISO19005-1 (REG_DWORD) 存储在注册表中。 (将 12.0 替换为正确的版本。)ExportAsFixedFormat 函数也遵守此设置。您可以在调用 ExportAsFixedFormat 以获取 Excel 将文件导出为 PDF/A.
之前将此值设置为 1
我知道这个解决方案并不完美,因为它使用全局状态来解决局部问题。完成后考虑恢复之前的值。
如何将 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 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)
使用以下方法将 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
Excel 允许用户在保存对话框的选项中选择是否将文件另存为 PDF/A。此设置在 HKEY_CURRENT_USER\Software\Microsoft\Office.0\Common\FixedFormat 下作为 LastISO19005-1 (REG_DWORD) 存储在注册表中。 (将 12.0 替换为正确的版本。)ExportAsFixedFormat 函数也遵守此设置。您可以在调用 ExportAsFixedFormat 以获取 Excel 将文件导出为 PDF/A.
之前将此值设置为 1我知道这个解决方案并不完美,因为它使用全局状态来解决局部问题。完成后考虑恢复之前的值。