In This Topic
Programming / PDF / Creating an image from a page located in a PDF document (rasterization)

Creating an image from a page located in a PDF document (rasterization)

In This Topic

In this tutorial, we will see how easy it is to create an image from a PDF document's page.

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

Dim oGdPictureImaging As New GdPictureImaging()

Dim oGdPicturePDF As New GdPicturePDF()

'Loading the PDF document from a file.

Dim status As GdPictureStatus = oGdPicturePDF.LoadFromFile("input.pdf", False)

'Checking if the PDF has been correctly loaded.

If status <> GdPictureStatus.OK Then

    MessageBox.Show("The PDF document can't be loaded. Error: " + status.ToString(), "Rasterization Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

Else

    'Selecting the fifth page of the PDF.

    oGdPicturePDF.SelectPage(5)

    'Rendering the selected page to an image.

    Dim imageId As Integer = oGdPicturePDF.RenderPageToGdPictureImage(200, True)

    'Checking if the image has been rendered successfully.

    If oGdPicturePDF.GetStat() = GdPictureStatus.OK Then

        'Saving the image to a file.

        If oGdPictureImaging.SaveAsBMP(imageId, "input_page_5.bmp") <> GdPictureStatus.OK Then

            MessageBox.Show("Error occurred when saving the image. Error: " + oGdPicturePDF.GetStat().ToString(), "Rasterization Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Else

            MessageBox.Show("Done!", "Rasterization Example", MessageBoxButtons.OK, MessageBoxIcon.Information)

        End If

        oGdPictureImaging.ReleaseGdPictureImage(imageId)

    Else

        MessageBox.Show("Error occurred when rendering the page to an image. Error: " + oGdPicturePDF.GetStat().ToString(), "Rasterization Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

    End If

End If

oGdPictureImaging.Dispose()

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

GdPictureImaging oGdPictureImaging = new GdPictureImaging();

GdPicturePDF oGdPicturePDF = new GdPicturePDF();

//Loading the PDF document from a file.

GdPictureStatus status = oGdPicturePDF.LoadFromFile("input.pdf", false);

//Checking if the PDF has been correctly loaded.

if (status != GdPictureStatus.OK)

{

    MessageBox.Show("The PDF document can't be loaded. Error: " + status.ToString(), "Rasterization Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

else

{

    //Selecting the fifth page of the PDF.

    oGdPicturePDF.SelectPage(5);

    //Rendering the selected page to an image.

    int imageId = oGdPicturePDF.RenderPageToGdPictureImage(200, true);

    //Checking if the image has been rendered successfully.

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

    {

        //Saving the image to a file.

        if (oGdPictureImaging.SaveAsBMP(imageId, "input_page_5.bmp") != GdPictureStatus.OK)

        {

            MessageBox.Show("Error occurred when saving the image. Error: " + oGdPicturePDF.GetStat().ToString(), "Rasterization Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        else

            MessageBox.Show("Done!", "Rasterization Example", MessageBoxButtons.OK, MessageBoxIcon.Information);

        oGdPictureImaging.ReleaseGdPictureImage(imageId);

    }

    else

    {

        MessageBox.Show("Error occurred when rendering the page to an image. Error: " + oGdPicturePDF.GetStat().ToString(), "Rasterization Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

}

oGdPictureImaging.Dispose();

oGdPicturePDF.Dispose();