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:

  1. Create a GdPicturePDF object.
  2. Load the PDF file with the LoadFromFile method.
  3. Set the origin of the coordinate system with the SetOrigin method. This method requires a member of the PDFOrigin enumeration.
  4. Set the measurement unit with the SetMeasurementUnit method to specify the annotation’s dimensions and position. This method uses a member of the PdfMeasurementUnit enumeration.
  5. Select the PDF page to place the annotation on using the SelectPage method.
  6. Load the file to a byte array.
  7. 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 (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 — The 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 — The icon type displayed. It uses the PdfFileAttachmentAnnotIcon enumeration. The possible values are the following:
      • None
      • Graph
      • Tag
      • Paperclip
      • PushPin
  8. 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");