VB.Net Merge multiple pdfs into one and export

后端 未结 4 531
小蘑菇
小蘑菇 2020-12-11 11:13

I have to merge multiple PDFs into a single PDF.

I am using the iText.sharp library, and collect converted the code and tried to use it (from here) The actual code

4条回答
  •  误落风尘
    2020-12-11 11:29

    the code that was marked correct does not close all the file streams therefore the files stay open within the app and you wont be able to delete unused PDFs within your project

    This is a better solution:

    Public Sub MergePDFFiles(ByVal outPutPDF As String) 
    
        Dim StartPath As String = FileArray(0) ' this is a List Array declared Globally
        Dim document = New Document()
        Dim outFile = Path.Combine(outPutPDF)' The outPutPDF varable is passed from another sub this is the output path
        Dim writer = New PdfCopy(document, New FileStream(outFile, FileMode.Create))
    
        Try
    
            document.Open()
            For Each fileName As String In FileArray
    
                Dim reader = New PdfReader(Path.Combine(StartPath, fileName))
    
                For i As Integer = 1 To reader.NumberOfPages
    
                    Dim page = writer.GetImportedPage(reader, i)
                    writer.AddPage(page)
    
                Next i
    
                reader.Close()
    
            Next
    
            writer.Close()
            document.Close()
    
        Catch ex As Exception
            'catch a Exception if needed
    
        Finally
    
            writer.Close()
            document.Close()
    
        End Try
    
    
    End Sub
    

提交回复
热议问题