diff --git a/knowledge-base/generate-pdf-with-headers-footers-from-separate-html-files.md b/knowledge-base/generate-pdf-with-headers-footers-from-separate-html-files.md new file mode 100644 index 00000000..5dda3d99 --- /dev/null +++ b/knowledge-base/generate-pdf-with-headers-footers-from-separate-html-files.md @@ -0,0 +1,124 @@ +--- +title: Generating PDF with Headers and Footers from Separate HTML Files +description: Learn how to generate PDF documents with headers and footers using separate HTML files. +type: how-to +page_title: Generating PDF with Headers and Footers from Separate HTML Files +meta_title: Generating PDF with Headers and Footers from Separate HTML Files +slug: generate-pdf-with-headers-footers-from-separate-html-files +tags: telerik, document, processing, word, header, footer, pdf, html, merge +res_type: kb +ticketid: 1702165 +--- + +## Environment + +| Version | Product | Author | +| ---- | ---- | ---- | +| 2025.3.806| RadWordsProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| + +## Description + +Learn how to combine **separate** HTML files for **header**, **footer** and **content** into one common document and product a PDF document with the result. + +## Solution + +To generate a PDF with separate headers and footers, process the HTML files using RadWordsProcessing. Follow these steps: + +1. **Import the HTML content**: Use the [HtmlFormatProvider]({%slug radwordsprocessing-overview%}) to import the HTML content into [RadFlowDocument]({%slug radwordsprocessing-model-radflowdocument%}) objects. +2. **Add headers and footers**: Use the [DocumentElementImporter]({%slug radwordsprocessing-editing-import-document-element%}) to insert [header and footer]({%slug radwordsprocessing-model-headers-footers%}) content into the main document. +3. **Export the document to PDF**: Use the [PdfFormatProvider]({%slug radwordsprocessing-formats-and-conversion-pdf-pdfformatprovider%}) to export the final document to PDF format. + +### Code Example + +```csharp + static void Main(string[] args) + { + + string headerHtml = "\r\n
\r\n Company Name\r\n
\r\n\r\n Document Title\r\n
\r\n\r\n Date: October 28, 2025\r\n
\r\n"; + string contentHtml = "This is the main content of the document.
"; + string footerHtml = "\r\n\r\n Confidential - For Internal Use Only\r\n
\r\n\r\n Page 1 of 1\r\n
\r\n"; + + var htmlProvider = new HtmlFormatProvider(); + + // Load each HTML file into a separate document + RadFlowDocument headerDoc = htmlProvider.Import(headerHtml, TimeSpan.FromSeconds(10)); + RadFlowDocument contentDoc = htmlProvider.Import(contentHtml, TimeSpan.FromSeconds(10)); + RadFlowDocument footerDoc = htmlProvider.Import(footerHtml, TimeSpan.FromSeconds(10)); + + // Create importer for header + var headerImporter = new DocumentElementImporter(contentDoc, headerDoc, ConflictingStylesResolutionMode.UseTargetStyle); + Header defaultHeader = contentDoc.Sections.First().Headers.Add(); + + foreach (var block in headerDoc.Sections.First().Blocks) + { + BlockBase importedBlock = headerImporter.Import(block); + defaultHeader.Blocks.Add(importedBlock); + } + + // Create importer for footer + var footerImporter = new DocumentElementImporter(contentDoc, footerDoc, ConflictingStylesResolutionMode.UseTargetStyle); + Footer defaultFooter = contentDoc.Sections.First().Footers.Add(); + + foreach (var block in footerDoc.Sections.First().Blocks) + { + BlockBase importedBlock = footerImporter.Import(block); + defaultFooter.Blocks.Add(importedBlock); + } + + // Export to PDF + var pdfProvider = new PdfFormatProvider(); + string outputFilePath = "FinalDocument.pdf"; + using (var outputStream = File.Create(outputFilePath)) + { + pdfProvider.Export(contentDoc, outputStream, TimeSpan.FromSeconds(10)); + } + Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); + } +``` +Let's have the following 3 separate HTML files: + +* Header HTML: + +```html ++ Company Name +
++ Document Title +
++ Date: October 28, 2025 +
+ +``` + + +* Footer HTML + +```html ++ Confidential - For Internal Use Only +
++ Page 1 of 1 +
+ +``` + + +* Content HTML + +```html +This is the main content of the document.
+ +``` + + +The result PDF document combined all of the HTML files in one common document: + + + +## See Also + +- [DocumentElementImporter]({%slug radwordsprocessing-editing-import-document-element%}) +- [Headers and footers]({%slug radwordsprocessing-model-headers-footers%}) diff --git a/knowledge-base/images/combined_pdf.png b/knowledge-base/images/combined_pdf.png new file mode 100644 index 00000000..cff8e6fd Binary files /dev/null and b/knowledge-base/images/combined_pdf.png differ diff --git a/knowledge-base/images/html-content-preview.png b/knowledge-base/images/html-content-preview.png new file mode 100644 index 00000000..339712e6 Binary files /dev/null and b/knowledge-base/images/html-content-preview.png differ diff --git a/knowledge-base/images/html-footer-preview.png b/knowledge-base/images/html-footer-preview.png new file mode 100644 index 00000000..694a0169 Binary files /dev/null and b/knowledge-base/images/html-footer-preview.png differ diff --git a/knowledge-base/images/html-header-preview.png b/knowledge-base/images/html-header-preview.png new file mode 100644 index 00000000..2005d8b1 Binary files /dev/null and b/knowledge-base/images/html-header-preview.png differ