PDF is so powerful that it can attach every kind of files in it, such as Word, Excel, Text, TTF, Images, PDF and so on, which escapes the trouble of converting these files to PDF documents, which also provides great convenience to use the information of these files. Thus, PDF attachment function is really useful and practical. While how to attach these files in PDF with C#, VB.NET is what we talk about in this article.
In this article, I will introduce an easy method to attach Word, Excel, Text, TTF files and Images in one PDF document with C#, VB.NET. Of course, you also can attach other files in it. During my method, I use a powerful PDF document creation component Spire.PDFto help me. It can be used on the server-side (ASP.NET or any other environment) or with Windows Forms applications. If you want to give it a try, you can Freely Install Spire.PDF in your system. Please look at the below procedure.
In this article, I will introduce an easy method to attach Word, Excel, Text, TTF files and Images in one PDF document with C#, VB.NET. Of course, you also can attach other files in it. During my method, I use a powerful PDF document creation component Spire.PDFto help me. It can be used on the server-side (ASP.NET or any other environment) or with Windows Forms applications. If you want to give it a try, you can Freely Install Spire.PDF in your system. Please look at the below procedure.
Procedure
Step1. Create a new project
1. Create a new project in Visual Studio, and set its Target framework to be .NET Framework 4 in Properties.
2. Add System. Drawing and Spire.PDF Dll as reference in Project.
3. Add the below using at the top of the file.
C#
using System.Drawing;
using System.IO;
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Attachments;
using Spire.Pdf.Graphics;
1. Create a new project in Visual Studio, and set its Target framework to be .NET Framework 4 in Properties.
2. Add System. Drawing and Spire.PDF Dll as reference in Project.
3. Add the below using at the top of the file.
C#
using System.Drawing;
using System.IO;
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Attachments;
using Spire.Pdf.Graphics;
Step2. Attach Word, Excel, TTF, Text and images in PDF document with C#, VB.NET.
1. Create a new PDF document and set its margin, then, create a section in it and one page in the section.
C# Code:
//Create a pdf document.
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 one page
PdfPageBase page = section.Pages.Add();
float y = 10;
1. Create a new PDF document and set its margin, then, create a section in it and one page in the section.
C# Code:
//Create a pdf document.
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 one page
PdfPageBase page = section.Pages.Add();
float y = 10;
2. Set the title of the PDF document.
C# Code:
//title
PdfBrush brush1 = PdfBrushes.Black;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, 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;
C# Code:
//title
PdfBrush brush1 = PdfBrushes.Black;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, 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;
3. Attach images in PDF document. Spire.PDF allows people attach different formats of images in it.
C# Code:
//attach images
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(@"D:\michelle\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;
location = new PointF(0, y);
label = "Science Personification Boston";
data = File.ReadAllBytes(@"D:\michelle\SciencePersonificationBoston.jpg");
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, "SciencePersonificationBoston.jpg", data);
annotation2.Color = Color.Orange;
annotation2.Flags = PdfAnnotationFlags.NoZoom;
annotation2.Icon = PdfAttachmentIcon.PushPin;
annotation2.Text = "SciencePersonificationBoston.jpg, from Wikipedia, the free encyclopedia";
(page as PdfNewPage).Annotations.Add(annotation2);
y = y + size.Height + 2;
location = new PointF(0, y);
label = "Picture of Science";
data = File.ReadAllBytes(@"D:\michelle\Wikipedia_Science.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 annotation3
= new PdfAttachmentAnnotation(bounds, "Wikipedia_Science.png", data);
annotation3.Color = Color.SaddleBrown;
annotation3.Flags = PdfAnnotationFlags.Locked;
annotation3.Icon = PdfAttachmentIcon.Tag;
annotation3.Text = "Wikipedia_Science.png, from Wikipedia, the free encyclopedia";
(page as PdfNewPage).Annotations.Add(annotation3);
y = y + size.Height + 2;
C# Code:
//attach images
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(@"D:\michelle\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;
location = new PointF(0, y);
label = "Science Personification Boston";
data = File.ReadAllBytes(@"D:\michelle\SciencePersonificationBoston.jpg");
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, "SciencePersonificationBoston.jpg", data);
annotation2.Color = Color.Orange;
annotation2.Flags = PdfAnnotationFlags.NoZoom;
annotation2.Icon = PdfAttachmentIcon.PushPin;
annotation2.Text = "SciencePersonificationBoston.jpg, from Wikipedia, the free encyclopedia";
(page as PdfNewPage).Annotations.Add(annotation2);
y = y + size.Height + 2;
location = new PointF(0, y);
label = "Picture of Science";
data = File.ReadAllBytes(@"D:\michelle\Wikipedia_Science.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 annotation3
= new PdfAttachmentAnnotation(bounds, "Wikipedia_Science.png", data);
annotation3.Color = Color.SaddleBrown;
annotation3.Flags = PdfAnnotationFlags.Locked;
annotation3.Icon = PdfAttachmentIcon.Tag;
annotation3.Text = "Wikipedia_Science.png, from Wikipedia, the free encyclopedia";
(page as PdfNewPage).Annotations.Add(annotation3);
y = y + size.Height + 2;
4. Attach documents in PDF file. In this step, I attach a word, excel, text, ttf in it, if necessary,you also can attach other formats files such as html, xml, tiff and so on.
C# Code:
//attach ttf file
location = new PointF(0, y);
label = "Hawaii Killer Font";
data = File.ReadAllBytes(@"D:\michelle\Hawaii_Killer.ttf");
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 annotation4
= new PdfAttachmentAnnotation(bounds, "Hawaii_Killer.ttf", data);
annotation4.Color = Color.CadetBlue;
annotation4.Flags = PdfAnnotationFlags.NoRotate;
//annotation4.Icon = PdfAttachmentIcon.Paperclip;
annotation4.Text = "Hawaii Killer Font, from http://www.1001freefonts.com";
(page as PdfNewPage).Annotations.Add(annotation4);
y = y + size.Height + 2;
//attanch a word document
location = new PointF(0, y);
label = "Jane Eyre";
data = File.ReadAllBytes(@"E:\JaneEyre.docx");
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 annotation5
= new PdfAttachmentAnnotation(bounds,"JaneEyre.docx" , data);
annotation5.Color = Color.Green;
annotation5.Flags = PdfAnnotationFlags.NoRotate;
//annotation5.Icon = PdfAttachmentIcon.Paperclip;
annotation5.Text = "JaneEyre, from http://www.encyclopaedia.com";
(page as PdfNewPage).Annotations.Add(annotation5);
y = y + size.Height + 2;
//attach a txt file
location = new PointF(0, y);
label = "The Spring Festival";
data = File.ReadAllBytes(@"E:\SpringFestival.txt");
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 annotation6
= new PdfAttachmentAnnotation(bounds, "SpringFestival.txt", data);
annotation6.Color = Color.HotPink;
annotation6.Flags = PdfAnnotationFlags.NoRotate;
//annotation6.Icon = PdfAttachmentIcon.Paperclip;
annotation6.Text = "The Spring Festival, from http://www.everlasting129.weebly.com";
(page as PdfNewPage).Annotations.Add(annotation6);
y = y + size.Height + 2;
//attach an excel document
location = new PointF(0, y);
label = "Excel";
data = File.ReadAllBytes(@"E:\excel.xlsx");
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 annotation7
= new PdfAttachmentAnnotation(bounds, "excel.xlsx", data);
annotation7.Color = Color.OrangeRed;
annotation7.Flags = PdfAnnotationFlags.NoRotate;
//annotation7.Icon = PdfAttachmentIcon.Paperclip;
annotation7.Text = "excel, from http://www.e-iceblue.com";
(page as PdfNewPage).Annotations.Add(annotation7);
y = y + size.Height + 2;
C# Code:
//attach ttf file
location = new PointF(0, y);
label = "Hawaii Killer Font";
data = File.ReadAllBytes(@"D:\michelle\Hawaii_Killer.ttf");
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 annotation4
= new PdfAttachmentAnnotation(bounds, "Hawaii_Killer.ttf", data);
annotation4.Color = Color.CadetBlue;
annotation4.Flags = PdfAnnotationFlags.NoRotate;
//annotation4.Icon = PdfAttachmentIcon.Paperclip;
annotation4.Text = "Hawaii Killer Font, from http://www.1001freefonts.com";
(page as PdfNewPage).Annotations.Add(annotation4);
y = y + size.Height + 2;
//attanch a word document
location = new PointF(0, y);
label = "Jane Eyre";
data = File.ReadAllBytes(@"E:\JaneEyre.docx");
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 annotation5
= new PdfAttachmentAnnotation(bounds,"JaneEyre.docx" , data);
annotation5.Color = Color.Green;
annotation5.Flags = PdfAnnotationFlags.NoRotate;
//annotation5.Icon = PdfAttachmentIcon.Paperclip;
annotation5.Text = "JaneEyre, from http://www.encyclopaedia.com";
(page as PdfNewPage).Annotations.Add(annotation5);
y = y + size.Height + 2;
//attach a txt file
location = new PointF(0, y);
label = "The Spring Festival";
data = File.ReadAllBytes(@"E:\SpringFestival.txt");
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 annotation6
= new PdfAttachmentAnnotation(bounds, "SpringFestival.txt", data);
annotation6.Color = Color.HotPink;
annotation6.Flags = PdfAnnotationFlags.NoRotate;
//annotation6.Icon = PdfAttachmentIcon.Paperclip;
annotation6.Text = "The Spring Festival, from http://www.everlasting129.weebly.com";
(page as PdfNewPage).Annotations.Add(annotation6);
y = y + size.Height + 2;
//attach an excel document
location = new PointF(0, y);
label = "Excel";
data = File.ReadAllBytes(@"E:\excel.xlsx");
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 annotation7
= new PdfAttachmentAnnotation(bounds, "excel.xlsx", data);
annotation7.Color = Color.OrangeRed;
annotation7.Flags = PdfAnnotationFlags.NoRotate;
//annotation7.Icon = PdfAttachmentIcon.Paperclip;
annotation7.Text = "excel, from http://www.e-iceblue.com";
(page as PdfNewPage).Annotations.Add(annotation7);
y = y + size.Height + 2;
Step3. Save and launch the file.
C# Code:
//Save pdf file.
doc.SaveToFile("Attachment.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("Attachment.pdf");
C# Code:
//Save pdf file.
doc.SaveToFile("Attachment.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("Attachment.pdf");
Besides, PDF has many other functions. All can be realized by C#, VB.NET. If you want to know more, please consult by the below link.
More PDF Functions
More PDF Functions
No comments:
Post a Comment