Add an image to a PDF using C#
PDF
An image can be added to a PDF file in two ways:
Drawing an image to a PDF page
To draw an image as a part of a PDF page, follow the steps outlined below.
- Load the PDF file.
- Add the specified image as a resource by using one of the following methods:
AddJpegImageFromFile, which is where you specify the path to the image.AddJpegImageFromStream, with which you specify the stream containing an image.
- Set the origin of the coordinate system with the
SetOriginmethod. This method requires thePdfOriginenumeration, which accepts the following values:PdfOriginTopLeftPdfOriginTopRightPdfOriginBottomRightPdfOriginBottomLeft
- Set the dimension units with the
SetMeasurementUnitmethod, which uses thePdfMeasurementUnitenumeration as an argument. It allows the following values:PdfMeasurementUnitCentimeterPdfMeasurementUnitMillimeterPdfMeasurementUnitInchPdfMeasurementUnitPointPdfMeasurementUnitUndefined
- Select the PDF page you want to add the image to with the
SelectPagemethod. - Draw the image to the PDF page with the
DrawImagemethod. This method requires the following parameters:
ImageResName— Image resource name.DstX— Horizontal distance of the bottom-left corner of the PDF file, where the image is drawn.DstY— Vertical distance of the bottom-left corner of the PDF file, where the image is drawn.Width— Width of the drawn image resource.Height— Height of the drawn image resource.
To add an image to the first and the last PDF page, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");// Add an image as a resource.string image = gdpicturePDF.AddJpegImageFromFile(@"C:\temp\source.jpg");// Set the origin of the coordinate system to the top-left corner.gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft);// Set the measurement units to millimeters.gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter);var width = gdpicturePDF.GetPageWidth();// Select the first page of the PDF file.gdpicturePDF.SelectPage(1);// Draw the image to the top-center of the PDF page.gdpicturePDF.DrawImage(image, (width - 10) / 2, 15, 10, 10);// Select the last page of the PDF file.gdpicturePDF.SelectPage(gdpicturePDF.GetPageCount());// Change the origin of the coordinate system to the bottom-left corner.gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomLeft);// Draw the image to the bottom-center of the PDF page.gdpicturePDF.DrawImage(image, (width - 10) / 2, 5, 10, 10);gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Add an image as a resource. Dim image As String = gdpicturePDF.AddJpegImageFromFile("C:\temp\source.jpg") ' Set the origin of the coordinate system to the top-left corner. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft) ' Set the measurement units to millimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter) Dim width = gdpicturePDF.GetPageWidth() ' Select the first page of the PDF file. gdpicturePDF.SelectPage(1) ' Draw the image to the top-center of the PDF page. gdpicturePDF.DrawImage(image, (width - 10) / 2, 15, 10, 10) ' Select the last page of the PDF file. gdpicturePDF.SelectPage(gdpicturePDF.GetPageCount()) ' Change the origin of the coordinate system to the bottom-left corner. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomLeft) ' Draw the image to the bottom-center of the PDF page. gdpicturePDF.DrawImage(image, (width - 10) / 2, 5, 10, 10) gdpicturePDF.SaveToFile("C:\temp\output.pdf")End UsingUsed methods
Related topics
Adding an image as a separate PDF page
To add an image as a separate PDF page at the end of a file, follow the steps outlined below.
- Load the PDF file.
- Create a GdPicture image from the specified file using the
CreateGdPictureImageFromFilemethod. - Insert the image as a separate page at the end of the file with one of the following methods:
Both methods, AddImageFromGdPictureImage and AddImageFromBitmap, require the following parameters:
- Either
imageIDorbitmap. ImageMask— Indicates whether the inserted image is treated as an image mask (or stencil mask). The recommended default value isfalse. Applicable only for 1 bit per pixel images.DrawImage— Set this parameter totrue, which adds a new page, and the image is drawn on its whole surface.
To add an image as a separate page, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();using GdPictureImaging gdpictureImaging = new GdPictureImaging();gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");// Add an image as a resource.int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");// Add the image to a new page.gdpicturePDF.AddImageFromGdPictureImage(imageID, false, true);gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");gdpictureImaging.ReleaseGdPictureImage(imageID);Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Add an image as a resource. Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Add the image to a new page. gdpicturePDF.AddImageFromGdPictureImage(imageID, False, True) gdpicturePDF.SaveToFile("C:\temp\output.pdf") gdpictureImaging.ReleaseGdPictureImage(imageID)End UsingEnd UsingUsed methods
Related topics