Attach a file to a PDF in C#
Nutrient .NET SDK (formerly GdPicture.NET) 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
GdPicturePDFobject. - Specify the origin of the coordinate system with the
SetOriginmethod. It requires thePdfOriginenumeration, which accepts the following values:
PdfOriginTopLeftPdfOriginTopRightPdfOriginBottomRightPdfOriginBottomLeft
Set the measurement unit used to specify the dimensions of the annotation. The
SetMeasurementUnitmethod takes a member of thePdfMeasurementUnitenumeration as an argument. It accepts the following values:PdfMeasurementUnitCentimeterPdfMeasurementUnitMillimeterPdfMeasurementUnitInchPdfMeasurementUnitPointPdfMeasurementUnitUndefined
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
SelectPagemethod.Use the
AddFileAttachmentAnnotmethod. 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 (from0to255).Cyan,Magenta,Yellow,Black— CMYK values (from0to255).Colorobject — For more information, refer to the color object guide.
Opacity— Opacity value of the annotation, where0means full transparency and1means fully visible. Use thefsuffix to convert the double value to float (0.75f).AnnotIcon— Icon type displayed. This uses thePdfFileAttachmentAnnotIconenumeration. The possible values are the following:NoneGraphTagPaperclipPushPin
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 UsingUsed Methods
Related Topics