C# – Add Attachment in PDF Document

Generally speaking, attachment is one related file which is created along with primary file. It also can be necessary files or things sent with other files. People often share their documents by using attachment when sending E-mail or submitting to internet.

In PDF document, we can add useful attachment to be convenient for readers to get it quickly. For example, we can add reference files which we use in documents as attachment. If the attachment is helpful for them as well, they can get it quickly and use it in their own document.

In this post, I will show the method about how to add attachment in PDF with C#.

Before coding, add necessary references. Because I use Spire.PDF to achieve my goal, I add its dll file as reference. Then add system.drawing for color settings.

Steps:

 1. Create a PDF document and set margin to have a good layout.

 2. Add section and page to add contents and attachment titles.

 3. Add document title and set format for it.

 4. Add attachments. The first two attachments I add are two logos which will be set as header and footer picture.

 5. Add other four attachments. Set titles to make readers know what attachments shows. Then, format these titles.

 6. Save and launch file.

Coding:

using System;

using System.Drawing;

using System.IO;

using Spire.Pdf;

using Spire.Pdf.Annotations;

using Spire.Pdf.Attachments;

using Spire.Pdf.Graphics;

namespace Attachment

{

    class Program

    {

        static void Main(string[] args)

        {

            //Create PDF

            PdfDocument doc = new PdfDocument();

 

            //Margin

            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();

            PdfMargins margin = new PdfMargins();

            margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);

            margin.Bottom = margin.Top;

            margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);

            margin.Right = margin.Left;

 

            //Create Section

            PdfSection section = doc.Sections.Add();

            section.PageSettings.Size = PdfPageSize.A4;

            section.PageSettings.Margins = margin;

 

            // Create Page

            PdfPageBase page = section.Pages.Add();

 

            float y = 10;

 

            //Document Title

            PdfBrush brush1 = PdfBrushes.Black;

            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font(“Calibri”, 14f, FontStyle.Bold));

            PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);

            page.Canvas.DrawString(“Attachment”, font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);

            y = y + font1.MeasureString(“Attachment”, format1).Height;

            y = y + 5;

 

            //Add Attachment

            PdfAttachment attachment = new PdfAttachment(“Twitter.png”);

            attachment.Data = File.ReadAllBytes(@”D:\work\My Documents\twitter.png”);

            attachment.Description = “Twitter Logo”;

            attachment.MimeType = “image/png”;

            doc.Attachments.Add(attachment);

 

            attachment = new PdfAttachment(“Facebook.png”);

            attachment.Data = File.ReadAllBytes(@”D:\work\My Documents\facebook.png”);

            attachment.Description = “Facebook Logo”;

            attachment.MimeType = “image/png”;

            doc.Attachments.Add(attachment);

 

            //Set Attachment Label and Format

            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font(“Calibri”, 12f, FontStyle.Bold));

            PointF location = new PointF(0, y);

            String label = “Sales Report”;

            byte[] data = File.ReadAllBytes(@”D:\work\My Documents\Sales Report.xlsx”);

            SizeF size = font2.MeasureString(label);

            RectangleF bounds = new RectangleF(location, size);

            page.Canvas.DrawString(label, font2, PdfBrushes.DarkTurquoise, bounds);

            bounds = new RectangleF(bounds.Right + 3, bounds.Top, font2.Height / 2, font2.Height);

            PdfAttachmentAnnotation annotation1

                = new PdfAttachmentAnnotation(bounds, “Sales Report.xlsx”, data);

            annotation1.Color = Color.Teal;

            annotation1.Flags = PdfAnnotationFlags.ReadOnly;

            annotation1.Icon = PdfAttachmentIcon.Graph;

            annotation1.Text = “Sales Report”;

            (page as PdfNewPage).Annotations.Add(annotation1);

            y = y + size.Height + 2;

 

            location = new PointF(0, y);

            label = “Employee Information”;

            data = File.ReadAllBytes(@”D:\work\My Documents\employee.xlsx”);

            size = font2.MeasureString(label);

            bounds = new RectangleF(location, size);

            page.Canvas.DrawString(label, font2, PdfBrushes.DarkKhaki, bounds);

            bounds = new RectangleF(bounds.Right + 3, bounds.Top, font2.Height / 2, font2.Height);

            PdfAttachmentAnnotation annotation2

                = new PdfAttachmentAnnotation(bounds, “employee.xlsx”, data);

            annotation2.Color = Color.Orange;

            annotation2.Flags = PdfAnnotationFlags.NoZoom;

            annotation2.Icon = PdfAttachmentIcon.PushPin;

            annotation2.Text = “Employee Information”;

            (page as PdfNewPage).Annotations.Add(annotation2);

            y = y + size.Height + 2;

 

            location = new PointF(0, y);

            label = “VIP Information”;

            data = File.ReadAllBytes(@”D:\work\My Documents\vip.bmp”);

            size = font2.MeasureString(label);

            bounds = new RectangleF(location, size);

            page.Canvas.DrawString(label, font2, PdfBrushes.DarkViolet, bounds);

            bounds = new RectangleF(bounds.Right + 3, bounds.Top, font2.Height / 2, font2.Height);

            PdfAttachmentAnnotation annotation4

                = new PdfAttachmentAnnotation(bounds, “VIP Information”, data);

            annotation4.Color = Color.CadetBlue;

            annotation4.Flags = PdfAnnotationFlags.NoRotate;

            annotation4.Icon = PdfAttachmentIcon.Paperclip;

            annotation4.Text = “VIP Information (Parts)”;

            (page as PdfNewPage).Annotations.Add(annotation4);

            y = y + size.Height + 2;

 

            location = new PointF(0, y);

            label = “Background Picture”;

            data = File.ReadAllBytes(@”D:\work\My Documents\F.jpg”);

            size = font2.MeasureString(label);

            bounds = new RectangleF(location, size);

            page.Canvas.DrawString(label, font2, PdfBrushes.LightCoral, bounds);

            bounds = new RectangleF(bounds.Right + 3, bounds.Top, font2.Height / 2, font2.Height);

            PdfAttachmentAnnotation annotation3

                = new PdfAttachmentAnnotation(bounds, “Background Picture”, data);

            annotation3.Color = Color.SaddleBrown;

            annotation3.Flags = PdfAnnotationFlags.Locked;

            annotation3.Icon = PdfAttachmentIcon.Tag;

            annotation3.Text = “PDF Background Picture(Watermark)”;

            (page as PdfNewPage).Annotations.Add(annotation3);

            y = y + size.Height + 2;

 

            //Save and Launch File

            doc.SaveToFile(“Attachment.pdf”);

            doc.Close();

            System.Diagnostics.Process.Start(“Attachment.pdf”);

        }

    }

Result Shown by Following:

__________________________________________________________________________

Click Here to Learn more about Spire.PDF

Click Here to Download Spire.PDF

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s