How can I create a table in HTML and add it to a PDF?

176 views
Skip to first unread message

Andrey Safonov

unread,
Feb 20, 2019, 1:34:00 PM2/20/19
to PDFTron PDFNet SDK
Q: How can I create a table and add it to a PDF?

A: On Windows, Mac OS, and Linux you can use the following code to add arbitrary HTML code to a PDF page:


//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2018 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

using System;
using System.IO;

using pdftron;
using pdftron.Common;
using pdftron.Filters;
using pdftron.SDF;
using pdftron.PDF;


namespace HTML2PDFTestCS
{
    //---------------------------------------------------------------------------------------
    // The following sample illustrates how to stamp HTML string onto a PDF.
    // 
    // 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be 
    // used to convert HTML web pages into PDF documents by using an external module (html2pdf).
    //
    // html2pdf modules can be downloaded from http://www.pdftron.com/pdfnet/downloads.html.
    //---------------------------------------------------------------------------------------
    class HTML2PDFSample
    {
        private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
        static HTML2PDFSample() { }

        static void Main(string[] args)
        {
            string output_path = "../../../../TestFiles/Output/";
            string input_path = "../../../../TestFiles/";

            PDFNet.Initialize();
            HTML2PDF.SetModulePath("../../../../../Lib");

            // Convert HTML string to PDF and then stamp it

            //HTML Strings
            string html = "<html> <head> <style> table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; } </style> </head> <body> <h2>HTML Table</h2> <table> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>Ernst Handel</td> <td>Roland Mendel</td> <td>Austria</td> </tr> <tr> <td>Island Trading</td> <td>Helen Bennett</td> <td>UK</td> </tr> <tr> <td>Laughing Bacchus Winecellars</td> <td>Yoshi Tannamuri</td> <td>Canada</td> </tr> <tr> <td>Magazzini Alimentari Riuniti</td> <td>Giovanni Rovelli</td> <td>Italy</td> </tr> </table> </body> </html>";

            //Initialize doc and stamper
            PDFDoc input_doc = new PDFDoc(input_path + "newsletter" + ".pdf");
            Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, 1, 1);

            //Configure stamper options
            s.SetOpacity(1);
            s.SetRotation(0);
            s.SetPosition(0.5, 0.5);
            s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_left, Stamper.VerticalAlignment.e_vertical_bottom);

            //Create a page set where stamp should be applied, for example on every other page
            PageSet dest_pages = new PageSet(1, input_doc.GetPageCount(), PageSet.Filter.e_even);

            //Stamp input document with HTML string
            StampHtml(s, ref input_doc, html, dest_pages);

            //Save the resulting doc
            input_doc.Save(output_path + "HTML_STAMP_Test" + ".pdf", SDFDoc.SaveOptions.e_linearized);

        }

        static void StampHtml(Stamper stamp, ref PDFDoc dest_doc, string html, PageSet dest_pages)
        {
            try
            {
                using (PDFDoc doc = new PDFDoc())
                {
                    HTML2PDF converter = new HTML2PDF();

                    //Convert the HTML
                    converter.InsertFromHtmlString(html);

                    //Verify the conversion results
                    if (converter.Convert(doc)) Console.WriteLine("Converted html successfully.");
                    else Console.WriteLine("Conversion failed. HTTP Code: {0}\n{1}", converter.GetHTTPErrorCode(), converter.GetLog());

                    dest_doc.InitSecurityHandler();
                    doc.InitSecurityHandler();

                    //Get page one of the HTML document
                    Page page_one = doc.GetPage(1);

                    //Stamp the input document with HTML 
                    stamp.StampPage(dest_doc, page_one, dest_pages);
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }
        }


    }
}
Reply all
Reply to author
Forward
0 new messages