Sunday, July 8, 2012

HTML to PDF Custom Header and Footer

Full demo source code and details  at HiQPDF HTML to PDF .NET Library
A PdfDocument object can be created by a call to HtmlToPdf.ConvertUrlToPdfDocument() or using the PdfDocument class constructor.  
A PdfDocument can have a header and footer which is repeated in each page of the document. Using the HiQPdf software it is possible to change the document header and footer in a page with a custom header and footer. 
Below you can find a complete example where a custom header is set in the first page and a custom footer is set in the second page. 


C# code:
private void buttonCreatePdf_Click(object sender, EventArgs e)
{
    // create an empty PDF document
    PdfDocument document = new PdfDocument();
    // add a page to document
    PdfPage page1 = document.AddPage(PdfPageSize.A4, new PdfDocumentMargins(5), PdfPageOrientation.Portrait);
    // set the document header and footer before adding any objects to document
    SetHeader(document);
    SetFooter(document);
    // add some content to first page
    PdfHtml html1 = new PdfHtml("<b>First Page Body</b>", null);
    PdfLayoutInfo html1LayoutInfo = page1.Layout(html1);
    // add a second page to document
    PdfPage page2 = document.AddPage(PdfPageSize.A4, new PdfDocumentMargins(5), PdfPageOrientation.Portrait);
    // add some content to second page
    PdfHtml html2 = new PdfHtml("<b>Second Page Body</b>", null);
    PdfLayoutInfo html2LayoutInfo = page2.Layout(html2);
    // set a custom header in first page
    page1 = document.Pages[0];
    // override the document header in first page
    page1.Header = document.CreateHeaderCanvas(document.Header.Width, document.Header.Height);
    // draw the custom page header
    DrawCustomHeader(page1.Header);
    // set a custom footer in second page
    page2 = document.Pages[1];
    // override the document footer in second page
    page2.Footer = document.CreateFooterCanvas(document.Footer.Width, document.Footer.Height);
    // draw the custom page footer
    DrawCustomFooter(page2.Footer);
    // write the output PDF file
    document.WriteToFile("CustomHeaderAndFooter.pdf");
}
private void SetHeader(PdfDocument document)
{
    // create the document header
    document.Header = document.CreateHeaderCanvas(document.Pages[0].DrawableRectangle.Width, 50);
    // layout HTML in header
    PdfHtml headerHtml = new PdfHtml(0, 0, 0, document.Header.Height, "<span style=\"color:Blue\">Default Header Content</span>", null);
    headerHtml.FitDestHeight = true;
    headerHtml.FontEmbedding = true;
    document.Header.Layout(headerHtml);
}
private void SetFooter(PdfDocument document)
{
    //create the document footer
    document.Footer = document.CreateFooterCanvas(document.Pages[0].DrawableRectangle.Width, 50);
    // layout HTML in footer
    PdfHtml footerHtml = new PdfHtml(0, 0, 0, document.Footer.Height, "<span style=\"color:Green\">Default Footer Content</span>", null);
    footerHtml.FitDestHeight = true;
    footerHtml.FontEmbedding = true;
    document.Footer.Layout(footerHtml);
}
private void DrawCustomHeader(PdfDocumentHeader pageHeader)
{
    // layout HTML in custom header
    PdfHtml headerHtml = new PdfHtml(0, 0, 0, pageHeader.Height, "<span style=\"color:Red\">Custom Header Content</span>", null);
    headerHtml.FitDestHeight = true;
    headerHtml.FontEmbedding = true;
    pageHeader.Layout(headerHtml);
}
private void DrawCustomFooter(PdfDocumentFooter pageFooter)
{
    // layout HTML in custom footer
    PdfHtml footerHtml = new PdfHtml(0, 0, 0, pageFooter.Height, "<span style=\"color:Red\">Custom Footer Content</span>", null);
    footerHtml.FitDestHeight = true;
    footerHtml.FontEmbedding = true;
    pageFooter.Layout(footerHtml);
}

No comments:

Post a Comment