In This Topic
Programming / PDF / Splitting PDF documents by number of pages

Splitting PDF documents by number of pages

In This Topic

In this example, we will see how to split a multipage PDF document into separate PDF documents that will contain the same number of pages. You can also examine how to optimize the file size of resulting PDF documents by reducing redundant resources and using packing option when saving documents.

Copy Code
'We assume that GdPicture has been correctly installed and unlocked.

Dim src_pdf As New GdPicturePDF()

Dim dest_pdf As New GdPicturePDF()

If (src_pdf.LoadFromFile("source.pdf", False) = GdPictureStatus.OK) AndAlso

    (dest_pdf.NewPDF() = GdPictureStatus.OK) Then

    Dim filename As String = "dest", filetype As String = ".pdf"

    Dim pageCount As Integer = src_pdf.GetPageCount()

    If src_pdf.GetStat() = GdPictureStatus.OK Then

        Dim quotient As Integer = 0, remainder As Integer = 0

        For i As Integer = 1 To pageCount

            If dest_pdf.ClonePage(src_pdf, i) = GdPictureStatus.OK Then

                quotient = Math.DivRem(i, 3, remainder)

                If remainder = 0 Then

                    If (dest_pdf.RemoveUnusedResources() <> GdPictureStatus.OK) OrElse

                       (dest_pdf.SaveToFile(filename + quotient.ToString() + filetype, True) <> GdPictureStatus.OK) OrElse

                       (dest_pdf.CloseDocument() <> GdPictureStatus.OK) OrElse

                       (dest_pdf.NewPDF() <> GdPictureStatus.OK) Then

                        MessageBox.Show("Error occurred when handling destination file. Status: " + dest_pdf.GetStat().ToString(), "Document Splitter", MessageBoxButtons.OK, MessageBoxIcon.Error)

                        Exit For

                    End If

                End If

            Else

                MessageBox.Show("Error occurred when cloning the page. Status: " + dest_pdf.GetStat().ToString(), "Document Splitter", MessageBoxButtons.OK, MessageBoxIcon.Error)

                Exit For

            End If

        Next

        If remainder <> 0 Then

            If (dest_pdf.RemoveUnusedResources() = GdPictureStatus.OK) AndAlso

               (dest_pdf.SaveToFile(filename + (quotient + 1).ToString() + filetype, True) = GdPictureStatus.OK) AndAlso

               (dest_pdf.CloseDocument() = GdPictureStatus.OK) Then

                MessageBox.Show("Done!", "Document Splitter", MessageBoxButtons.OK, MessageBoxIcon.Information)

            Else

                MessageBox.Show("Error occurred when saving the last destination file. Status: " + dest_pdf.GetStat().ToString(), "Document Splitter", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else

            MessageBox.Show("Error occurred when cloning the page. Status: " + dest_pdf.GetStat().ToString(), "Document Splitter", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End If

    Else

        MessageBox.Show("Error occurred in GetPageCount() method. Status: " + src_pdf.GetStat().ToString(), "Document Splitter", MessageBoxButtons.OK, MessageBoxIcon.Error)

    End If

Else

    MessageBox.Show("The documents can't be loaded or created.", "Document Splitter", MessageBoxButtons.OK, MessageBoxIcon.Error)

End If

src_pdf.Dispose()

dest_pdf.Dispose()
Copy Code
//We assume that GdPicture has been correctly installed and unlocked.

GdPicturePDF src_pdf = new GdPicturePDF();

GdPicturePDF dest_pdf = new GdPicturePDF();

if ((src_pdf.LoadFromFile("source.pdf", false) == GdPictureStatus.OK) &&

    (dest_pdf.NewPDF() == GdPictureStatus.OK))

{

    string filename = "dest", filetype = ".pdf";

    int pageCount = src_pdf.GetPageCount();

    if (src_pdf.GetStat() == GdPictureStatus.OK)

    {

        int quotient = 0, remainder = 0;

        for (int i = 1; i <= pageCount; i++)

        {

            if (dest_pdf.ClonePage(src_pdf, i) == GdPictureStatus.OK)

            {

                quotient = Math.DivRem(i, 3, out remainder);

                if (remainder == 0)

                {

                    if ((dest_pdf.RemoveUnusedResources() != GdPictureStatus.OK) ||

                        (dest_pdf.SaveToFile(filename + quotient.ToString() + filetype, true) != GdPictureStatus.OK) ||

                        (dest_pdf.CloseDocument() != GdPictureStatus.OK) ||

                        (dest_pdf.NewPDF() != GdPictureStatus.OK))

                    {

                        MessageBox.Show("Error occurred when handling destination file. Status: " + dest_pdf.GetStat().ToString(), "Document Splitter", MessageBoxButtons.OK, MessageBoxIcon.Error);

                        break;

                    }

                }

            }

            else

            {

                MessageBox.Show("Error occurred when cloning the page. Status: " + dest_pdf.GetStat().ToString(), "Document Splitter", MessageBoxButtons.OK, MessageBoxIcon.Error);

                break;

            }

        }

        if (remainder != 0)

        {

            if ((dest_pdf.RemoveUnusedResources() == GdPictureStatus.OK) &&

                (dest_pdf.SaveToFile(filename + (quotient + 1).ToString() + filetype, true) == GdPictureStatus.OK) &&

                (dest_pdf.CloseDocument() == GdPictureStatus.OK))

            {

                MessageBox.Show("Done!", "Document Splitter", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

            else

            {

                MessageBox.Show("Error occurred when saving the last destination file. Status: " + dest_pdf.GetStat().ToString(), "Document Splitter", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

        }

        else

            MessageBox.Show("Error occurred when cloning the page. Status: " + dest_pdf.GetStat().ToString(), "Document Splitter", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    else

        MessageBox.Show("Error occurred in GetPageCount() method. Status: " + src_pdf.GetStat().ToString(), "Document Splitter", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

else

    MessageBox.Show("The documents can't be loaded or created.", "Document Splitter", MessageBoxButtons.OK, MessageBoxIcon.Error);

src_pdf.Dispose();

dest_pdf.Dispose();