Print MultiLine text from a List

Hi,


I need to append a page in existing pdf and need to print text from a list. I need to print every list item on a new line. I could not figure out any way other than using TextFragment and then finding out position. Is there a easier and better way to perform this operation

Regards,

Hi Rajeev,

Thanks for your inquiry. As per my understanding you want to add each list item text on new line without specifying the text position in a new page. Please check the following code to add multiline text on a page without setting its position.

    Document pdfDocument = new Document();
    Page pdfPage = pdfDocument.getPages().add();
    String test = "Aspose.Pdf for Java" + System.getProperty("line.separator") + "is a PDF document creation component that enables your Java applications" + System.getProperty("line.separator") + "to read, write and manipulate PDF documents without using Adobe Acrobat.";
    pdfPage.getParagraphs().add(new TextFragment(test));
    TextFragment textFragment = new TextFragment("Aspose.Pdf for Java \r\nis a PDF document creation component that enables your Java \r\napplications \r\nto read, write and manipulate PDF documents without using Adobe Acrobat.");
    pdfPage.getParagraphs().add(textFragment);

    ////save output file
    pdfDocument.save(myDir + "LineBreak_output.pdf");

Please feel free to contact us for any further assistance.

Best Regards,

Hello Tech-Support:
Can you please help me with a sample code for: aspose.java for PDF

I need to write several simple lines of text on a PDF document.

for example:
I need to write text “This is my text #1” on position (100, 200)
then write another text “This is my text #2” on position (200, 300)
then write another text “This is my text #3” on position (300, 400)

There is a sample code on your web-site but it only writes a single line of text
but when i try to write another line it does not work.

Please help.
Thank you very much!!
Marcos

@mromero_rubinrothman_com

Please use following code snippet in order to add text at different positions:

Document doc = new Document();
Page page = doc.getPages().add();
TextFragment tf1 = new TextFragment("“This is my text #1” on position (100, 200)");
tf1.setPosition(new Position(100, 200));
TextFragment tf2 = new TextFragment("“This is my text #2” on position (200, 300)");
tf2.setPosition(new Position(200, 300));
TextFragment tf3 = new TextFragment("“This is my text #3” on position (300, 400)");
tf3.setPosition(new Position(300, 400));
page.getParagraphs().add(tf1);
page.getParagraphs().add(tf2);
page.getParagraphs().add(tf3);
doc.save(dataDir + "textposition.pdf");

textposition.pdf (2.0 KB)

WOW!!! thank you that looks great.
However I just need one more change.

the number of lines of text may be variable we may need 2 or 5 or 10, etc.
can we do this in a while loop?
it does not work for me, please help…

TextFragment tf = new TextFragment();
while x < y{
Text dbFieldText = getMyText(x,y);

 tf = new TextFragment( dbFieldText );
 tf.setposition(x, y);

page.getParagraphs().add(tf);
}

@mromero_rubinrothman_com

Would you kindly share the issue which you are facing while adding text in while loop? Are you facing some exception or resultant PDF does not has all text?