C# tutorial: PDF file attachment

PDF file attachment

iTextSharp allows you to embed or attach files to a PDF document. A file attachment is also an annotation. If you click the Manage File Attachments icon in the left side bar of Foxit Reader, you will see files that were attached to the PDF document. As you can see the attachments are a series of JPG image files (gift1.jpg,gift2.jpg...). If you click the attachment icon next to the book's identification number (B001, B002...), the image will be opened. Note
that, depending on your security preferences, the PDF viewer may ask you if you're sure before opening an image viewer application.

pdf file attachment

One way to embed or attach files to the PDF document is using the CreateFileAttachment method of the PdfAnnotation class. The CreateFileAttachment method has the following paramethers:

- PdfWriterwriter—An instance of PdfWriter.
- Rectangle rect—The rectangle where you want the annotation to appear.
- String contents—A description for the file attachment.
- byte[] fileStore—The bytes of the file you want to attach to the PDF, or null if the fileparameter is a valid path to a file.

- String file—The path to the file you want to attach. This path will be ignored if fileStore isn't null.
- String fileDisplay—The (new) filename that will be given to the attached file.

//loop through the TblBook table to get all books' IDs and add file attachment annotation next to these IDs in PDF document
foreach (var book in books)
Paragraph pa = new Paragraph();
Chunk cid = new Chunk(book.BID);
Chunk can = new Chunk("\u00a0\u00a0");
PdfAnnotation an = PdfAnnotation.CreateFileAttachment(writer, null, book.Title, null, imgpath + book.ImgPath, book.ImgPath);


You can download the complete C# code of this tutorial from here.


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.