Situation (my task): I need calculate table of contents. My process look like data -> java classes -> xml -> xslt -> html -> parsing to ElementList -> analize element list -> making pdf. On the stage 'xslt' I add marker for points of (future) table contents. On the stage 'analize element list' I make table of contents and clear markers in the Element of ElementList. Up to this point there are no problems. For correct location points page number I have to form pdf several times. The problem occurs when I create a second (or more) time pdf. It looks like the absence of some content (it can be text and / or pictures) in the tables - not all of them disappear.
After first pdf making all content is present. If use second instance ElementList with parsed elements for second pdf making content correct.
What's a problem in resusing ElementList and how fix it ?
ps. itext-5.5.11 + xmlparser-5.5.11
pps. sorry for my poor English
private void test() throws DocumentException, IOException {
// step 1: get parsed elements
ElementList list = contentElement.get(BODY).getElements();
// step 2: bytes for first pdf
byte[] bytes = getBytes(list);
// step 3: file 1
File out = new File("1.pdf");
FileOutputStream fos = new FileOutputStream(out);
fos.write(bytes);
fos.close();
// step 4: reuse ElementList
bytes = getBytes(list);
// step 5: file 2
out = new File("2.pdf");
fos = new FileOutputStream(out);
fos.write(bytes);
fos.close();
// Why will it be two different file ?
}
private byte[] getBytes(ElementList list) throws DocumentException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Rectangle page = new Rectangle(PageSize.A4);
page = page.rotate();
Document doc = new Document(page, 56, 42, 10, 10);
Rectangle body = new Rectangle(56, 60, 800, 525);
writer = PdfWriter.getInstance(doc, baos);
writer.setPageEvent(this);
doc.open();
ColumnText text = new ColumnText(writer.getDirectContent());
for (Element element : list) {
text.setText(null);
text.addElement(element);
boolean makeNewPage = ColumnText.hasMoreText(text.go());
while (makeNewPage) {
doc.newPage();
text.setSimpleColumn(body);
makeNewPage = ColumnText.hasMoreText(text.go());
}
}
doc.close();
byte[] bytes = baos.toByteArray();
baos.close();
return bytes;
}
ColumnTextobject, and you render the elements, then those elements are gone.ColumnTextcan be used directly, or indirectly. As you don't show any of your code, nor tell us which version of iText you are using, it is hard to give you a more accurate answer. - Bruno Lowagie