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?
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>
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();
/* ... */
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With