How to create TOC or index using the Flying Saucer project?
Asked Answered
C

2

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?

Cloche answered 11/10, 2010 at 10:10 Comment(0)
C
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>
Coulson answered 28/1, 2015 at 13:26 Comment(3)
The CSS is missing an argument. Use: ol.toc a::after { content: leader('.') target-counter(attr(href url), page, decimal);} See Link for more info.Paschasia
apparently the pageCount does not show up. It is only generating a list with a clickable link to navigate to the corresponding content.Erythro
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.Erythro
C
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();
    /* ... */
}
Cloche answered 11/10, 2010 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.