Viking
Viking

Reputation: 177

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>

Now I see like this:enter image description here

Upvotes: 1

Views: 1768

Answers (4)

Sanush
Sanush

Reputation: 325

This method is for save values in array list into a pdf document. In the mfilePath variable "/" in here you can give folder name. As a example "/example/".

and also for mFileName variable you can use name. I give the date and time that document will created. don't give static name other vice your values are overriding in same pdf.

private void savePDF() {

    com.itextpdf.text.Document mDoc = new com.itextpdf.text.Document();

    String mFileName = new SimpleDateFormat("YYYY-MM-DD-HH-MM-SS", Locale.getDefault()).format(System.currentTimeMillis());

    String mFilePath = Environment.getExternalStorageDirectory() + "/" + mFileName  + ".pdf";


    try
    {
        PdfWriter.getInstance(mDoc, new FileOutputStream(mFilePath));

        mDoc.open();

        for(int d = 0; d < g; d++)
        {
            String mtext = answers.get(d);

            mDoc.add(new Paragraph(mtext));

        }

        mDoc.close();

    }
    catch (Exception e)
    {

    }

}

Upvotes: 0

GhostCat
GhostCat

Reputation: 140613

You might be able to fix that by simply appending "\n" to the strings that you collecting in your list; but I think: that very much depends on the PDF library you are using.

You see, "newlines" or "paragraphs" are to a certain degree about formatting. It seems like a conceptual problem to add that "formatting" information to the data that you are processing.

Meaning: you might want to check if your library allows you to provide strings - and then have the library do the formatting for you!

In other words: instead of giving strings with newlines; you should check if you can keep using strings without newlines, but if there is way to have the PDF library add line breaks were appropriate.

Side note on code quality: you are using raw types:

ArrayList nameArrays = new ArrayList();

should better be

ArrayList<String> names = new ArrayList<>();

[ I also changed the name - there is no point in putting the type of a collection into the variable name! ]

Upvotes: 1

torkleyy
torkleyy

Reputation: 1227

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');
}

Upvotes: 2

Shahid
Shahid

Reputation: 2330

What about:

nameArrays.add(properties+"\n");

Upvotes: 2

Related Questions