5

I convert HTML files to PDF format using The Flying Saucer Project. This are documents containing repetitive information - premises and their addresses, let's call them elements. At the end of a document I need to create an index. Each index entry should have a page number referring to the page where element was added. The number of elements that can fit on one page will vary.

How can I create a document index? Or how can I get notified while library adds certain type of HTML element to the PDF document?

Daniel Stefaniuk
  • 5,264
  • 2
  • 17
  • 13

2 Answers2

8

Try this:

In CSS

ol.toc a::after {  content: leader('.') target-counter(attr(href), page);}

In HTML

<h1>Table of Contents</h1>
<ol class='toc'>
<li><a href=\"#chapter1\">Loomings</a></li>
<li><a href=\"#chapter2\">The Carpet-Bag</a></li>
<li><a href=\"#chapter3\">The Spouter-Inn</a></li>
</ol>

<div id="chapter1">Loomings</div>
Rodonako
  • 81
  • 1
  • 2
  • 3
    The CSS is missing an argument. Use: `ol.toc a::after { content: leader('.') target-counter(attr(href url), page, decimal);}` See [Link](https://www.w3.org/TR/css-gcpm-3/#target-counter) for more info. – Nicklas Andersen Nov 05 '18 at 14:30
  • apparently the pageCount does not show up. It is only generating a list with a clickable link to navigate to the corresponding content. – Naxos84 Mar 29 '19 at 12:29
  • it works when just using `attr(href)` instead of `attr(href url)` and without `leader('.')` But then you get the page number right after the Link-Name. – Naxos84 Mar 29 '19 at 14:15
1

I found possible answer. You have to start playing with org.xhtmlrenderer.render.BlockBox class. A method public void layout(LayoutContext c, int contentStart) is used to place properly any HTML element in the PDF document. This method iterates through an element a few times. After the last iteration a valid page number is set.

If you mark an element you want to index, by for example using a class attribute, then you can get a page number using following code:

String cssClass = getElement().getAttribute("class");
if(!cssClass.equals("index")) {
    int pageNumber = c.getRootLayer().getPages().size();
    /* ... */
}
Daniel Stefaniuk
  • 5,264
  • 2
  • 17
  • 13