How can I split and merge PDF files from VB.NET program language?

You can split and merge PDF files from VB.NET program easily, "pdfpage.dll" is a Win32 DLL Library, you can call it from your VB.NET code directly, the sample code like below,

 

Imports System.IO

Imports System.Data

Public Class Form1

 

    Private Declare Function VeryAppendPDF Lib "pdfpage.dll" (ByVal ptrExistingPDFName As String, ByVal ptrAppendPDF As String) As Integer

    Private Declare Function VerySplitMergePDFEx Lib "pdfpage.dll" (ByVal lpInputPDF As String, ByVal lpPageRange As String, ByVal lpOutPDF As String) As Integer

    Private Declare Function VeryGetPDFFilePageCount Lib "pdfpage.dll" (ByVal lpInputPDF As String) As Integer

    Private Declare Function VerySplitMergeSetParameter Lib "pdfpage.dll" (ByVal key As String, ByVal value As String) As Integer

    Private Declare Function VerySplitMergePDF Lib "pdfpage.dll" (ByVal strCmd As String) As Integer

    Private Declare Function PDF_MergePDFFiles Lib "pdfpage.dll" (ByVal strPDFFiles As String, ByVal strOutPDF As String) As Integer

    Private Declare Sub PDF_SetCode Lib "pdfpage.dll" (ByVal strRegcode As String)

 

    Dim gsBronbestand As String

    Dim gsMergebestand() As String

 

    Private Sub btnVerwerken_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVerwerken.Click

        Dim iRet As Integer

        Dim iPageCount As Integer

        Dim sPath As String

        Dim k As Short

 

        sPath = gsBronbestand.Substring(0, InStrRev(gsBronbestand, "\"))

        If File.Exists(sPath & "temp.pdf") Then File.Delete(sPath & "temp.pdf")

        File.Copy(gsBronbestand, sPath & "temp.pdf")

 

        PDF_SetCode("XXXXXXXXXXXXXXXXXXXX")

        iRet = VerySplitMergeSetParameter("RetainBookmarks", "off")

        iRet = VerySplitMergeSetParameter("RetainAcroForms", "on")

 

        For k = 0 To UBound(gsMergebestand)

            iRet = VeryAppendPDF(sPath & "temp.pdf", gsMergebestand(k))

        Next

 

        iPageCount = VeryGetPDFFilePageCount(sPath & "temp.pdf")

        lblverwerken.Text = "Gereed. " & iPageCount & " pagina's verwerkt."

 

    End Sub

    Private Sub btnZoek1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZoek1.Click

        OpenFileDialog1.InitialDirectory = ""

        OpenFileDialog1.Filter = "PDF bestand (*.pdf)|*.pdf"

        OpenFileDialog1.Multiselect = False

        OpenFileDialog1.FilterIndex = 2

        OpenFileDialog1.RestoreDirectory = True

testlus:

        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

            If OpenFileDialog1.FileName <> "" Then

                gsBronbestand = OpenFileDialog1.FileName

                lblZoek1.Text = gsBronbestand

            Else

                MsgBox("Er is nog geen correct bestand geselecteerd.", MsgBoxStyle.Critical)

                OpenFileDialog1.ShowDialog()

                GoTo testlus

            End If

            Me.btnZoek1.Enabled = False

            Me.btnZoek2.Enabled = True

            Me.btnZoek2.Focus()

        End If

    End Sub

    Private Sub btnZoek2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZoek2.Click

        OpenFileDialog1.InitialDirectory = ""

        OpenFileDialog1.Filter = "PDF bestand (*.pdf)|*.pdf"

        OpenFileDialog1.Multiselect = True

        OpenFileDialog1.FilterIndex = 2

        OpenFileDialog1.RestoreDirectory = True

testlus:

        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

            If OpenFileDialog1.FileName <> "" Then

                gsMergebestand = OpenFileDialog1.FileNames

                lblZoek2.Text = OpenFileDialog1.FileName

            Else

                MsgBox("There is not a correct file is selected.", MsgBoxStyle.Critical)

                OpenFileDialog1.ShowDialog()

                GoTo testlus

            End If

            Me.btnZoek1.Enabled = False

            Me.btnZoek2.Enabled = False

            Me.btnVerwerken.Enabled = True

            Me.btnVerwerken.Focus()

        End If

    End Sub

 

    Private Sub BatchSelectPDFFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BatchSelectPDFFiles.Click

        OpenFileDialog1.InitialDirectory = ""

        OpenFileDialog1.Filter = "PDF bestand (*.pdf)|*.pdf"

        OpenFileDialog1.Multiselect = True

        OpenFileDialog1.FilterIndex = 2

        OpenFileDialog1.RestoreDirectory = True

testlus:

        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

            If OpenFileDialog1.FileName <> "" Then

                gsMergebestand = OpenFileDialog1.FileNames

            Else

                MsgBox("There is not a correct file is selected.", MsgBoxStyle.Critical)

                OpenFileDialog1.ShowDialog()

                GoTo testlus

            End If

        End If

    End Sub

 

    Private Sub BatchMergePDFs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BatchMergePDFs.Click

        Dim sPath As String = gsMergebestand(0).Substring(0, InStrRev(gsMergebestand(0), "\"))

        Dim scommand As String = ""

        Dim tstart As DateTime

        Dim tstop As DateTime

 

        'pdfmerge

        Dim iRet As Integer

        Dim iPageCount As Integer

        Dim k As Short

 

 

        If File.Exists(sPath & "output.pdf") Then File.Delete(sPath & "output.pdf")

 

        PDF_SetCode("XXXXXXXXXXXXXXXXXXXX")

        iRet = VerySplitMergeSetParameter("RetainBookmarks", "off")

        iRet = VerySplitMergeSetParameter("RetainAcroForms", "on")

 

        Dim sw As New StreamWriter(sPath & "filelist.txt", False)

        For k = 0 To UBound(gsMergebestand)

            sw.WriteLine(gsMergebestand(k))

        Next

        sw.Close()

 

        'scommand = Chr(34) & "C:\Program Files\VeryPDF PDF Split-Merge v3.0\pdfpg.exe" & Chr(34) & " " & Chr(34) & sPath & "filelist.txt" & Chr(34) & " " & Chr(34) & sPath & "output.pdf" & Chr(34)

        'Shell(scommand, AppWinStyle.NormalFocus, True)

 

        tstart = Now

 

        'This line has to many arguments

        Dim strCmd As String = """" & sPath & "filelist.txt"" """ & sPath & "output.pdf"""

        iRet = VerySplitMergePDF(strCmd)

 

        iPageCount = VeryGetPDFFilePageCount(sPath & "output.pdf")

 

        tstop = Now

 

        lblverwerken.Text = "Ready " & iPageCount & " Pages processed. " & DateAndTime.DateDiff(DateInterval.Second, tstart, tstop) & " seconds."

        My.Application.DoEvents()

    End Sub

End Class

 

 

Please notice, you need compile your VB.NET code to an EXE file first, place this EXE and pdfpage.dll, pdftoolsdk.dll files into same folder, run your EXE file from this folder, your EXEwill able to load pdfpage.dll and split and merge PDF files correctly.

VN:F [1.9.20_1166]
Rating: 9.3/10 (3 votes cast)
VN:F [1.9.20_1166]
Rating: +2 (from 2 votes)
How can I split and merge PDF files from VB.NET program language?, 9.3 out of 10 based on 3 ratings

Related Posts

This entry was posted in PDF Split-Merge and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


Verify Code   If you cannot see the CheckCode image,please refresh the page again!