How to Use the ClonePage method

Discussions about PDF management.
Post Reply
JNewhouse
Posts: 48
Joined: Thu Nov 15, 2007 9:37 pm

How to Use the ClonePage method

Post by JNewhouse » Fri Aug 05, 2011 5:44 am

Hi Loic,
Can you provide a code snippet of how to use the ClonePage PDF method for the case where i have one 10-page pdf file and i want to split it into two-five page pdf files? Pages 1-5 in the first pdf, pages 6-10 in the second pdf.

specifically, what i'm trying to understand is the placement of the copied page. does it always go to the end of the new pdf? or do we have to define it somehow??

Thanks as always!
Jean-Claude

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: How to Use the ClonePage method

Post by Loïc » Fri Aug 05, 2011 9:15 pm

Hi Jean-Claude,

Please have a look on the PDF Splitter demo that came with the GdPicture.NET package.

Here a simple copy of the main form content of this demo app:

Code: Select all

   Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
      Dim sImagepath As String = TextBox2.Text
      Dim SplitEach As Integer = 0
      Dim OutputFolder As String = TextBox3.Text
      Dim Status As GdPicture.GdPictureStatus = GdPictureStatus.OK
      Dim oGdPicturePDFSrc As New GdPicturePDF

      Button3.Enabled = False

        oGdPicturePDFSrc.SetLicenseNumber("XXX") 'Replace XXX by a valid commercial or demo key; You can obtain a demo key from http://www.evaluation-gdpicture.com

      Try 'prevent for overflow exception
         SplitEach = CInt(Val(TextBox1.Text))
      Catch ex As Exception

      End Try

      If SplitEach > 0 Then

         If oGdPicturePDFSrc.LoadFromFile(sImagepath, False) = GdPictureStatus.OK Then

            'Check for encryption
            If oGdPicturePDFSrc.IsEncrypted() Then
               If oGdPicturePDFSrc.SetPassword("") = False Then
                  If oGdPicturePDFSrc.SetPassword(InputBox("Password: ", "Password protected document")) = False Then
                     MsgBox("Can not uncrypt document")
                     oGdPicturePDFSrc.CloseDocument()
                     Return
                  End If
               End If
            End If

            'Checking if the document is  multipage 
            Dim PageCount As Integer = oGdPicturePDFSrc.GetPageCount
            If PageCount > 1 Then
               'Getting the initial compression sheme
               Dim CurrentPage As Integer = 0
               Dim OutputFilePath As String
               Dim OutputFileCount As Integer = CInt(Math.Ceiling(PageCount / SplitEach))

               ProgressBar1.Maximum = PageCount

               For i As Integer = 1 To OutputFileCount
                  Dim oGdPicturePDFDest As New GdPicturePDF
                  oGdPicturePDFDest.NewPDF()
                  OutputFilePath = OutputFolder + "split" + Str(i) + ".pdf"

                  For j As Integer = 1 To SplitEach
                     CurrentPage = CurrentPage + 1
                     If CurrentPage <= PageCount And Status = GdPictureStatus.OK Then
                        oGdPicturePDFDest.ClonePage(oGdPicturePDFSrc, CurrentPage)
                        ProgressBar1.Value = CurrentPage
                        Application.DoEvents()
                     End If
                  Next
                  oGdPicturePDFDest.SaveToFile(OutputFilePath)
                  oGdPicturePDFDest.CloseDocument()
               Next

               If Status = GdPictureStatus.OK Then
                  MsgBox("Success !")
               Else
                  MsgBox("Error: " + Status.ToString)
               End If
            Else
               MsgBox("Error, The PDF is not a multipage document.")
            End If

            oGdPicturePDFSrc.CloseDocument()
         Else
            MsgBox("Error, Can't open document: " + sImagepath)
         End If
      Else
         MsgBox("Error, Incorrect value for split each.")
      End If
      Button3.Enabled = True
   End Sub
Kind regards,

Loïc

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests