Add PDF Attachments on .NET

Adding attachment is one of the distinctive features for PDF files. PDF allows people to add different file formats as attachments such as doc/docx, xls/xlsx, txt, jpg, png, etc. This section will introduce a solution to quickly add attachments in PDF and also create PDF attachments annotations in C#, VB.NET via a .NET PDF component.

Spire.PDF for .NET, implementing rich capabilities to manipulate PDF files, enables you to add attachments in PDF as well as remove these attachments from PDF in C#, VB.NET. In the solution, two methods are mainly called to perform the task of adding attachments. The first method is byte[] ReadAllBytes(string path) which is applied to load the certain file that you want to attach in PDF. The other method is PdfAttachmentAnnotation(RectangleF rectangle, string fileName, byte[] data) that is used to add PDF attachment with attachment annotation.

Please download Spire.PDF for .NET and see the picture below:

Add PDF Attachments

Detail Code:

  //attach an image
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Bold));
            PointF location = new PointF(0, y);
            String label = "Sales Report Chart";
            byte[] data = File.ReadAllBytes(@"..\SalesReportChart.png");
            SizeF size = font2.MeasureString(label);
            RectangleF bounds = new RectangleF(location, size);
            page.Canvas.DrawString(label, font2, PdfBrushes.DarkOrange, bounds);
            bounds = new RectangleF(bounds.Right + 3, bounds.Top, font2.Height / 2, font2.Height);
            PdfAttachmentAnnotation annotation1
                = new PdfAttachmentAnnotation(bounds, "SalesReportChart.png", data);
            annotation1.Color = Color.Teal;
            annotation1.Flags = PdfAnnotationFlags.ReadOnly;
            annotation1.Icon = PdfAttachmentIcon.Graph;
            annotation1.Text = "Sales Report Chart";
            (page as PdfNewPage).Annotations.Add(annotation1);
            y = y + size.Height + 2;
'attach an image
Dim font2 As New PdfTrueTypeFont(New Font("Arial", 12F, FontStyle.Bold))
Dim location As New PointF(0, y)
Dim label As [String] = "Sales Report Chart"
Dim data As Byte() = File.ReadAllBytes("..\SalesReportChart.png")
Dim size As SizeF = font2.MeasureString(label)
Dim bounds As New RectangleF(location, size)
page.Canvas.DrawString(label, font2, PdfBrushes.DarkOrange, bounds)
bounds = New RectangleF(bounds.Right + 3, bounds.Top, font2.Height / 2, font2.Height)
Dim annotation1 As New PdfAttachmentAnnotation(bounds, "SalesReportChart.png", data)
annotation1.Color = Color.Teal
annotation1.Flags = PdfAnnotationFlags.[ReadOnly]
annotation1.Icon = PdfAttachmentIcon.Graph
annotation1.Text = "Sales Report Chart"
TryCast(page, PdfNewPage).Annotations.Add(annotation1)
y = y + size.Height + 2

Spire.PDF for .NET is an independent PDF component which allows you to perform a wide range of editing tasks directly in your .NET applications. Know more about this .NET PDF component.