|
| 1 | +--- |
| 2 | +title: Generating PDF with Headers and Footers from Separate HTML Files |
| 3 | +description: Learn how to generate PDF documents with headers and footers using separate HTML files. |
| 4 | +type: how-to |
| 5 | +page_title: Generating PDF with Headers and Footers from Separate HTML Files |
| 6 | +meta_title: Generating PDF with Headers and Footers from Separate HTML Files |
| 7 | +slug: generate-pdf-with-headers-footers-from-separate-html-files |
| 8 | +tags: telerik, document, processing, word, header, footer, pdf, html, merge |
| 9 | +res_type: kb |
| 10 | +ticketid: 1702165 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +| Version | Product | Author | |
| 16 | +| ---- | ---- | ---- | |
| 17 | +| 2025.3.806| RadWordsProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +Learn how to combine **separate** HTML files for **header**, **footer** and **content** into one common document and product a PDF document with the result. |
| 22 | + |
| 23 | +## Solution |
| 24 | + |
| 25 | +To generate a PDF with separate headers and footers, process the HTML files using RadWordsProcessing. Follow these steps: |
| 26 | + |
| 27 | +1. **Import the HTML content**: Use the [HtmlFormatProvider]({%slug radwordsprocessing-overview%}) to import the HTML content into [RadFlowDocument]({%slug radwordsprocessing-model-radflowdocument%}) objects. |
| 28 | +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. |
| 29 | +3. **Export the document to PDF**: Use the [PdfFormatProvider]({%slug radwordsprocessing-formats-and-conversion-pdf-pdfformatprovider%}) to export the final document to PDF format. |
| 30 | + |
| 31 | +### Code Example |
| 32 | + |
| 33 | +```csharp |
| 34 | + static void Main(string[] args) |
| 35 | + { |
| 36 | + |
| 37 | + string headerHtml = "\r\n<p style=\"text-align: center; font-size: 16pt; font-weight: bold;\">\r\n Company Name\r\n</p>\r\n<p style=\"text-align: center; font-size: 12pt;\">\r\n Document Title\r\n</p>\r\n<p style=\"text-align: center; font-size: 10pt;\">\r\n Date: October 28, 2025\r\n</p>\r\n"; |
| 38 | + string contentHtml = "<p>This is the main content of the document.</p>"; |
| 39 | + string footerHtml = "\r\n<p style=\"text-align: center; font-size: 10pt;\">\r\n Confidential - For Internal Use Only\r\n</p>\r\n<p style=\"text-align: center; font-size: 10pt;\">\r\n Page 1 of 1\r\n</p>\r\n"; |
| 40 | + |
| 41 | + var htmlProvider = new HtmlFormatProvider(); |
| 42 | + |
| 43 | + // Load each HTML file into a separate document |
| 44 | + RadFlowDocument headerDoc = htmlProvider.Import(headerHtml, TimeSpan.FromSeconds(10)); |
| 45 | + RadFlowDocument contentDoc = htmlProvider.Import(contentHtml, TimeSpan.FromSeconds(10)); |
| 46 | + RadFlowDocument footerDoc = htmlProvider.Import(footerHtml, TimeSpan.FromSeconds(10)); |
| 47 | + |
| 48 | + // Create importer for header |
| 49 | + var headerImporter = new DocumentElementImporter(contentDoc, headerDoc, ConflictingStylesResolutionMode.UseTargetStyle); |
| 50 | + Header defaultHeader = contentDoc.Sections.First().Headers.Add(); |
| 51 | + |
| 52 | + foreach (var block in headerDoc.Sections.First().Blocks) |
| 53 | + { |
| 54 | + BlockBase importedBlock = headerImporter.Import(block); |
| 55 | + defaultHeader.Blocks.Add(importedBlock); |
| 56 | + } |
| 57 | + |
| 58 | + // Create importer for footer |
| 59 | + var footerImporter = new DocumentElementImporter(contentDoc, footerDoc, ConflictingStylesResolutionMode.UseTargetStyle); |
| 60 | + Footer defaultFooter = contentDoc.Sections.First().Footers.Add(); |
| 61 | + |
| 62 | + foreach (var block in footerDoc.Sections.First().Blocks) |
| 63 | + { |
| 64 | + BlockBase importedBlock = footerImporter.Import(block); |
| 65 | + defaultFooter.Blocks.Add(importedBlock); |
| 66 | + } |
| 67 | + |
| 68 | + // Export to PDF |
| 69 | + var pdfProvider = new PdfFormatProvider(); |
| 70 | + string outputFilePath = "FinalDocument.pdf"; |
| 71 | + using (var outputStream = File.Create(outputFilePath)) |
| 72 | + { |
| 73 | + pdfProvider.Export(contentDoc, outputStream, TimeSpan.FromSeconds(10)); |
| 74 | + } |
| 75 | + Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); |
| 76 | + } |
| 77 | +``` |
| 78 | +Let's have the following 3 separate HTML files: |
| 79 | + |
| 80 | +* Header HTML: |
| 81 | + |
| 82 | +```html |
| 83 | +<p style="text-align: center; font-size: 16pt; font-weight: bold;"> |
| 84 | + Company Name |
| 85 | +</p> |
| 86 | +<p style="text-align: center; font-size: 12pt;"> |
| 87 | + Document Title |
| 88 | +</p> |
| 89 | +<p style="text-align: center; font-size: 10pt;"> |
| 90 | + Date: October 28, 2025 |
| 91 | +</p> |
| 92 | + |
| 93 | +``` |
| 94 | + |
| 95 | + |
| 96 | +* Footer HTML |
| 97 | + |
| 98 | +```html |
| 99 | +<p style="text-align: center; font-size: 10pt;"> |
| 100 | + Confidential - For Internal Use Only |
| 101 | +</p> |
| 102 | +<p style="text-align: center; font-size: 10pt;"> |
| 103 | + Page 1 of 1 |
| 104 | +</p> |
| 105 | + |
| 106 | +``` |
| 107 | + |
| 108 | + |
| 109 | +* Content HTML |
| 110 | + |
| 111 | +```html |
| 112 | +<p>This is the main content of the document.</p> |
| 113 | + |
| 114 | +``` |
| 115 | + |
| 116 | + |
| 117 | +The result PDF document combined all of the HTML files in one common document: |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +## See Also |
| 122 | + |
| 123 | +- [DocumentElementImporter]({%slug radwordsprocessing-editing-import-document-element%}) |
| 124 | +- [Headers and footers]({%slug radwordsprocessing-model-headers-footers%}) |
0 commit comments