Attachments
Any file can travel inside the PDF, listed in the viewer's attachments panel: the source spreadsheet of a report, the data behind a chart, a signed contract.
var options = new PdfOptions { Conformance = PdfConformance.PdfA3b }; // optional
options.Attachments.Add(new PdfAttachment("figures.csv", csvBytes)
{
Description = "The figures behind the charts", // default: the file name
Relationship = PdfAttachmentRelationship.Data, // Source, Data, Alternative, Supplement or Unspecified
// MimeType = "text/csv", // default: guessed from the extension
// CreationDate = ..., ModDate = ... // default: ModDate is the time of conversion
});
options.Attachments.Add(PdfAttachment.FromFile("contract.pdf"));
byte[] pdf = HtmlToPdf.Convert(html, options);
// Read them back, or attach files to a PDF made elsewhere.
foreach (PdfAttachment a in PdfAttachments.List(pdf)) Console.WriteLine($"{a.FileName}: {a.Data.Length} bytes");
byte[]? csv = PdfAttachments.Extract(pdf, "figures.csv");
byte[] more = PdfAttachments.Add(File.ReadAllBytes("scan.pdf"), new PdfAttachment("notes.txt", notes));
How files are stored
Each file is written with its MIME type, size, dates and MD5 checksum, in the document's embedded-files list sorted by
name, and as an associated file (/AF) with its relationship to the pages, as PDF/A-3 requires. File names must be
unique (ArgumentException otherwise; an attachment can't take the e-invoice's factur-x.xml). Attachments don't change
Conformance: without one the file is a plain PDF, PDF/A-3b, PDF/A-3u and PDF/A-4f allow any attachment, and PDF/A-2b,
PDF/A-2u and PDF/A-4, which allow only PDF/A files as attachments, throw ArgumentException rather than produce a file
that fails validation. With Security the attachments are encrypted with the rest of the file, and for PDF/UA each one
gets the description ISO 14289 asks for. The output passes veraPDF's PDF/A-3b, PDF/A-3u, PDF/A-4f, PDF/UA-1 and
PDF/UA-2 checks. PdfAttachments.Add likewise refuses PDF/A-1, PDF/A-2 and PDF/A-4 files, and adds to PDF/A-3 and
PDF/A-4f files.
Adding to an existing PDF
PdfAttachments.Add appends the files as an incremental update, so the original bytes, and any digital signatures
over them, stay as they were; an encrypted PDF (pass its password) stays encrypted, a PDF/A-3 file gets the new
associated files, and PDF/A-1 and PDF/A-2 files are refused. List and Extract read the embedded-files list and
associated files of any PDF, decrypting with the password you pass; files attached to page annotations (the paper-clip
icons) are not read.