You can add attachments to a PDF document from a file using the PdfDocumentCreateAttachmentFromFile methods or from PDF document data using the PdfDocumentCreateAttachmentFromData methods. The attachments can be represented in PDF pages by an icon or they can be created without an icon.
PDF Attachments Demo
In this demo you can learn how to attach external files and streams to a PDF document. When you create the attachment you can also give a page and a location in page where to create a link to the attachment marked with an icon.
Demo Source Code
C#
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Hosting; using HiQPdfClient; namespace HiQPdf_Demo.Controllers { public class AttachmentsAndFileLinksController : Controller { IWebHostEnvironment m_hostingEnvironment; public AttachmentsAndFileLinksController(IWebHostEnvironment hostingEnvironment) { m_hostingEnvironment = hostingEnvironment; } // GET: AttachmentsAndFileLinks public ActionResult Index() { return View(); } [HttpPost] public ActionResult CreatePdf(IFormCollection collection) { string serverIP = collection["textBoxServerIP"]; uint serverPort = uint.Parse(collection["textBoxServerPort"]); string serverPassword = collection["textBoxServerPassword"]; // create a PDF document PdfDocument document = new PdfDocument(serverIP, serverPort); // use server password if necessary if (serverPassword.Length > 0) document.ServerPassword = serverPassword; // set a demo serial number document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ=="; // display the attachments when the document is opened document.Viewer.PageMode = PdfPageMode.Attachments; // create a page in document PdfPage page1 = document.AddPage(); // create the true type fonts that can be used in document PdfFont pdfFontEmbed = document.CreateFontFromName("Times New Roman", 10, true); // create an attachment with icon from file string filePath1 = m_hostingEnvironment.WebRootPath + "/DemoFiles/Attach/TextAttach1.txt"; PdfAttachment pdfAttachment1 = document.CreateAttachmentFromFile(page1, new RectangleFloat(10, 30, 10, 20), PdfAttachIconType.PushPin, filePath1); pdfAttachment1.Description = "Attachment with icon from a file"; // write a description at the right of the icon PdfText pdfAttachment1Descr = new PdfText(40, 35, pdfAttachment1.Description, pdfFontEmbed); page1.Layout(pdfAttachment1Descr); // create an attachment with icon from a data byte[] fileData2 = System.IO.File.ReadAllBytes(filePath1); PdfAttachment pdfAttachment2 = document.CreateAttachmentFromData(page1, new RectangleFloat(10, 60, 10, 20), PdfAttachIconType.Paperclip, fileData2, "AttachFromData_WithIcon.txt"); pdfAttachment2.Description = "Attachment with icon from data"; // write a description at the right of the icon PdfText pdfAttachment2Descr = new PdfText(40, 65, pdfAttachment2.Description, pdfFontEmbed); page1.Layout(pdfAttachment2Descr); // create an attachment without icon in PDF from a file string filePath2 = m_hostingEnvironment.WebRootPath + "/DemoFiles/Attach/TextAttach2.txt"; document.CreateAttachmentFromFile(filePath2, "Attachment without icon from a file"); // create an attachment without icon in PDF from data byte[] fileData1 = System.IO.File.ReadAllBytes(filePath2); document.CreateAttachmentFromData(fileData1, "AttachFromData_NoIcon.txt", "Attachment without icon from data"); try { // write the PDF document to a memory buffer byte[] pdfBuffer = document.WriteToMemory(); FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf"); fileResult.FileDownloadName = "PdfAttachments.pdf"; return fileResult; } finally { document.Close(); } } } }
See Also