ITextSharp repeats PDF table header using external css file

I have an HTML table in a view. I use ITextSharp to convert HTML to PDF. I want to use external CSS and repeat the table title for each page. How can I show the title on each page using CSS and the page title?

This means the page title

str.Append("<table class='text-center'>");
                    str.Append("<tr class='h4'><td> " + companyName + " </ td ></ tr >");
                    str.Append("</table>");

And this is to show my HTML

TextReader xmlString = new StringReader(str.Append(html).ToString());

And to add my CSS

ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssResolver.AddCssFile(HttpContext.Current.Server.MapPath(@"~/Content/Print.css"), true);
IPipeline pipeline = new CssResolverPipeline(cssResolver,
    new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));

var worker = new XMLWorker(pipeline, true);
var xmlParse = new XMLParser(true, worker);
xmlParse.Parse(xmlString);
xmlParse.Flush();

This code works fine, but I use 1 table to display the data, and the table title is not repeated for every page. If I use this code to repeat table headings, CSS doesn't Work. How can I solve this problem using both CSS and a repeating header.

using (TextReader sr = new StringReader(html))
{
    List<IElement> elements =
      iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, null);
    foreach (IElement el in elements)
    {
        if (el is PdfPTable)
        {
            ((PdfPTable)el).HeaderRows = 2;
        }
        document.Add(el);
        xmlParse.Parse(sr);
    }
}
+4
1

XML Worker CSS: repeat-header repeat-footer. CSS. XML Worker .

<thead> <tfoot>.

<table style="repeat-header: yes; repeat-footer: yes;">
  <tfoot>
    <tr>
      <td>Footer 1</td>
    </tr>
  </tfoot>
  <thead>
    <tr>
      <td>Header 1</td>
    </tr>
    <tr>
      <td>Header 2</td>
    </tr>
  </thead>
  <tbody>
    <!-- ... -->
  </tbody>
</table>

HTMLWorker. . XMLWorker.

XMLWorker XMLParser, CSS repeat-header, HTML.

+11

Source: https://habr.com/ru/post/1612446/


All Articles