Question

I am creating PDF using Itext and xmlworker. My problem is I want to create the contents in new pages. Below is my code.

File file = new File("D:/PDFFiles/Sathesh.pdf");
FileOutputStream fos=new FileOutputStream(file);
Document doc=new Document(PageSize.A4, 50, 50, 70, 70);
PdfWriter pdfWriter=PdfWriter.getInstance(doc, fos);
doc.open();
XMLWorkerHelper worker=XMLWorkerHelper.getInstance();
String firstString="<table><tr><td>First Page</td></tr></table>" ;
String secondString="<table><tr><td>Second Page</td></tr></table>" ; 
String final=firstString+secondString;
ByteArrayInputStream is = new ByteArrayInputStream(final.getBytes());
worker.parseXHtml(pdfWriter, doc, is);
doc.close();
fos.close();

I want the firstString in first page and secondString in page. Waiting for your answer.

Was it helpful?

Solution

Parse the Strings separately and add a call to newPage() in between.

Something like this:

...
String firstString="<table><tr><td>First Page</td></tr></table>" ;
String secondString="<table><tr><td>Second Page</td></tr></table>" ;
ByteArrayInputStream is = new ByteArrayInputStream(firstString.getBytes());
worker.parseXHtml(pdfWriter, doc, is);
doc.newPage();
is = new ByteArrayInputStream(secondString.getBytes());
worker.parseXHtml(pdfWriter, doc, is);
doc.close();
fos.close();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top