I'm attempting to create a PdfOutline to a PdfDestination to a target page after I have generated all the pages in the document. But the default initialization of PdfOutline is causing some problems.
The code sequence I use looks like this:
PdfDestination dest = new PdfDestination(PdfDestination.FITBH, 300);
PdfAction.gotoLocalPage(1, dest, writer);
PdfOutline pdfOutline = new PdfOutline(root, dest, "label");and the problem occurs (AFAICT) in PdfOutline.initOutline():
void initOutline(PdfOutline parent, String title, boolean open) {
...
setDestinationPage(writer.getCurrentPage());
}Since I'm creating the outlines after the last page is created, the call to writer.getCurrentPage() references a non-existing page.
I have attached a test program and a patch to PdfOutline that avoid the default call to setDestinationPage() when a destination is supplied already. At least it works for me, but maybe there is a better way?
regards, finn
--- PdfOutline.java.org Tue Mar 25 12:52:43 2003
+++ PdfOutline.java Tue Mar 25 12:52:57 2003
@@ -326,7 +326,10 @@
writer = parent.writer;
put(PdfName.TITLE, new PdfString(title, PdfObject.TEXT_UNICODE));
parent.addKid(this);
- setDestinationPage(writer.getCurrentPage());
+ //setDestinationPage(writer.getCurrentPage());
+ if (destination != null && !destination.hasPage())
+ setDestinationPage(writer.getCurrentPage());
+
}
/**
@@ -515,4 +518,4 @@
this.style = style;
}
-}
\ No newline at end of file
+}
import java.io.*; import com.lowagie.text.*; import com.lowagie.text.pdf.*;
public class tstoutline {
public static void main(String[] args) throws Exception {
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance(doc,
new FileOutputStream("tstoutline.pdf"));
PdfAcroForm acroForm = writer.getAcroForm();
doc.open();
doc.add(new Paragraph("Hello World"));
doc.newPage();
PdfContentByte cb = writer.getDirectContent();
PdfOutline root = cb.getRootOutline();
PdfDestination dest = new PdfDestination(PdfDestination.FITBH,
300);
PdfAction.gotoLocalPage(1, dest, writer);
PdfOutline pdfOutline = new PdfOutline(root, dest, "label");
cb.addOutline(pdfOutline);
doc.close();
}
}
