Attach a file to an annotation in C#
Attach a File
To add a file in a form of a PDF annotation, follow these steps:
- Create a
GdPicturePDF
object. - Load the PDF file with the
LoadFromFile
method. - Set the origin of the coordinate system with the
SetOrigin
method. This method requires a member of thePDFOrigin
enumeration. - Set the measurement unit with the
SetMeasurementUnit
method to specify the annotation’s dimensions and position. This method uses a member of thePdfMeasurementUnit
enumeration. - Select the PDF page to place the annotation on using the
SelectPage
method. - Load the file to a byte array.
- Add the annotation with the attached file using the
AddFileAttachmentAnnot
method. 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 file associated with the annotation object, which is expressed as a binary array.FileName
— The full path to the attached file.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 (from0
to255
).Cyan
,Magenta
,Yellow
,Black
— CMYK values (from0
to255
).Color
object — For more information, refer to the color object guide.
Opacity
— The opacity value of the annotation, where0
means full transparency and1
means fully visible. Use thef
suffix to convert the double value to float (0.75f
).AnnotIcon
— The icon type displayed. It uses thePdfFileAttachmentAnnotIcon
enumeration. The possible values are the following:None
Graph
Tag
Paperclip
PushPin
- Save the PDF document to a file with the
SaveToFile
method.
To add a file as an annotation to the last page of a PDF, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();string filename = @"C:\temp\source.txt";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 file as a binary array.byte[] data = File.ReadAllBytes(filename);// Select the page to attach the file to.gdpicturePDF.SelectPage(gdpicturePDF.GetPageCount());// Add an annotation with the file.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.txt" 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 file as a binary array. Dim data = File.ReadAllBytes(filename) ' Select the page to attach the file to. gdpicturePDF.SelectPage(gdpicturePDF.GetPageCount()) ' Add an annotation with the file. 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 Using
Used methods
Related topics