Attach a File to a PDF in C#
PSPDFKit GdPicture.NET Library enables you to attach a file to a PDF in the form of an annotation.
To attach a file to a PDF, follow the steps below.
-
Create a
GdPicturePDF
object. -
Specify the origin of the coordinate system with the
SetOrigin
method. It requires thePdfOrigin
enumeration, which allows the following values:-
PdfOriginTopLeft
-
PdfOriginTopRight
-
PdfOriginBottomRight
-
PdfOriginBottomLeft
-
-
Set the measurement unit used to specify the dimensions of the annotation. The
SetMeasurementUnit
method takes a member of thePdfMeasurementUnit
enumeration as an argument. It allows the following values:-
PdfMeasurementUnitCentimeter
-
PdfMeasurementUnitMillimeter
-
PdfMeasurementUnitInch
-
PdfMeasurementUnitPoint
-
PdfMeasurementUnitUndefined
-
-
Open the file you want to attach in the form of a byte array.
The file you want to attach must be a binary file.
-
Select the PDF page where you want to add the annotation using the
SelectPage
method. -
Use the
AddFileAttachmentAnnot
method. It accepts the following parameters:-
Left
— X coordinate of the top-left corner. -
Top
— Y coordinate of the top-left corner. -
Width
— Width of the annotation icon. -
Height
— Height of the annotation icon. -
Data
— The content of the file associated with the annotation object, which is expressed as a binary value. -
FileName
— Name of the attached file. -
Title
— Title of the annotation. -
Description
— Annotation description. -
color:
-
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
— 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
— Icon type displayed. This uses thePdfFileAttachmentAnnotIcon
enumeration. The possible values are the following:-
None
-
Graph
-
Tag
-
Paperclip
-
PushPin
-
-
To add an image 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.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 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 image file. int annotID = gdpicturePDF.AddFileAttachmentAnnot(Left: 5, Top: 5, Width: 2, Height: 4, data, filename, "Attachment", "Attachment for review", Color.DarkBlue, 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 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 image file. Dim annotID As Integer = gdpicturePDF.AddFileAttachmentAnnot(Left:=5, Top:=5, Width:=2, Height:=4, data, filename, "Attachment", "Attachment for review", Color.DarkBlue, 0.75F, PdfFileAttachmentAnnotIcon.Paperclip) gdpicturePDF.SaveToFile("C:\temp\output.pdf") End Using