In This Topic
Programming / PDF / Adding an image to a newly created PDF document

Adding an image to a newly created PDF document

In This Topic

Adding images to PDF documents requires adding an image to the PDF as a resource and then you can draw the image onto a page using that resource. Of course, you can do both in one step by setting the DrawImage parameter to True/true in the AddImageFromGdPictureImage() method.

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

Dim oGdPicturePDF As New GdPicturePDF()

Dim oGdPictureImaging As New GdPictureImaging()

'Creating a new empty PDF document.

If oGdPicturePDF.NewPDF() = GdPictureStatus.OK Then

    'Just to remind you that units are set to points and the origin is set to bottom left by default.

    'Loading an image from a file.

    Dim imageID As Integer = oGdPictureImaging.CreateGdPictureImageFromFile("input.tif")

    If oGdPictureImaging.GetStat() = GdPictureStatus.OK Then

        'Adding a new empty page into the PDF document.

        oGdPicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4)

        'Getting the page width and height.

        Dim pageWidth As Single = oGdPicturePDF.GetPageWidth()

        Dim pageHeight As Single = oGdPicturePDF.GetPageHeight()

        'Adding an image as a resource into the PDF document - we decided not to draw the image in this moment.

        Dim imageResName As String = oGdPicturePDF.AddImageFromGdPictureImage(imageID, False, False)

        'Drawing the image resource onto the current page and saving the PDF document.

        If (oGdPicturePDF.GetStat() <> GdPictureStatus.OK) OrElse

           (oGdPicturePDF.DrawImage(imageResName, pageWidth / 3, pageHeight / 3, pageWidth / 3, pageHeight / 3) <> GdPictureStatus.OK) OrElse

           (oGdPicturePDF.SaveToFile("output.pdf") <> GdPictureStatus.OK) Then

            MessageBox.Show("The example has NOT been followed successfully. Error: " + oGdPicturePDF.GetStat().ToString(), "Adding an image Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End If

        'Releasing the image.

        oGdPictureImaging.ReleaseGdPictureImage(imageID)

    Else

        MessageBox.Show("The image can't be loaded. Error: " + oGdPictureImaging.GetStat().ToString(), "Adding an image Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

    End If

    oGdPicturePDF.CloseDocument()

Else

    MessageBox.Show("The new PDF document can't be created. Error: " + oGdPicturePDF.GetStat().ToString(), "Adding an image Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

End If

oGdPictureImaging.Dispose()

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

GdPicturePDF oGdPicturePDF = new GdPicturePDF();

GdPictureImaging oGdPictureImaging = new GdPictureImaging();

//Creating a new empty PDF document.

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

{

   //Just to remind you that units are set to points and the origin is set to bottom left by default.

   //Loading an image from a file.

   int imageID = oGdPictureImaging.CreateGdPictureImageFromFile("input.tif");

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

    {

        //Adding a new empty page into the PDF document.

        oGdPicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4);

        //Getting the page width and height.

        float pageWidth = oGdPicturePDF.GetPageWidth();

        float pageHeight = oGdPicturePDF.GetPageHeight();

        //Adding an image as a resource into the PDF document - we decided not to draw the image in this moment.

        string imageResName = oGdPicturePDF.AddImageFromGdPictureImage(imageID, false, false);

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

            //Drawing the image resource onto the current page.

            (oGdPicturePDF.DrawImage(imageResName, pageWidth / 3, pageHeight / 3, pageWidth / 3, pageHeight / 3) != GdPictureStatus.OK) ||

            //Saving the PDF document.

            (oGdPicturePDF.SaveToFile("output.pdf") != GdPictureStatus.OK))

        {

            MessageBox.Show("The example has NOT been followed successfully. Error: " + oGdPicturePDF.GetStat().ToString(), "Adding an image Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        //Releasing the image.

        oGdPictureImaging.ReleaseGdPictureImage(imageID);

    }

    else

    {

        MessageBox.Show("The image can't be loaded. Error: " + oGdPictureImaging.GetStat().ToString(), "Adding an image Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    oGdPicturePDF.CloseDocument();

}

else

{

    MessageBox.Show("The new PDF document can't be created. Error: " + oGdPicturePDF.GetStat().ToString(), "Adding an image Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

oGdPictureImaging.Dispose();

oGdPicturePDF.Dispose();