How to Add Attachments to PDF in WPF

2016-04-01 08:16:37 Written by  support iceblue
Rate this item
(0 votes)

In PDF, an attachment is an additional file that is attached to a PDF document. There are many kinds of attachments, such as a document, an image file or other supported file types.

Spire.PDF, as a professional library, enables us to add various attachments as per our needs without having Adobe Acrobat been installed on system. This article demonstrates how to add attachments - a word document and an image to PDF in WPF applications using Spire.PDF for WPF.

Please see the following effective screenshot after adding attachments:

How to Add Attachments to PDF in WPF

Double click the attachment icon, the attached file will be open automatically.

Detail steps:

Use following namespace:

using System;
using System.Drawing;
using System.IO;
using System.Windows;
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;

Step 1: Load the original PDF document and add a new page to it.

PdfDocument doc = new PdfDocument("Sales Report.pdf");
PdfPageBase page = section.Pages.Add();

Step 2: In the new page, draw a title for the attachments.

float y = 10;
PdfBrush brush1 = PdfBrushes.CornflowerBlue;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, System.Drawing.FontStyle.Bold));
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
page.Canvas.DrawString("Attachments", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);

Step 3: Load the file that needs to be attached using the File.ReadAllBytes(string path) method, then add the file to the specified page of the PDF document as attachment.

Add Word document attachment with attachment annotation:

PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 12f, System.Drawing.FontStyle.Bold));
PointF location = new PointF(50, y);
String label = "Sales Report";
byte[] data = File.ReadAllBytes("Sales Report.docx");
SizeF size = font2.MeasureString(label);
RectangleF bounds = new RectangleF(location, size);
page.Canvas.DrawString(label, font2, PdfBrushes.MediumPurple, bounds);
bounds = new RectangleF(bounds.Right + 3, bounds.Top, font2.Height / 2, font2.Height);
PdfAttachmentAnnotation annotation1 = new PdfAttachmentAnnotation(bounds, "Sales Report.docx", data);
annotation1.Color = Color.Teal;
annotation1.Flags = PdfAnnotationFlags.NoZoom;
annotation1.Icon = PdfAttachmentIcon.Graph;
annotation1.Text = "Sales Report.docx";
(page as PdfNewPage).Annotations.Add(annotation1);

Add image attachment is similar with adding Word attachment, refer to following codes:

location = new PointF(50, y);
label = "Vendors Info";
data = File.ReadAllBytes("Vendors Info.png");
size = font2.MeasureString(label);
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 annotation2 = new PdfAttachmentAnnotation(bounds, "Vendors Info.png", data);
annotation2.Color = Color.Orange;
annotation2.Flags = PdfAnnotationFlags.ReadOnly;
annotation2.Icon = PdfAttachmentIcon.PushPin;
annotation2.Text = "Vendors info image";
(page as PdfNewPage).Annotations.Add(annotation2);

Step 4: Save and launch the file.

doc.SaveToFile("Attachment.pdf");
System.Diagnostics.Process.Start("Attachment.pdf");

Full codes:

private void button1_Click(object sender, RoutedEventArgs e)
{
    //Load the original PDF document.
    PdfDocument doc = new PdfDocument("Sales Report.pdf");
    //Add a new page
    PdfPageBase page = doc.Pages.Add();

    float y = 10;
    //Set title text
    PdfBrush brush1 = PdfBrushes.CornflowerBlue;
    PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f,   System.Drawing.FontStyle.Bold));
    PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
    page.Canvas.DrawString("Attachments", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);
    y = y + font1.MeasureString("Attachments", format1).Height;
    y = y + 5;

    //Add Word document attachment
    PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 12f, System.Drawing.FontStyle.Bold));
    PointF location = new PointF(50, y);
    String label = "Sales Report";
    byte[] data = File.ReadAllBytes("Sales Report.docx");
    SizeF size = font2.MeasureString(label);
    RectangleF bounds = new RectangleF(location, size);
    page.Canvas.DrawString(label, font2, PdfBrushes.MediumPurple, bounds);
    bounds = new RectangleF(bounds.Right + 3, bounds.Top, font2.Height / 2, font2.Height);
    PdfAttachmentAnnotation annotation1 = new PdfAttachmentAnnotation(bounds, "Sales  Report.docx", data);
    annotation1.Color = Color.Teal;
    annotation1.Flags = PdfAnnotationFlags.NoZoom;
    annotation1.Icon = PdfAttachmentIcon.Graph;
    annotation1.Text = "Sales Report.docx";
    (page as PdfNewPage).Annotations.Add(annotation1);
    y = y + size.Height + 2;

    //Add image attachment
    location = new PointF(50, y);
    label = "Vendors Info";
    data = File.ReadAllBytes("Vendors Info.png");
    size = font2.MeasureString(label);
    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 annotation2 = new PdfAttachmentAnnotation(bounds, "Vendors  Info.png", data);
    annotation2.Color = Color.Orange;
    annotation2.Flags = PdfAnnotationFlags.ReadOnly;
    annotation2.Icon = PdfAttachmentIcon.PushPin;
    annotation2.Text = "Vendors info image";
    (page as PdfNewPage).Annotations.Add(annotation2);

    //Save and launch the file.
    doc.SaveToFile("Attachment.pdf");
    System.Diagnostics.Process.Start("Attachment.pdf");
}

Additional Info

  • tutorial_title: Add Attachments to PDF in WPF
Last modified on Friday, 24 September 2021 09:55