Attach an image to an annotation in C#
Attach an Image
To add an image in a form of a PDF annotation, follow these steps:
- Create a
GdPicturePDFobject. - Load the PDF file with the
LoadFromFilemethod. - Set the origin of the coordinate system with the
SetOriginmethod. This method requires a member of thePDFOriginenumeration. - Set the measurement unit with the
SetMeasurementUnitmethod to specify the annotation’s dimensions and position. This method uses a member of thePdfMeasurementUnitenumeration. - Select the PDF page to place the annotation on using the
SelectPagemethod. - Load the image to a byte array.
- Add the annotation with the attached image using the
AddFileAttachmentAnnotmethod. This method uses the following parameters:Left— The X coordinate of the top-left corner.Top— The Y coordinate of the top-left corner.Width— The width of the annotation icon.Height— The height of the annotation icon.Data— The content of the image associated with the annotation object, which is expressed as a binary array.FileName— The full path to the attached image.Title— The title of the newly added annotation object. By convention, this represents the author of the annotation.Description— The description of the file associated with the annotation object.- The annotation icon’s color expressed in one of the following ways:
Red,Green,Blue— RGB values (from0to255).Cyan,Magenta,Yellow,Black— CMYK values (from0to255).Colorobject — For more information, refer to the color object guide.
Opacity— The opacity value of the annotation, where0means full transparency and1means fully visible. Use thefsuffix to convert the double value to float (0.75f).AnnotIcon— The icon type displayed. It uses thePdfFileAttachmentAnnotIconenumeration. The possible values are the following:NoneGraphTagPaperclipPushPin
- Save the PDF document to a file with the
SaveToFilemethod.
To add an image as an annotation to the last page of a PDF, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();string filename = @"C:\temp\source.jpg";gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");// Set the origin of the coordinate system to the bottom-left corner.gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomLeft);// Set the measurement unit to centimeters.gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);// Load the image as a binary array.byte[] data = File.ReadAllBytes(filename);// Select the page to attach the image to.gdpicturePDF.SelectPage(gdpicturePDF.GetPageCount());// Add an annotation with the image.gdpicturePDF.AddFileAttachmentAnnot(Left: 5, Top: 5, Width: 2, Height: 4, data, filename, "Nutrient", "Attachment for review", 173, 216, 230, 0.75f, PdfFileAttachmentAnnotIcon.Paperclip);gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() Dim filename = "C:\temp\source.jpg" gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Set the origin of the coordinate system to the bottom-left corner. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomLeft) ' Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter) ' Load the image as a binary array. Dim data = File.ReadAllBytes(filename) ' Select the page to attach the image to. gdpicturePDF.SelectPage(gdpicturePDF.GetPageCount()) ' Add an annotation with the image. gdpicturePDF.AddFileAttachmentAnnot(Left:=5, Top:=5, Width:=2, Height:=4, data, filename, "Nutrient", "Attachment for review", 173, 216, 230, 0.75F, PdfFileAttachmentAnnotIcon.Paperclip) gdpicturePDF.SaveToFile("C:\temp\output.pdf")End UsingUsed methods
Related topics