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.

  1. Create a GdPicturePDF object.

  2. Specify the origin of the coordinate system with the SetOrigin method. It requires the PdfOrigin enumeration, which allows the following values:

    • PdfOriginTopLeft

    • PdfOriginTopRight

    • PdfOriginBottomRight

    • PdfOriginBottomLeft

  3. Set the measurement unit used to specify the dimensions of the annotation. The SetMeasurementUnit method takes a member of the PdfMeasurementUnit enumeration as an argument. It allows the following values:

    • PdfMeasurementUnitCentimeter

    • PdfMeasurementUnitMillimeter

    • PdfMeasurementUnitInch

    • PdfMeasurementUnitPoint

    • PdfMeasurementUnitUndefined

  4. Open the file you want to attach in the form of a byte array.

Information

The file you want to attach must be a binary file.

  1. Select the PDF page where you want to add the annotation using the SelectPage method.

  2. 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 (from 0 to 255).

      • Cyan, Magenta, Yellow, Black — CMYK values (from 0 to 255).

      • Color object — For more information, refer to the color object guide.

    • Opacity — Opacity value of the annotation, where 0 means full transparency and 1 means fully visible. Use the f suffix to convert the double value to float (0.75f).

    • AnnotIcon — Icon type displayed. This uses the PdfFileAttachmentAnnotIcon 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
Used Methods

Related Topics