views:

231

answers:

2

I'm using xhtmlrenderer (Also known as Flying Saucer) with iText to convert HTML to PDF. How would I create bookmarks with this? Does someone with have a small example?

Thanks in advance.

+1  A: 

It seems bookmarks were added in R6, but the user guide only gives them a passing reference. with a TODO to the author to add an example later.

Searching the forums, I found this example:

<html> 
<head>  
<bookmarks>  
  <bookmark name="A bookmark" href="#bm" />  
  <bookmark name="A bookmark 2" href="#bm2" />  
  <bookmark name="A bookmark 3" href="#bm3" />    
  <bookmark name="A bookmark 4" href="#bm4" />  
  <bookmark name="A bookmark invalid" href="#bm99" />  
</bookmarks> 
</head> 
<body>  
   <div style="line-height: 100%; font-size: 12pt; page-break-before: always;">  
      <a name="bm">some text</a>  
   </div>  
   <div style="line-height: 100%; font-size: 12pt; page-break-before: always;">  
      <a name="bm2">some text</a>  
   </div>  
   <div style="line-height: 100%; font-size: 12pt; page-break-before: always;">  
     <a name="bm3">some text</a>  
   </div>  
   <div style="line-height: 100%; font-size: 12pt; page-break-before: always;">  
      <p>some text</p>  <p>some text</p>  <p>some text</p>  <p>some text</p>  <p>some text</p>  <p>some text</p>  
      <p><a name="bm4">and some more text</a></p>  
   </div> 
</body> 
</html>` 

So it seems that adding bookmarks is no more than declaring the bookmarks in the <head> and referencing them as anchors in the <body>.

This should work will with your existing XHTML->PDF conversion without requiring any code changes.

mdma
Thanks, but I don't want a header or footer on the first page and want it to begin counting from the second page. How would I alter your example to do this? Thanks.
usertest
Sorry, I don't understand. Can you tell me how the header/footer is connected with bookmarks?
mdma
Hi, ignore that last question, I got it working thanks.
usertest
Good to hear you got it working.
mdma
+1  A: 

Its correct that above approach will work.

Sample example from flyingsaucer-R8-src modified for BookMarks Demonstration

public class PDFRenderToMultiplePages {
    public static void main(String[] args) throws Exception {
        OutputStream os = null;
        try {
            final String[] inputs = new String[] { newPageHtml(1, "red"),
                    newPageHtml(2, "blue"), newPageHtml(3, "green") };
            final File outputFile = File.createTempFile("FlyingSacuer", ".pdf");
            os = new FileOutputStream(outputFile);
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocumentFromString(inputs[0]);
            renderer.layout();
            renderer.createPDF(os, false);
            BlockBox rootBox = renderer.getRootBox();
            for (int i = 1; i < inputs.length; i++) {
                renderer.setDocumentFromString(inputs[i]);
                renderer.layout();
                renderer.writeNextDocument();
            }
            renderer.finishPDF();
            System.out.println("Sample file with " + inputs.length
                    + " documents rendered as PDF to " + outputFile);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) { /* ignore */
                }
            }
        }
    }

    private static String newPageHtml(int pageNo, String color) {
        return "<html><head><bookmarks>"
                + "<bookmark name=\"bookmark"
                + pageNo
                + "\" href=\"#bookMark\"/></bookmarks></head>"
                + "<body><div style=\"color:"
                + color
                + ""
                + ";\"><a name=\"bookMark\">Book Mark Example</a></div></body></html>";
    }
}
Hi, somethings wrong with thebook marks, the above example produces - bookmark1, bookmark1, bookmark2, bookmark1, bookmark2, bookmark3 - instead of just three bookmarks. Is it something to do with the for loop? Thanks.
usertest