Question

I tried to create contents in new page using doc.newPage();. It worked fine but when i tried to use the same inside the forloop it's not working. Below is my code

String first="First Page";
String second="second Page";

ByteArrayInputStream is = new ByteArrayInputStream(first.getBytes());
worker.parseXHtml(pdfWriter, doc, is);

doc.newPage();               
is=new ByteArrayInputStream(second.getBytes());
worker.parseXHtml(pdfWriter, doc, is);

This code is working fine but when I put this code in for loop it's not creating the contents in 2 pages.

ByteArrayInputStream is = null;
List<String> strList=new ArrayList<String>();
String first="First Page";
String second="second Page";
strList.add(first);
strList.add(second);
for(String string:strList)
{
   doc.newPage();                
   is=new ByteArrayInputStream(second.getBytes());
   worker.parseXHtml(pdfWriter, doc, is);
}

How to overcome this issue?

No correct solution

OTHER TIPS

Apparently adding text without HTML tags fails when you use XMLWorker. This is a known bug. For now a workaround would be to check whether a string has HTML elements or not and if it doesn't, you could wrap it in a paragraph tag.

I adjusted your sample to make it work:

List<String> strList=new ArrayList<String>();
String first="<p>First Page</p>";
String second="<p>second Page</p>";
strList.add(first);
strList.add(second);
for(String string:strList)
{
    doc.newPage();
    // I just prefer StringReaders over ByteArrayInputStreams
    worker.parseXHtml(pdfWriter, doc, new StringReader(string));
}

Also, for future reference, your code threw an Exception, it would be best if you would provide any thrown exceptions in future questions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top