Viking Viking - 3 months ago 6
Java Question

ArrayList<String> in PDF from a new row

I want to send some survey in PDF from java, I tryed different methods. I use with

StringBuffer
and without, but always see text in PDF in one row.

public void writePdf(OutputStream outputStream) throws Exception {

Paragraph paragraph = new Paragraph();
Document document = new Document();
PdfWriter.getInstance(document, outputStream);

document.open();

document.addTitle("Survey PDF");

ArrayList nameArrays = new ArrayList();
StringBuffer sb = new StringBuffer();
int i = -1;

for (String properties : textService.getAnswer()) {

nameArrays.add(properties);
i++;
}
for (int a= 0; a<=i; a++){
System.out.println("nameArrays.get(a) -"+nameArrays.get(a));
sb.append(nameArrays.get(a));

}
paragraph.add(sb.toString());
document.add(paragraph);

document.close();
}


textService.getAnswer()
this -
ArrayList<String>



  • Could you please advise how to separate the text in order each new sentence will be starting from new row?



Now I see like this:enter image description here

Answer

You forgot the newline character \n and your code seems a bit overcomplicated. Try this:

StringBuffer sb = new StringBuffer();

for (String property : textService.getAnswer()) {
    sb.append(property);
    sb.append('\n');
}