Add an attachment to PDF

<< Click to Display Table of Contents >>

Navigation:  Gnostice Document Studio .NET > Going Deeper > Document Engines > Working with PDF > Enhancement >

Add an attachment to PDF

In this section we present example code to add a file attachment to an existing PDF document. Before proceeding further please make sure that the prerequisite steps as listed in the Document Engines parent topic are followed.

 

Given below is the sample code.

 

// Path to the original PDF document
var originalPdfPath = @"input.pdf";
// Path to the file to be attached
var attachmentPath = @"Validation Report.html";
 
using (var pdfDoc = new PDF())
{
   pdfDoc.LoadDocument(originalPdfPath);
   pdfDoc.MeasurementUnit = MeasurementUnit.Inches;
 
   // Create a file attachment annotation
   var fileAttachmentAnnot = new PDFFileAttachmentAnnot(x: 1f, y: 1f, attachmentPath, icon: PDFFileAttachmentIcon.Paperclip)
   {
      Color = Color.YellowGreen,
      Rectangle = new SimpleRect(1f, 1f, 0.5f, 0.5f),
   };
   // Add the annotation
   pdfDoc.AddAnnot(fileAttachmentAnnot);
 
   // Save the output document
   pdfDoc.Save(@"output.pdf");
}