from ironpdf import * # Instantiate Renderer renderer = ChromePdfRenderer() multi_page_html = """ <p> This is 1st Page</p> <div style = 'page-break-after: always;' ></div> <p> This is 2nd Page</p> <div style = 'page-break-after: always;' ></div> <p> This is 3rd Page</p> <div style = 'page-break-after: always;' ></div> <p> This is 4th Page</p> <div style = 'page-break-after: always;' ></div> <p> This is 5th Page</p> <div style = 'page-break-after: always;' ></div> <p> This is 6th Page</p> <div style = 'page-break-after: always;' ></div> <p> This is 7th Page</p> """ # Create a PDF or Load an existing PDF using PdfDocument.FromFile() pdf = renderer.RenderHtmlAsPdf(multi_page_html) # Create a Header header = HtmlHeaderFooter() header.HtmlFragment = "<div>THIS IS HEADER {page} of {total-pages}</div>" # Create a Page Range 0 .. 7 all_page_indexes = range(pdf.PageCount) # Example 1 # Apply header to even page index only. (page number will be odd number because index start at 0 but page number start at 1) even_page_indexes = [i for i in all_page_indexes if i % 2 == 0] pdf.AddHtmlHeaders(header, 1, ToPageList(even_page_indexes)) pdf.SaveAs("even_page_indexes.pdf") # Example 2 # Apply header to odd page index only. (page number will be even number because index start at 0 but page number start at 1) new_pdf = renderer.RenderHtmlAsPdf(multi_page_html) odd_page_indexes = [i for i in all_page_indexes if i % 2 != 0] new_pdf.AddHtmlHeaders(header, 1, ToPageList(odd_page_indexes)) new_pdf.SaveAs("odd_page_indexes.pdf") # Example 3 # Apply header to last page only. new_pdf = renderer.RenderHtmlAsPdf(multi_page_html) new_pdf.AddHtmlHeaders(header, 1, ToPageList([new_pdf.PageCount - 1])) new_pdf.SaveAs("last_page_only.pdf") # Example 4 # Apply header to first page only. new_pdf = renderer.RenderHtmlAsPdf(multi_page_html) new_pdf.AddHtmlHeaders(header, 1, ToPage(0)) new_pdf.SaveAs("first_page_only.pdf") # Example 5 # Skip the first page. new_pdf = renderer.RenderHtmlAsPdf(multi_page_html) new_pdf.AddHtmlHeaders(header, 1, ToPageRange(1, new_pdf.PageCount)) new_pdf.SaveAs("skip_first_page.pdf") # Example 6 # Skip first page and make second pages label as 1. new_pdf = renderer.RenderHtmlAsPdf(multi_page_html) new_pdf.AddHtmlHeaders(header, 0, ToPageRange(1, new_pdf.PageCount)) new_pdf.SaveAs("skip_and_dont_count_the_first_page.pdf")
Kopfzeilen/Fußzeilen auf bestimmten Seiten hinzufügen