Add text on specific position

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

Thank you for contacting support.

Please try using below code snippet in your environment and then share your kind feedback with us.

Document document = new Document();
Page page = document.getPages().add();
TextFragment textFragment1 = new TextFragment();
textFragment1.setText("This is my text #1");
textFragment1.getPosition().setXIndent(100);
textFragment1.getPosition().setYIndent(200);
//create TextParagraph object
TextParagraph par1 = new TextParagraph();
//set paragraph position
par1.setPosition(new Position(textFragment1.getPosition().getXIndent(), textFragment1.getPosition().getYIndent()));
//add new TextFragment to paragraph
par1.appendLine(textFragment1);
//add the TextParagraph using TextBuilder
TextBuilder textBuilder1 = new TextBuilder(page);
textBuilder1.appendParagraph(par1);
TextFragment textFragment2 = new TextFragment();
textFragment2.setText("This is my text #2");
textFragment2.getPosition().setXIndent(200);
textFragment2.getPosition().setYIndent(300);
//create TextParagraph object
TextParagraph par2 = new TextParagraph();
//set paragraph position
par2.setPosition(new Position(textFragment2.getPosition().getXIndent(), textFragment2.getPosition().getYIndent()));
//add new TextFragment to paragraph
par2.appendLine(textFragment2);
//add the TextParagraph using TextBuilder
TextBuilder textBuilder2 = new TextBuilder(page);
textBuilder2.appendParagraph(par2);
document.save(dataDir + "Test_19.7.pdf");

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

Hi Farhan,

Yes, it works for me… Thank you very much.

YOU ARE THE BEST!!

Hi Farhan,

Just one more question, please help.

can we do this in a while loop, because some time i will have 3 text lines or 5 text lines or 10, etc.
and they go woll on different position on the PDF document.

Document document = new Document();
Page page = document.getPages().add();
while x < y {

}
document.save(dataDir + “Test_19.7.pdf”);

@mromero_rubinrothman_com

Thank you for your kind comments.

Yes, you can use the same approach with a loop as well. Below is a code snippet for your reference:

int x[] = {100 , 200 , 300 , 400};
int y[] = {200 , 300 , 400 , 500};
Document document = new Document();
Page page = document.getPages().add();
int i = 0;
while(i<4)
{
TextFragment textFragment1 = new TextFragment();
textFragment1.setText("This is my text #" + (i+1));
textFragment1.getPosition().setXIndent(x[i]);
textFragment1.getPosition().setYIndent(y[i]);
//create TextParagraph object
TextParagraph par1 = new TextParagraph();
//set paragraph position
par1.setPosition(new Position(textFragment1.getPosition().getXIndent(), textFragment1.getPosition().getYIndent()));
//add new TextFragment to paragraph
par1.appendLine(textFragment1);
//add the TextParagraph using TextBuilder
TextBuilder textBuilder1 = new TextBuilder(page);
textBuilder1.appendParagraph(par1);
i++;
}
document.save(dataDir + "Test_19.7.pdf");
1 Like

Hi Farhan,

This is exactly what I was looking for for. . . .

" HEY… This is what I call great tech support, you are the best!!"

Thank you very much.

Marcos

1 Like

@mromero_rubinrothman_com

Thanks for sharing your kind feedback.

Please keep using our API and in case you face any issue, please feel free to let us know.