I've got a lot of data that's sorted out in tables for easy analysis. There are
some situations where I want to flag some bit of data as important. I've
noticed some odd behavior with respect to Images and Tables, and I was hoping
someone here could shed some light on this.
Below is some code that tries to display images in four ways. The first two
display both text and an image, but the third and fourth don't. Unfortunately,
because I don't know what data is going in the table until the instant I write
it, the third or fourth code segments, the ones that don't work, are the
patterns I've been having to follow...
public static void main(String args[])
{
try
{
Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream("test.pdf"));
Paragraph result = new Paragraph();
Image img = Image.getInstance("image.png");
img.setAlignment(Image.RIGHT | Image.TEXTWRAP);
// This works
Paragraph paragraph = new Paragraph();
paragraph.add(new Phrase("Some Text"));
paragraph.add(img);
paragraph.add(new Phrase("More Text"));
paragraph.add(new Phrase("Even more text"));
result.add(paragraph);
result.add(Chunk.NEWLINE);
// So does this
Table goodTable = new Table(2);
Cell cell = new Cell(img);
cell.setRowspan(2);
goodTable.addCell("Some Text");
goodTable.addCell(cell);
goodTable.addCell("More Text");
result.add(goodTable);
result.add(Chunk.NEWLINE);
// This displays only the text, not the image...
Table badTable = new Table(1);
badTable.add(paragraph);
result.addCell(badTable);
// This displays, well, nothing...
Table badTable2 = new Table(1);
Paragraph badParagraph = new Paragraph();
badParagraph.add(goodTable);
badTable2.addCell(badParagraph);
testResult.add(badTable2);
doc.open();
doc.add(result);
doc.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
This should work as-is, as long as you have an actual 'image.png' in the
directory you run this in.
Another approach I tried was adding an Anchor to identify the location as
needing an image, and processing the 'WARNING_IMAGE_NEEDED_#' anchors to add
them in after the fact, but I couldn't figure out how to get the information I
needed.
-------------------------------------------------------------------------
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/