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.
Download sampleTo 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— The horizontal coordinate of the annotation corner closest to the origin.Top— The vertical coordinate of the annotation corner closest to the origin.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, as a byte array.FileName— The name of the attached file.Title— The title of the annotation. By convention, this is the author.Description— The description of the attached file.- The annotation color, in one of the following forms:
Red,Green,Blue— RGB values (from0to255).Cyan,Magenta,Yellow,Black— CMYK values (from0to255).Color— AGdPictureColorobject. For more information, refer to the color object guide.
Opacity— The opacity of the annotation, where0means fully transparent and1means fully visible. Use thefsuffix to convert the double value to float (0.75f).AnnotIcon— The 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:
Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
using GdPicture14;using GdPicture14.Imaging;
LicenseManager license = new LicenseManager();license.RegisterKEY("");using GdPicturePDF gdpicturePDF = new GdPicturePDF();string filename = @"input.jpg";gdpicturePDF.LoadFromFile(@"input.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", 0, 0, 128, 0.75f, PdfFileAttachmentAnnotIcon.Paperclip);gdpicturePDF.SaveToFile(@"output.pdf");Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
Imports System.IOImports GdPicture14Imports GdPicture14.Imaging
Dim license As LicenseManager = New LicenseManager()license.RegisterKEY("")Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() Dim filename = "input.jpg" gdpicturePDF.LoadFromFile("input.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(5, 5, 2, 4, data, filename, "Attachment", "Attachment for review", 0, 0, 128, 0.75F, PdfFileAttachmentAnnotIcon.Paperclip) gdpicturePDF.SaveToFile("output.pdf")End Using