In This Topic
Programming / Color Detection / Converting a multipage PDF document to a multipage TIFF file using Color Detection

Converting a multipage PDF document to a multipage TIFF file using Color Detection

In This Topic

This sample code shows how to convert an existing PDF document to a multipage TIFF file using the Color Detection plugin to produce the best quality output while keeping the output file size as low as possible.

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

Const  INPUT_DOCUMENT As String = "input.pdf"

Const  OUTPUT_DOCUMENT As String = "output.tif"

Const  DPI As Single = 200F

Dim oGdPicturePDF As New GdPicturePDF()

Dim oGdPictureImaging As New GdPictureImaging() 

If oGdPicturePDF.LoadFromFile(INPUT_DOCUMENT, False) = GdPictureStatus.OK Then

    Dim tiffID As Integer = 0

    For i As Integer = 0 To oGdPicturePDF.GetPageCount() - 1

        oGdPicturePDF.SelectPage(i + 1)

        'Rasterizing a page or extracting a page bitmap.

        Dim rasterPage As Integer = oGdPicturePDF.RenderPageToGdPictureImageEx(DPI, True, System.Drawing.Imaging.PixelFormat.Format24bppRgb)

        If oGdPicturePDF.GetStat() = GdPictureStatus.OK Then

            'Defining the compression of the tiff page. Let's use CCITT4 for bitonal pages and JPEG for others.

            Dim tiffCompression As TiffCompression

            'Applying color detection and color depth automatic conversion.

            oGdPictureImaging.ColorDetection(rasterPage, True, False, False)

            If oGdPictureImaging.GetBitDepth(rasterPage) = 1 Then

                tiffCompression = TiffCompression.TiffCompressionCCITT4

            Else

                tiffCompression = TiffCompression.TiffCompressionJPEG

            End If

            If i = 0 Then

                If oGdPictureImaging.TiffSaveAsMultiPageFile(rasterPage, OUTPUT_DOCUMENT, tiffCompression) = GdPictureStatus.OK Then

                    'Firstly rasterized page's GdPicture ID becomes the multipage tiff id.

                    tiffID = rasterPage

                Else

                    MessageBox.Show("Error occurred when saving the file. Status: " + oGdPictureImaging.GetStat().ToString(), "Color Detection Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

                    Exit For

                End If

            Else

                If oGdPictureImaging.TiffAddToMultiPageFile(tiffID, rasterPage, tiffCompression) <> GdPictureStatus.OK Then

                    MessageBox.Show("Error occurred when adding a page. Status: " + oGdPictureImaging.GetStat().ToString(), "Color Detection Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

                    Exit For

                End If

                oGdPictureImaging.ReleaseGdPictureImage(rasterPage)

            End If

        Else

            MessageBox.Show("Error occurred when rendering the page. Status: " + oGdPicturePDF.GetStat().ToString(), "Color Detection Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End If

    Next

    oGdPictureImaging.TiffCloseMultiPageFile(tiffID)

    oGdPictureImaging.ReleaseGdPictureImage(tiffID)

Else

    MessageBox.Show("The file can't be opened. Status: " + oGdPicturePDF.GetStat().ToString(), "Color Detection Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

End If

oGdPicturePDF.Dispose()

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

const string INPUT_DOCUMENT = "input.pdf";

const string OUTPUT_DOCUMENT = "output.tif";

const float DPI = 200F;

GdPicturePDF oGdPicturePDF = new GdPicturePDF();

GdPictureImaging oGdPictureImaging = new GdPictureImaging(); 

if (oGdPicturePDF.LoadFromFile(INPUT_DOCUMENT, false) == GdPictureStatus.OK)

{

    int tiffID = 0;

    for (int i = 0; i < oGdPicturePDF.GetPageCount(); i++)

    {

        oGdPicturePDF.SelectPage(i + 1);

        //Rasterizing a page or extracting a page bitmap.

        int rasterPage = oGdPicturePDF.RenderPageToGdPictureImageEx(DPI, true, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

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

        {

            //Defining the compression of the tiff page. Let's use CCITT4 for bitonal pages and JPEG for others.

            TiffCompression tiffCompression;

            //Applying color detection and color depth automatic conversion.

            oGdPictureImaging.ColorDetection(rasterPage, true, false, false);

            if (oGdPictureImaging.GetBitDepth(rasterPage) == 1)

            {

                tiffCompression = TiffCompression.TiffCompressionCCITT4;

            }

            else

            {

                tiffCompression = TiffCompression.TiffCompressionJPEG;

            }

            if (i == 0)

            {

                if (oGdPictureImaging.TiffSaveAsMultiPageFile(rasterPage, OUTPUT_DOCUMENT, tiffCompression) == GdPictureStatus.OK)

                {

                    //Firstly rasterized page's GdPicture ID becomes the multipage tiff id.

                    tiffID = rasterPage;

                }

                else

                {

                    MessageBox.Show("Error occurred when saving the file. Status: " + oGdPictureImaging.GetStat().ToString(), "Color Detection Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    break;

                }

            }

            else

            {

                if (oGdPictureImaging.TiffAddToMultiPageFile(tiffID, rasterPage, tiffCompression) != GdPictureStatus.OK)

                {

                    MessageBox.Show("Error occurred when adding a page. Status: " + oGdPictureImaging.GetStat().ToString(), "Color Detection Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    break;

                }

                oGdPictureImaging.ReleaseGdPictureImage(rasterPage);

            }

        }

        else

        {

            MessageBox.Show("Error occurred when rendering the page. Status: " + oGdPicturePDF.GetStat().ToString(), "Color Detection Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

    }

    oGdPictureImaging.TiffCloseMultiPageFile(tiffID);

    oGdPictureImaging.ReleaseGdPictureImage(tiffID);

}

else

{

    MessageBox.Show("The file can't be opened. Status: " + oGdPicturePDF.GetStat().ToString(), "Color Detection Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

oGdPicturePDF.Dispose();

oGdPictureImaging.Dispose();