Merge Capabilities |
The HTML to PDF converter can add existing PDF documents before and after the conversion result: a cover page in front of a converted report, terms and conditions after an offer. The documents are set with properties of the PdfDocumentOptions class, as files or as streams, before the conversion method is called. Their pages are merged as they are, with their own size and links, and the header and footer of the converter are not drawn on them. A document that cannot be read fails the conversion.
The properties are:
public string AppendPDFFile { get; set; } public string[] AppendPDFFileArray { get; set; } public Stream AppendPDFStream { get; set; } public Stream[] AppendPDFStreamArray { get; set; } public string PrependPDFFile { get; set; } public string[] PrependPDFFileArray { get; set; } public Stream PrependPDFStream { get; set; } public Stream[] PrependPDFStreamArray { get; set; }
PdfConverter pdfConverter = new PdfConverter(); // existing documents appended after the converted page: a file, and a document from // memory pdfConverter.PdfDocumentOptions.AppendPDFFile = "terms-and-conditions.pdf"; byte[] attachmentBytes = File.ReadAllBytes("price-list.pdf"); pdfConverter.PdfDocumentOptions.AppendPDFStreamArray = new Stream[] { new MemoryStream(attachmentBytes) }; // and a cover page placed before it pdfConverter.PdfDocumentOptions.PrependPDFFile = "cover.pdf"; pdfConverter.SavePdfFromUrlToFile("https://www.example.com/offer", "offer.pdf");
When the converter produces a PDF/A document (see PDF Standards), the documents you add before or after the conversion result must conform to the same level: the same PDF/A part, with at least the same conformance (a PDF/A-2b result takes PDF/A-2b, PDF/A-2u and PDF/A-2a documents). Their pages are added as they are, so the conversion refuses a document that does not conform, with an HtmlConvertException. Convert such a document first: load it into a Document, call ConvertToPdfA(PdfStandardSubset) and save it. AppendDocument(Document) on a PDF/A document applies the same rule.
// make an existing document PDF/A-2b before it is appended to a PDF/A-2b conversion using (Document terms = new Document("terms-and-conditions.pdf")) { terms.ConvertToPdfA(PdfStandardSubset.Pdf_A_2b); terms.Save("terms-pdfa.pdf"); } PdfConverter pdfConverter = new PdfConverter(); pdfConverter.PdfStandardSubset = PdfStandardSubset.Pdf_A_2b; pdfConverter.PdfDocumentOptions.AppendPDFFile = "terms-pdfa.pdf"; pdfConverter.SavePdfFromUrlToFile("https://www.example.com/offer", "offer-pdfa.pdf");
To insert pages anywhere in the result, convert to a Document object and use AppendDocument(Document) or the pages collection (see Post Convert Customization). To merge existing PDF files without a conversion, use the PDF merge tool described in Merge PDF.