Watch this example on YouTube
This example shows how to:
- save Excel file as PDF
- shrink PDF file
- fit Excel sheet to one page of PDF file. (horizontally and vertically)
Imports System.IO
Imports Microsoft.Office.Interop.Excel
Dim oApplication As ApplicationClass = New ApplicationClass()
Dim oWorkbook As Workbook = Nothing
Dim PDFFile As String = "C:\TEMP\ExcelTest\Test.pdf"
Dim pFormatType As XlFixedFormatType = XlFixedFormatType.xlTypePDF
Dim pQuality As XlFixedFormatQuality = XlFixedFormatQuality.xlQualityMinimum
' xlQualityMinimum == smallest file size
Dim pIncludeDocProperties As Boolean = True
Dim pIgnorePrintAreas As Boolean = True
Dim pFrom As Object = Type.Missing
Dim pTo As Object = Type.Missing
Dim pOpenAfterPublish As Boolean = False
Try
oWorkbook = oApplication.Workbooks.Open("C:\TEMP\ExcelTest\test.xlsx")
' Ensure that it fits to one page
Dim oWorksheet As Excel.Worksheet
oWorksheet = oApplication.Worksheets(1)
oWorksheet.PageSetup.FitToPagesWide = 1
oWorksheet.PageSetup.FitToPagesTall = 1
oWorksheet.PageSetup.Zoom = False
If Not oWorkbook Is Nothing Then
oWorkbook.ExportAsFixedFormat(pFormatType, PDFFile, pQuality, _
pIncludeDocProperties, _
pIgnorePrintAreas, _
pFrom, pTo, pOpenAfterPublish)
End If
Catch ex As Exception
Finally
If Not oWorkbook Is Nothing Then
oWorkbook.Close(False)
oWorkbook = Nothing
End If
If Not oApplication Is Nothing Then
oApplication.Quit()
oApplication = Nothing
End If
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
Thank you! Works like a charm!
ReplyDelete