Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42305

Spliting and merging pages with iText#

$
0
0
Hi, I'm working on a project but I'm stuck, this is what I need to do: I need to select a folder, next, all PDF files that are in that folder need to be printed, but only the first page of them.
I was trying to solve this by taking the first page of every PDF, and merge it into a new one using iTextSharp (a free pdf editor for VB.NET, you can download it here).

Unfortunately I'm not really familiar with iText# so is there anyone who could help me out? It'd be great if you could look into to this and help me out a bit. Thanks in advance!

This is my current code, I'm also having problems filling in the array with PDF files:

Code:

Imports System.IO
Imports iTextSharp
Imports iTextSharp.text
Imports iTextSharp.text.pdf

Public Class Form1
   

    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        ' declaring variables
        Dim MyFolderBrowser As New System.Windows.Forms.FolderBrowserDialog
        Dim intAantal As Integer

        ' text with instruction
        MyFolderBrowser.Description = "Select the folder"

        ' no new folders should be able to be made
        MyFolderBrowser.ShowNewFolderButton = False

        ' checking if a folder is selected
        Dim dlgResult As DialogResult
        dlgResult = MyFolderBrowser.ShowDialog()

        If dlgResult = Windows.Forms.DialogResult.OK Then
            txtUitvoer.Text = MyFolderBrowser.SelectedPath

            ' checking the extension (pdf)
            Const cstrExtension As String = "pdf"

            Dim arrBestanden() As String = System.IO.Directory.GetFiles(MyFolderBrowser.SelectedPath, "*.pdf")
            Dim intAantalBestanden As Integer = arrBestanden.Length
            Dim arPDFS(intAantalBestanden - 1) As String
            Dim pdfDoc As iTextSharp.text.Document = Nothing    'the output pdf document

            For Each foundfile In arrBestanden

                Dim strExtensie As String = System.IO.Path.GetExtension(foundfile)

                If strExtensie.ToLower = "." & cstrExtension Or strExtensie.ToLower = cstrExtension Then


                    ' searching for a pdf

                    Dim Proc As New System.Diagnostics.Process
                    Dim ProcPrint As New System.Diagnostics.Process

                    Proc.StartInfo.WorkingDirectory = MyFolderBrowser.SelectedPath
                    ProcPrint.StartInfo.WorkingDirectory = MyFolderBrowser.SelectedPath

                    arPDFS(intAantal) = foundfile
                    intAantal += 1

                    MergePdfFilesWithBookmarks(arPDFS, pdfDoc.ToString)

                End If
            Next

        End If

    End Sub

    Public Function MergePdfFilesWithBookmarks(ByVal sourcePdfs() As String, ByVal outputPdf As String) As Boolean
        Dim result As Boolean = False
        Dim pdfCount As Integer = 0    'total input pdf file count
        Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
        Dim pdfDoc As iTextSharp.text.Document = Nothing    'the output pdf document
        Dim page As iTextSharp.text.pdf.PdfImportedPage = Nothing
        Dim pdfCpy As iTextSharp.text.pdf.PdfCopy = Nothing
        Dim pageCount As Integer = 0    'number of pages in the current pdf
        Dim totalPages As Integer = 0  'number of pages so far in the merged pdf

        ' Must have more than 1 source pdf's to merge
        If sourcePdfs.Length > 1 Then

            Try

                For i As Integer = 0 To sourcePdfs.GetUpperBound(0)
                    reader = New iTextSharp.text.pdf.PdfReader(sourcePdfs(i))
                    reader.ConsolidateNamedDestinations()
                    pageCount = reader.NumberOfPages

                    If i = 0 Then
                        pdfDoc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1))
                        pdfCpy = New iTextSharp.text.pdf.PdfCopy(pdfDoc, New System.IO.FileStream(outputPdf, System.IO.FileMode.Create))
                        pdfDoc.Open()
                        totalPages = pageCount
                    Else

                        totalPages += pageCount
                    End If

                    For n As Integer = 1 To pageCount
                        page = pdfCpy.GetImportedPage(reader, n)
                        pdfCpy.AddPage(page)
                    Next

                    reader.Close()

                Next

                pdfDoc.Close()
                result = True

            Catch ex As Exception

                Throw New ApplicationException(ex.Message, ex)

            End Try

        End If

        Return result
    End Function

    Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click

        Me.Close()

    End Sub

End Class


Viewing all articles
Browse latest Browse all 42305

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>