Hi,
I'm using the genericTag functionality (as described in chapter 4.6 of THE
book) for being able to do custom drawings (DirectContent) in some cells of
my tables.
For that, I insert a Chunk in those cells and I set a genericTag on them. When
the event listener receives the onGenericTag event, it adds a line across the
chunk.
I'm OK with the fact that the chunk's content is drawn after the onGenericTag
event is called. But my problem is that the cell's background is drawn after
the onGenericTag event is called, overwriting all the nice stuff I've drawn
during that event.
In attachment, you'll find a little piece of code illustrating that. In the
generated PDF, the second table is missing the blue diagonal because it is
overwritten by the background.
IMHO, it's a bug. What do you think?
Regarding this very subject, I miss a few other functionalities:
- it would be cool to have an event sent after the chunk is drawn
- it would be cool to have the setGenericTag method on the Element class
instead of only the Chunk class.
CU and thanks
--
Patrick Valsecchi
Senior Software Engineer
Camptocamp Suisse SA
http://www.camptocamp.com
+41 21 619 1030
package org.mapfish.print;
import com.lowagie.text.*;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.*;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.awt.*;
public class BugCellBackground {
public static void main(String[] args) throws FileNotFoundException, DocumentException {
PdfPageEvent events= new MyPdfPageEvent();
OutputStream outFile=new FileOutputStream("/tmp/bugCellBackground.pdf");
Document doc = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(doc, outFile);
writer.setPageEvent(events);
doc.open();
doIt(doc, false);
doIt(doc, true);
doc.close();
writer.close();
}
private static void doIt(Document doc, boolean withBackground) throws DocumentException {
PdfPTable table=new PdfPTable(1);
Chunk chunk=new Chunk("Hello");
chunk.setGenericTag("diag");
Paragraph paragraph = new Paragraph(chunk);
paragraph.setAlignment(Paragraph.ALIGN_CENTER);
PdfPCell cell=new PdfPCell();
cell.setBorderWidth(1);
cell.addElement(paragraph);
if(withBackground)
cell.setBackgroundColor(Color.red);
table.addCell(cell);
table.setSpacingAfter(10);
doc.add(table);
}
private static class MyPdfPageEvent extends PdfPageEventHelper {
public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
if(text.equals("diag")) {
PdfContentByte dc = writer.getDirectContent();
dc.saveState();
dc.setColorStroke(Color.blue);
dc.moveTo(rect.getLeft(), rect.getTop());
dc.lineTo(rect.getRight(), rect.getBottom());
dc.stroke();
dc.restoreState();
}
}
}
}
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.1t3xt.com/docs/book.php