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

Free Support Forum - aspose.com

TextFragment is not setting the font

Hi,

I am trying to set the font for TextFragment which I need to write in to existing pdf file. Applied font is not reflected in the final pdf.

Please find the attached image to see the difference, I applied the same font which is applied in the left side of the document(highlighted with 1) and for Confidential label written using the TextFragment as below code snippet.
if(defaultFontStr != null){
defaultFont = com.aspose.pdf.FontRepository.findFont(defaultFontStr);
}
TextFragment designation =
new TextFragment(“Confidential”);
designation.getTextState().setFontSize(fontSize);
if(defaultFont != null){
designation.getTextState().setFont(defaultFont);
}

while finding the font i dont see any error aswell.

Please help me in this regard.

Thanks,
Shivaji

Hi Shivaji,


Thanks for using our API’s.

Please share the resource PDF file and font file which you are trying to use, so that we can test the scenario inn our environment. We are sorry for this inconvenience.

Please find attached zip file which contains, source pdf and font(.ttf) file.

Hi Shivaji,


Thanks for sharing the resource files.

I have tested the scenario using Aspose.Pdf for Java 11.1.0 with following code snippet where I have used following code snippet in Eclipse Juno project running over Windows 7 (x64) and JDK 1.7 and I am unable to notice any issue. For your reference, I have also attached the output generated over my end. Can you please share some details regarding your working environment i.e. Aspose.Pdf for Java version, JDK version, Operating System etc.

[Java]

com.aspose.pdf.Document doc = new
com.aspose.pdf.Document(“C:\pdftest\source_pdf_font\Shivaji.Dole_1454441557021.pdf”); <o:p></o:p>

com.aspose.pdf.Font defaultFont = com.aspose.pdf.FontRepository.findFont("Times New Roman");

TextFragment designation = new TextFragment("Confidential");

designation.getTextState().setFontSize(12);

if(defaultFont != null)

{

designation.getTextState().setFont(defaultFont);

}

designation.getMargin().setTop(100);

designation.getMargin().setLeft(20);

doc.getPages().get_Item(1).getParagraphs().add(designation);

designation.getTextState().setForegroundColor(com.aspose.pdf.Color.getRed());

doc.save("C:\\pdftest\\source_pdf_font\\Shivaji.Dole_1454441557021_Updated.pdf");

Hi,

Thank you for the update.

We are using aspose-pdf-10.2.0.jar for writing the text to pdf.
JDK1.7 we are using.

Operating system we used is linux, where our servers are running.

When i run it locally, i can see this fonts are applied properly, but in linux machine, its different.

Please verify on linux machines and let me know.

Thanks.
Shivaji

Can you please update me on this.

Hi Shivaji,


Thanks for sharing the details.

I am working on replicating the issue in my environment and will get back to you soon.

Hi Shivaji,



Thanks for your patience.



I have tested the scenario using latest release of Aspose.Pdf for Java 11.2.0 on Non-Windows platform using JDK 1.8.0_60 and I am unable to notice any issue while adding text in Times New Roman font. Can you please try using the latest release and in case the issue still persists, please share the version of Linux which you are using, so that we can further look into this matter.



For your reference, I have also attached the output generated over my end.



[Java]

com.aspose.pdf.Document doc = new com.aspose.pdf.Document(“Shivaji.Dole_1454441557021.pdf”);

com.aspose.pdf.Font defaultFont = com.aspose.pdf.FontRepository.findFont(“Times New Roman”);

TextFragment designation = new TextFragment(“Confidential”);

designation.getTextState().setFontSize(12);

if(defaultFont != null)

{

designation.getTextState().setFont(defaultFont);

}

designation.getMargin().setTop(100);

designation.getMargin().setLeft(20);

doc.getPages().get_Item(1).getParagraphs().add(designation);

designation.getTextState().setForegroundColor(com.aspose.pdf.Color.getRed());

doc.save(“Shivaji.Dole_1454441557021_Updated.pdf”);