Hello everybody,
I am a newly subscriber to this mailing list but I have been using Itext
since version 0.9.
I am facing 2 problems with generating a TOC with PdfCopy:
I cannot get this to work
PdfAction action = PdfAction.gotoLocalPage(page ,new
PdfDestination(PdfDestination.XYZ, -1, 10000, 0), copy)
<Chunk>.setAction(action);
No errors but links do not work, stay on the TOC page
If I do
PdfAction action = new PdfAction (PdfAction. FIRSTPAGE) ;
<Chunk>.setAction(action);
All links go normally to first page
What's more if if do
PdfAction action = PdfAction.gotoLocalPage(page ,new
PdfDestination(PdfDestination.XYZ, -1, 10000, 0), copy)
<Chunk>.setAction(action);
if(page == 56){
<PdfCopy>.setOpenAction(action);
}
Document opens on page 56
Also whatever I am doing in
2Nd problem phrase.setLeading(0.2f);
or phrase.setLeading(2);
or phrase.setLeading(20);
Line spacing in TOC remains desperately the same.
What am I am missing? Any help will be greatly appreciated.
Hubert
Here's the relevant code
Construction of the TOC paragraph (using a TreeMap <alpha_indexes>)
Paragraph paragraph = new Paragraph();
Phrase phrase;
for (Object key : alpha_indexes.keySet())
{
phrase = new Phrase();
phrase.setLeading(2);
Chunk ch = new Chunk((String) key, smallfontbold);
phrase.add(ch);
ch = new Chunk("\n", smallfontbold);
phrase.add(ch);
phrase.setLeading(2);
paragraph.add(phrase);
phrase = new Phrase();
phrase.setLeading(2);
for (Object keyname : ((TreeMap)
alpha_indexes.get(key)).keySet())
{
ch = new Chunk((String) keyname , smallfont);
int page = ((Integer) ((TreeMap)
alpha_indexes.get(key)).get(keyname));
PdfAction action = PdfAction.gotoLocalPage(page ,new
PdfDestination(PdfDestination.XYZ, -1, 10000, 0), copy);
ch.setAction(action);
phrase.add(ch);
ch = new Chunk(new VerticalPositionMark() , 185 ,
false);
phrase.add(ch);
ch = new Chunk("page " + page , smallfont);
phrase.add(ch);
ch = new Chunk("\n" , smallfont);
phrase.add(ch);
}
paragraph.add(phrase);
}
makeIndexes(copy, paragraph, indexvierge);
Method makeIndexes based on an example from Paolo Soares
private void makeIndexes(PdfCopy copy, Paragraph paragraph, String
pagevierge) throws Exception
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfReader reader = new PdfReader(pagevierge);
PdfStamper stamper = new PdfStamper(reader, baos);
PdfContentByte pdfcontentbyte = stamper.getOverContent(1);
ColumnText ct = new ColumnText(pdfcontentbyte);
ct.addText(paragraph);
ct.setIndent(0);
// Column ones's left and right borders
float[] left1 =
{80, 1050, 80, 51};
float[] right1 =
{320, 1050, 320, 51};
// Column two's left and right border
float[] left2 =
{325, 1050, 325, 51};
float[] right2 =
{565, 1050, 565, 51};
int status = 0;
int column = 0;
// While there's still more text to print out
while ((status & ColumnText.NO_MORE_TEXT) == 0)
{
// Set left column points
if (column == 0)
{
ct.setColumns(left1, right1);
column = 1;
}
// Else set right column points
else
{
ct.setColumns(left2, right2);
column = 0;
}
// Set top part of columns
ct.setYLine(800);
// Set alignment
ct.setAlignment(Element.ALIGN_LEFT);
status = ct.go();
// Go to a new page
if ((column == 0) && ((status & ColumnText.NO_MORE_COLUMN) !=
0))
{
stamper.close();
reader = new PdfReader(baos.toByteArray());
copy.addPage(copy.getImportedPage(reader, 1));
copy.freeReader(reader);
baos = new ByteArrayOutputStream();
reader = new PdfReader(pagevierge);
stamper = new PdfStamper(reader, baos);
pdfcontentbyte = stamper.getOverContent(1);
ct.setCanvas(pdfcontentbyte);
}
}
// Print last bits
ct.go();
stamper.close();
reader = new PdfReader(baos.toByteArray());
copy.addPage(copy.getImportedPage(reader, 1));
copy.freeReader(reader);
}
------------------------------------------------------------------------------
RSA(R) Conference 2012
Save $700 by Nov 18
Register now
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php