It's not a bug, you have to write to the right place. See the modified code:
Document document = new Document(PageSize.A4, 50, 50, 70, 70);
PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream("c:\\test.pdf"));
TestEvent te = new TestEvent();
writer.setPageEvent(te);
document.open();
PdfPTable table = new PdfPTable(1);
Chunk chunk = new Chunk(" ");
chunk.setGenericTag("test");
Phrase phrase = new Phrase();
phrase.add(chunk);
Paragraph paragraph = new Paragraph();
paragraph.add(phrase);
PdfPCell cell = new PdfPCell(paragraph);
cell.setCellEvent(te);
cell.setBackgroundColor(new Color(0,255,0));
table.addCell(cell);
document.add(table);
document.close();
public static class TestEvent extends PdfPageEventHelper implements
PdfPCellEvent {
PdfTemplate tpl;
BaseFont font;
Rectangle tagPosition;
public void onGenericTag(PdfWriter pdfWriter, Document document, Rectangle
rectangle, String string) {
tagPosition = rectangle;
}
public void onCloseDocument(PdfWriter pdfWriter, Document document) {
try {
font = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false);
String text = "" + (pdfWriter.getPageNumber() - 1);
float fontSize = 10;
tpl.beginText();
tpl.setFontAndSize(font, 10);
tpl.setTextMatrix(0, 0);
tpl.showText(text);
tpl.endText();
} catch (Exception e) {
e.printStackTrace();
}
}
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[]
canvases) {
PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
tpl = cb.createTemplate(10, 10);
cb.addTemplate(tpl, tagPosition.left(), tagPosition.bottom());
}
}
Paulo
________________________________
From: [EMAIL PROTECTED] on behalf of Eric Summkeller
Sent: Fri 15-Dec-06 17:56
To: [email protected]
Subject: [iText-questions] Bug in PdfPCell
I added a Paragraph with a PdfPTemplate into a PdfPCell and after that I
changed the color of the PdfPCell. The result was that I did't see the content
of the template in the cell.
Any help welcome!
Code example:
Testclass:
public class Test {
public static void main(String[] args)
{
Document document = new Document(PageSize.A4, 50, 50, 70, 70);
try {
PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream("test.pdf"));
writer.setPageEvent(new TestEvent());
document.open();
PdfPTable table = new PdfPTable(1);
Chunk chunk = new Chunk(" ");
chunk.setGenericTag("test");
Phrase phrase = new Phrase();
phrase.add(chunk);
Paragraph paragraph = new Paragraph();
paragraph.add(phrase);
PdfPCell cell = new PdfPCell(paragraph);
cell.setBackgroundColor(new Color(0,255,0));
table.addCell(cell);
document.add(table);
document.close();
}
catch (Exception de) {
de.printStackTrace();
}
}
}
Testevent:
public class TestEvent extends PdfPageEventHelper {
PdfTemplate tpl;
BaseFont font;
public void onGenericTag(PdfWriter pdfWriter, Document document, Rectangle
rectangle, String string) {
PdfContentByte cb = pdfWriter.getDirectContent();
tpl = cb.createTemplate(10, 10);
cb.addTemplate(tpl, rectangle.left(), rectangle.bottom());
}
public void onCloseDocument(PdfWriter pdfWriter, Document document) {
try {
font = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false);
String text = "" + (pdfWriter.getPageNumber() - 1);
float fontSize = 10;
tpl.beginText();
tpl.setFontAndSize(font, 10);
tpl.setTextMatrix(0, 0);
tpl.showText(text);
tpl.endText();
} catch (Exception e) {
e.printStackTrace();
}
}
}
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/
Aviso Legal:
Esta mensagem é destinada exclusivamente ao destinatário. Pode conter
informação confidencial ou legalmente protegida. A incorrecta transmissão desta
mensagem não significa a perca de confidencialidade. Se esta mensagem for
recebida por engano, por favor envie-a de volta para o remetente e apague-a do
seu sistema de imediato. É proibido a qualquer pessoa que não o destinatário de
usar, revelar ou distribuir qualquer parte desta mensagem.
Disclaimer:
This message is destined exclusively to the intended receiver. It may contain
confidential or legally protected information. The incorrect transmission of
this message does not mean the loss of its confidentiality. If this message is
received by mistake, please send it back to the sender and delete it from your
system immediately. It is forbidden to any person who is not the intended
receiver to use, distribute or copy any part of this message.
<<winmail.dat>>
------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions Buy the iText book: http://itext.ugent.be/itext-in-action/
