I'm completely newbie to PDFBox and I'm having an issue I can't find the way to solve by the moment.
I get from my database a list of folder and documents located in those folders, I iterate over all these data to generate an index with active links to the correct folder/document path. (Imagine I have a root folder and I want to have a pdf index in that root with relative links to all folders and documents contained in it)
The main code looks like follows:
try {
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDFont font = PDType1Font.HELVETICA;
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.moveTextPositionByAmount(100, 700);
contentStream.drawString("Índice " + expediente.getNombre());
contentStream.endText();
int i = 0;
for (Folder currFol : root.getFolders()) {
for (Document currDoc : currFol.getDocuments()) {
i++;
float x = 50;
float y = 250;
String text = currFol.getName() + "/" + currDoc.getName();
float textWidth = font.getStringWidth(text) / 1000 * 12;
PDAnnotationLink link = new PDAnnotationLink();
PDGamma colourBlue = new PDGamma();
colourBlue.setB(1);
link.setColour(colourBlue);
// add an action
PDActionURI action = new PDActionURI();
action.setURI(currFol.getName() + "/" + currDoc.getName());
link.setAction(action);
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.moveTextPositionByAmount(x, y);
contentStream.drawString(text);
contentStream.endText();
PDRectangle position = new PDRectangle();
position.setLowerLeftX(x);
position.setLowerLeftY(y -(i* 5));
position.setUpperRightX(x + textWidth);
position.setUpperRightY(y + 50);
link.setRectangle(position);
page.getAnnotations().add(link);
}
}
// Make sure that the content stream is closed:
contentStream.close();
document.save(output);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
My problem here is that all elements are printed ovelaped, text and boxes are over each other and by the moment can't find out how to print correctly all links in a list formatted style to create the index.
Any idea or suggestion will be very apreciated.
I tried following some tutorials, with no succes by the moment, like http://www.programcreek.com/java-api-examples/index.php?api=org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink, http://www.massapi.com/class/pd/PDAnnotationLink.html .
I tried without PDRectangle, just the text link (as PDRectangle is present in all examples I found but I don't need it really)
Thank you,