HiQPdf Documentation

In Place HTML to PDF Object

Quickly Create High Quality PDFs

An In Place HTML to PDF Object is represented by the PdfHtml class. An object of this class can be laid out in any position in a PDF document to render HTML content in place. Multiple objects of this type can be laid out in the same PDF page and even overlapped. If there is no background color or image defined in the HTML document then the background of the rendered content in PDF will be transparent, making visible the existing content under it.

Multiple HTML Layers Demo

In this demo you can see how to layout and overlay multiple HTML objects in the same PDF document. The HTML from URL 1 is laid out at the beginning of the PDF document and the URL 2 is laid out immediately after first HTML or on a new page if 'Layout on New Page' option is on. If the second HTML is laid out on a new page then there is the option to change the orientation of the new pages. The HTML code from the text box is finally overlaid in the first page of the PDF document at the given location.

Demo Source Code

C#
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);

    Cursor = Cursors.WaitCursor;
    string pdfFile = Application.StartupPath + @"\DemoOut\LayoutMultipleHtml.pdf";
    try
    {
        // set the document header and footer before adding any objects to document
        SetHeader(document);
        SetFooter(document);

        // layout the HTML from URL 1
        PdfHtml html1 = new PdfHtml(textBoxUrl1.Text);
        PdfLayoutInfo html1LayoutInfo = page1.Layout(html1);

        // determine the PDF page where to add URL 2
        PdfPage page2 = null;
        PointF location2 = PointF.Empty;
        if (checkBoxNewPage.Checked)
        {
            // URL 2 is laid out on a new page with the selected orientation
            page2 = document.AddPage(PdfPageSize.A4, new PdfDocumentMargins(5), GetSelectedPageOrientation());
            location2 = PointF.Empty;
        }
        else
        {
            // URL 2 is laid out immediately after URL 1 and html1LayoutInfo
            // gives the location where the URL 1 layout finished
            page2 = document.Pages[html1LayoutInfo.LastPageIndex];
            location2 = new PointF(html1LayoutInfo.LastPageRectangle.X, html1LayoutInfo.LastPageRectangle.Bottom);
        }

        // layout the HTML from URL 2
        PdfHtml html2 = new PdfHtml(location2.X, location2.Y, textBoxUrl2.Text);
        page2.Layout(html2);

        // overlay the HTML code at the given location
        float destX = int.Parse(textBoxXLocation.Text);
        float destY = int.Parse(textBoxYLocation.Text);
        PdfHtml overlayHtml = new PdfHtml(destX, destY, textBoxHtmlCode.Text, textBoxBaseUrl.Text);
        overlayHtml.BrowserWidth = 500;
        page1.Layout(overlayHtml);

        document.WriteToFile(pdfFile);
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("Cannot create the PDF document. {0}", ex.Message));
        return;
    }
    finally
    {
        document.Close();
        Cursor = Cursors.Arrow;
    }

    // open the created PDF document
    try
    {
        System.Diagnostics.Process.Start(pdfFile);
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("The PDF document was created but cannot open '{0}'. {1}", pdfFile, ex.Message));
    }
}

private void SetHeader(PdfDocument document)
{
    if (!checkBoxAddHeader.Checked)
        return;

    // create the document header
    document.CreateHeaderCanvas(50);

    // add PDF objects to the header canvas
    string headerImageFile = Application.StartupPath + @"\DemoFiles\Images\HiQPdfLogo.png";
    PdfImage logoHeaderImage = new PdfImage(5, 5, 40, Image.FromFile(headerImageFile));
    document.Header.Layout(logoHeaderImage);

    // layout HTML in header
    PdfHtml headerHtml = new PdfHtml(50, 5, @"<span style=""color:Navy; font-family:Times New Roman; font-style:italic"">
                    Quickly Create High Quality PDFs with </span><a href=""http://www.hiqpdf.com"">HiQPdf</a>", null);
    headerHtml.FitDestHeight = true;
    headerHtml.FontEmbedding = true;
    document.Header.Layout(headerHtml);

    // create a border for header
    float headerWidth = document.Header.Width;
    float headerHeight = document.Header.Height;
    PdfRectangle borderRectangle = new PdfRectangle(1, 1, headerWidth - 2, headerHeight - 2);
    borderRectangle.LineStyle.LineWidth = 0.5f;
    borderRectangle.ForeColor = Color.Navy;
    document.Header.Layout(borderRectangle);
}

private void SetFooter(PdfDocument document)
{
    if (!checkBoxAddFooter.Checked)
        return;

    //create the document footer
    document.CreateFooterCanvas(50);

    // layout HTML in footer
    PdfHtml footerHtml = new PdfHtml(5, 5, @"<span style=""color:Navy; font-family:Times New Roman; font-style:italic"">
                    Quickly Create High Quality PDFs with </span><a href=""http://www.hiqpdf.com"">HiQPdf</a>", null);
    footerHtml.FitDestHeight = true;
    footerHtml.FontEmbedding = true;
    document.Footer.Layout(footerHtml);


    float footerHeight = document.Footer.Height;
    float footerWidth = document.Footer.Width;

    // add page numbering
    Font pageNumberingFont = new Font(new FontFamily("Times New Roman"), 8, GraphicsUnit.Point);
    //pageNumberingFont.Mea
    PdfText pageNumberingText = new PdfText(5, footerHeight - 12, "Page {CrtPage} of {PageCount}", pageNumberingFont);
    pageNumberingText.HorizontalAlign = PdfTextHAlign.Center;
    pageNumberingText.EmbedSystemFont = true;
    pageNumberingText.ForeColor = Color.DarkGreen;
    document.Footer.Layout(pageNumberingText);

    string footerImageFile = Application.StartupPath + @"\DemoFiles\Images\HiQPdfLogo.png";
    PdfImage logoFooterImage = new PdfImage(footerWidth - 40 - 5, 5, 40, Image.FromFile(footerImageFile));
    document.Footer.Layout(logoFooterImage);

    // create a border for footer
    PdfRectangle borderRectangle = new PdfRectangle(1, 1, footerWidth - 2, footerHeight - 2);
    borderRectangle.LineStyle.LineWidth = 0.5f;
    borderRectangle.ForeColor = Color.DarkGreen;
    document.Footer.Layout(borderRectangle);
}
See Also

Other Resources