Search code examples
javaitextvelocity

Bookmarking in pdf generated using velocity template


I have huge data, the data will be used to generate PDF using velocity template. I have index page generated using .vm file which is a table. I should provide bookmarking from index page to other pages which is intended to.

I tried using just href in HTML.

index.vm:

<table>
<tr>
<td>
1
</td>
<td>
<a href="#go">chapter1</a>
<td>
</tr>
</table>

assembly.vm:

<table>
<tr>
<p1 id="go">assembly1</p>
</tr>
</table>

It's expected to have link in index page and on click on it, go to that respective content page.


Solution

  • Once the pdf has been generated using itext and with .vm file, while generating, store the description with page number in a map, this was acheived using this following code

    HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("Title", "INDEX");
        map.put("Action", "GoTo");
        map.put("Page", String.format("%d Fit", 8));
        ArrayList<HashMap<String, Object>> kids = new ArrayList<HashMap<String,Object>>();
    
        for(BookMark book : BookMarks) {
            HashMap<String, Object> kid = new HashMap<String, Object>();
            kid.put("Title", book.getDescription());
            kid.put("Action", "GoTo");
            kid.put("Page", String.format("%d Fit", book.getPageNumber()));
    
            ArrayList<HashMap<String, Object>> leafs = new ArrayList<HashMap<String,Object>>();
            for(BookMark books : book.getLeaf()) {
                HashMap<String, Object> leaf = new HashMap<String, Object>();
                leaf.put("Title", books.getDescription());
                leaf.put("Action", "GoTo");
                leaf.put("Page", String.format("%d Fit", books.getPageNumber()));
                leafs.add(leaf);
            }
            kid.put("Kids", leafs);
            kids.add(kid);
        }
    
        map.put("Kids", kids);
        ArrayList<HashMap<String, Object>> outlines = new ArrayList<HashMap<String,Object>>();
        outlines.add(map);
        PdfReader reader = new PdfReader(env.getProperty("path.generated.pdf").concat(fileName));
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(env.getProperty("path.generated.pdf").concat(catalogInfo.getCatalogName().trim().concat("raw1")).concat(".pdf")));
        stamper.setOutlines(outlines);
        stamper.setFullCompression();
        stamper.close();
        reader.close();
        File file = new File(env.getProperty("path.generated.pdf").concat(fileName)); 
        file.delete();