views:

17

answers:

1

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?

A: 

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