We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Adding New Text to PDF is not working Java

I am using apsose.pdf v 17.2.0,


When I am using TextBuilder to add text to a page it is not working… but works when adding via a paragraph… What am I doing wrong following is my code…

<span style=“font-family: “Courier New”; font-size: 9pt;”>Document pdfDoc = <span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>new <span style=“font-family: “Courier New”; font-size: 9pt;”>Document();

DocumentInfo docInfo = pdfDoc.getInfo();


<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(128, 128, 128); font-style: italic;”>//Meeting info
<span style=“font-family: “Courier New”; font-size: 9pt;”>Page page = pdfDoc.getPages().add();


TextFragment meetingNameLabel = <span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>new <span style=“font-family: “Courier New”; font-size: 9pt;”>TextFragment(<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>“Name:”<span style=“font-family: “Courier New”; font-size: 9pt;”>);
meetingNameLabel.getTextState().setFont(FontRepository.<span style=“font-family: “Courier New”; font-size: 9pt; font-style: italic;”>findFont<span style=“font-family: “Courier New”; font-size: 9pt;”>(<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>“Arial”<span style=“font-family: “Courier New”; font-size: 9pt;”>));
meetingNameLabel.getTextState().setFontSize(<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 255);”>14<span style=“font-family: “Courier New”; font-size: 9pt;”>);
meetingNameLabel.getTextState().setFontStyle(FontStyles.<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(102, 14, 122); font-weight: bold; font-style: italic;”>Bold<span style=“font-family: “Courier New”; font-size: 9pt;”>);

TextBuilder tb = <span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>new <span style=“font-family: “Courier New”; font-size: 9pt;”>TextBuilder(page);
tb.appendText(meetingNameLabel);
<pre style=“font-family: “Courier New”; font-size: 9pt;”>pdfDoc.save(“D:/work/temp.pdf”);
<pre style=“font-family: “Courier New”; font-size: 9pt;”>
But this works<span style=“font-family: “Courier New”; font-size: 9pt;”>

<pre style=“font-family: “Courier New”; font-size: 9pt;”>Document pdfDoc = new Document();

DocumentInfo docInfo = pdfDoc.getInfo();


//Meeting info
Page page = pdfDoc.getPages().add();


TextFragment meetingNameLabel = new TextFragment(“Name:”);
meetingNameLabel.getTextState().setFont(FontRepository.findFont(“Arial”));
meetingNameLabel.getTextState().setFontSize(14);
meetingNameLabel.getTextState().setFontStyle(FontStyles.Bold);<pre style=“font-family: “Courier New”; font-size: 9pt;”>page.getParagraphs().add(meetingNameLabel);<pre style=“font-family: “Courier New”; font-size: 9pt;”>pdfDoc.save(“D:/work/temp.pdf”);<span style=“font-family: “Courier New”;”>
What am I doing wrong in the first method ?

Hi Ravi,


Thank you for contacting support. We have tested your first method of appending text in the PDF page with the latest version 17.4 of Aspose.Pdf for Java API and it works fine. We have attached an output PDF to this reply. Furthermore, you can adjust the position of text by setting the Position property of the TextFragment class as below:

[Java]
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>TextFragment meetingNameLabel = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> TextFragment(“Name:”);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
meetingNameLabel.getTextState().setFont(FontRepository.findFont(“Arial”));<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
meetingNameLabel.getTextState().setFontSize(14);
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>meetingNameLabel.getTextState().setFontStyle(FontStyles.Bold);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>meetingNameLabel.setPosition(<span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”> Position(100, 600));<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>TextBuilder tb = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> TextBuilder(page);