locked
How to convert Excel files to PDF using VB RRS feed

  • Question

  • User1805789888 posted

    Hi all,

    is it possible to convert Excel files into PDF using Visual Basic 2008?

    I have an application that produces Excel files and I'd like to save them in PDF.

    Thank you

    Thursday, January 10, 2013 8:57 AM

Answers

  • User-751615625 posted

    I can provide you two solutions, the first solution is that you can first merge or copy excel files to one excel file sheet and then, convert excel file to PDF format by below code:

    Merge excel files into one excel sheet:

    Dim dest As New Workbook()
    Dim result As Worksheet = dest.CreateEmptySheet("copied sheet")
    'load the first workbook
    Dim workbook As New Workbook()
    workbook.LoadFromFile("D:\michelle\my file\excel comment.xlsx", ExcelVersion.Version2010)
    'load the second workbook
    Dim workbook2 As New Workbook()
    workbook2.LoadFromFile("D:\michelle\my file\excel comment1.xlsx", ExcelVersion.Version2010)
    'load the third workbook
    Dim workbook3 As New Workbook()
    workbook3.LoadFromFile("D:\michelle\my file\excel comment2.xlsx", ExcelVersion.Version2010)
    Dim sheetDest As Worksheet = workbook.Worksheets(0)
    Dim sourceRange As CellRange = workbook2.Worksheets(0).AllocatedRange
    Dim souce2 As CellRange = workbook3.Worksheets(0).AllocatedRange
    sheetDest.Copy(sourceRange, sheetDest, sheetDest.AllocatedRange.RowCount, 1, False)
    sheetDest.Copy(souce2, sheetDest, sheetDest.AllocatedRange.RowCount + sourceRange.RowCount, 1, True)
    workbook.SaveToFile("test.xlsx", ExcelVersion.Version2010)

    Then convert excel file to PDF by below code:

                           ' load Excel file
    			Dim workbook As New Workbook()
    			workbook.LoadFromFile("D:\test.xlsx")
    
    			' Set PDF template
    			Dim pdfDocument As New PdfDocument()
    			pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape
    			pdfDocument.PageSettings.Width = 970
    			pdfDocument.PageSettings.Height = 850
    
    			'Convert Excel to PDF using the template above
    			Dim pdfConverter As New PdfConverter(workbook)
    			Dim settings As New PdfConverterSettings()
    			settings.TemplateDocument = pdfDocument
    			pdfDocument = pdfConverter.Convert(settings)
    
    			' Save and preview PDF
    			pdfDocument.SaveToFile("sample.pdf")
    			System.Diagnostics.Process.Start("sample.pdf")


    In realizing the above code, I use a .NET Excel component, it can convert excel to PDF file without any third party library. In the downloaded Bin folder, there are both xls and pdf dll, you can add them both without downloading other PDF library.

    The other solution is to convert each excel file to PDF first using above solution and then, merge these PDF files into one PDF file by below code:

     Private Sub button1_Click(sender As Object, e As EventArgs)
    	'pdf document list
    	Dim files As [String]() = New [String]() {"..\PDFmerge0.pdf", "..\ PDFmerge1.pdf", "..\ PDFmerge2.pdf"}
    	'open pdf documents            
    	Dim docs As PdfDocument() = New PdfDocument(files.Length - 1) {}
    	For i As Integer = 0 To files.Length - 1
    		docs(i) = New PdfDocument(files(i))
    	Next
    
    	'append document
    	docs(0).AppendPage(docs(1))
    
    	'import PDF pages
    	Dim i As Integer = 0
    	While i < docs(2).Pages.Count
    		docs(0).InsertPage(docs(2), i)
    		i = i + 2
    	End While
    End Sub

    But certaily when you use the second solution, you may need both an excel library and a PDF library. So I think the first solution is more reasonable or you. Hope it can help you!





    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 16, 2013 12:17 AM

All replies

  • User-751615625 posted

    I can provide you two solutions, the first solution is that you can first merge or copy excel files to one excel file sheet and then, convert excel file to PDF format by below code:

    Merge excel files into one excel sheet:

    Dim dest As New Workbook()
    Dim result As Worksheet = dest.CreateEmptySheet("copied sheet")
    'load the first workbook
    Dim workbook As New Workbook()
    workbook.LoadFromFile("D:\michelle\my file\excel comment.xlsx", ExcelVersion.Version2010)
    'load the second workbook
    Dim workbook2 As New Workbook()
    workbook2.LoadFromFile("D:\michelle\my file\excel comment1.xlsx", ExcelVersion.Version2010)
    'load the third workbook
    Dim workbook3 As New Workbook()
    workbook3.LoadFromFile("D:\michelle\my file\excel comment2.xlsx", ExcelVersion.Version2010)
    Dim sheetDest As Worksheet = workbook.Worksheets(0)
    Dim sourceRange As CellRange = workbook2.Worksheets(0).AllocatedRange
    Dim souce2 As CellRange = workbook3.Worksheets(0).AllocatedRange
    sheetDest.Copy(sourceRange, sheetDest, sheetDest.AllocatedRange.RowCount, 1, False)
    sheetDest.Copy(souce2, sheetDest, sheetDest.AllocatedRange.RowCount + sourceRange.RowCount, 1, True)
    workbook.SaveToFile("test.xlsx", ExcelVersion.Version2010)

    Then convert excel file to PDF by below code:

                           ' load Excel file
    			Dim workbook As New Workbook()
    			workbook.LoadFromFile("D:\test.xlsx")
    
    			' Set PDF template
    			Dim pdfDocument As New PdfDocument()
    			pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape
    			pdfDocument.PageSettings.Width = 970
    			pdfDocument.PageSettings.Height = 850
    
    			'Convert Excel to PDF using the template above
    			Dim pdfConverter As New PdfConverter(workbook)
    			Dim settings As New PdfConverterSettings()
    			settings.TemplateDocument = pdfDocument
    			pdfDocument = pdfConverter.Convert(settings)
    
    			' Save and preview PDF
    			pdfDocument.SaveToFile("sample.pdf")
    			System.Diagnostics.Process.Start("sample.pdf")


    In realizing the above code, I use a .NET Excel component, it can convert excel to PDF file without any third party library. In the downloaded Bin folder, there are both xls and pdf dll, you can add them both without downloading other PDF library.

    The other solution is to convert each excel file to PDF first using above solution and then, merge these PDF files into one PDF file by below code:

     Private Sub button1_Click(sender As Object, e As EventArgs)
    	'pdf document list
    	Dim files As [String]() = New [String]() {"..\PDFmerge0.pdf", "..\ PDFmerge1.pdf", "..\ PDFmerge2.pdf"}
    	'open pdf documents            
    	Dim docs As PdfDocument() = New PdfDocument(files.Length - 1) {}
    	For i As Integer = 0 To files.Length - 1
    		docs(i) = New PdfDocument(files(i))
    	Next
    
    	'append document
    	docs(0).AppendPage(docs(1))
    
    	'import PDF pages
    	Dim i As Integer = 0
    	While i < docs(2).Pages.Count
    		docs(0).InsertPage(docs(2), i)
    		i = i + 2
    	End While
    End Sub

    But certaily when you use the second solution, you may need both an excel library and a PDF library. So I think the first solution is more reasonable or you. Hope it can help you!





    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 16, 2013 12:17 AM
  • User-627303614 posted

    Hi

    when I want to convert Excel files to PDF file no edit able

    Normaly when I use any software to convert file from excel to pdf it remain g edit able I want such software which disable editing function

    Saturday, September 14, 2013 11:46 AM