Text goes beyond cell definition when text includes certain characters

Hallo

I am using aspose pdf in order to fill in text in a table. I noticed that sometimes the text goes beyond the cell definition. I noticed that this happens when the text contains Japanese characters, for example. Text containing only English characters does not suffer from this problem, even inside the same table.

The code snippet below demonstrates this issue. There is one table and 2 cells with a lot of text. The text from the second cell contains some Japanese characters. The result is saved in the attachment: “Result.pdf”.

List inputStringList = new ArrayList<>();
inputStringList.add(“asd as ADFWgsfgs df fg43 df a dad a dsasd as ADFWgsfgs df fg43 df a dad a dsasd as ADFWgsfgs df fg43 df a dad a dsasd as ADFWgsfgs df fg43 df a dad a END”);
inputStringList.add(“The signer signer1 in signing package 昨夜のコンサートは最高でし has been manually authenticated. Passport authentication was completedEND”);

com.aspose.pdf.Document doc = new com.aspose.pdf.Document(“Blank.pdf”);
com.aspose.pdf.Table table = new com.aspose.pdf.Table();
table.getDefaultCellTextState().setFont(com.aspose.pdf.FontRepository.findFont(“Helvetica”));
table.setColumnWidths(“400”);

for (String currentString : inputStringList) {
Row row = table.getRows().add();
row.getCells().add(currentString);
}

doc.getPages().get_Item(1).getParagraphs().add(table);
doc.save(“Result.pdf”);

I noticed that removing the default font solves the problem, but I also need the table default font.
The same behavior is present under Windows and Linux.
The version of aspose.pdf is 11.8.0, but the problem is still present in the latest version.
Is this a bug? Is there any way to tweak the code in order to fix this problem?

Thank you,
Samuel

Hi Samuel,


Thanks for using our API’s.

In order to display non-English characters such as Japanese, you need to use the font which supports UniCode. The Arial Unicode MS is the font which supports most of the non-English characters and Helvetica may not be supporting it.

I have tested the scenario with “Arial Unicode MS” font and it is working fine. I have attached output PDF document for your reference as well.

JAVA

List inputStringList = new ArrayList<>();
inputStringList.add(“asd as ADFWgsfgs df fg43 df a dad a dsasd as ADFWgsfgs df fg43 df a dad a dsasd as ADFWgsfgs df fg43 df a dad a dsasd as ADFWgsfgs df fg43 df a dad a END”);
inputStringList.add(“The signer signer1 in signing package 昨夜のコンサートは最高でし has been manually authenticated. Passport authentication was completedEND”);
com.aspose.pdf.Document doc = new com.aspose.pdf.Document(“Blank.pdf”);
com.aspose.pdf.Table table = new com.aspose.pdf.Table();

table.setDefaultCellBorder(new BorderInfo(BorderSide.All, 0.1F));
table.getDefaultCellTextState().setFont(com.aspose.pdf.FontRepository.findFont(“Arial Unicode MS”));

table.setColumnWidths(“200 200”);
com.aspose.pdf.MarginInfo margin = new com.aspose.pdf.MarginInfo();
margin.setTop(5f);
margin.setLeft(5f);
margin.setRight(5f);
margin.setBottom(5f);
//Set the default cell padding to the MarginInfo object
table.setDefaultCellPadding(margin);
for (String currentString : inputStringList) {
Row row = table.getRows().add();
row.getCells().add(currentString);
}

doc.getPages().get_Item(1).getParagraphs().add(table);
doc.save(“Result12.pdf”);

Please feel free to contact us for any further assistance.

Best Regards,

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #3933ff}

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px ‘Helvetica Neue’; -webkit-text-stroke: #000000}
span.s1 {font-kerning: none}

Hallo

This is working under Windows. What would be a recommended font under Linux for such a case?

Thank you,
Samuel

Hi Samuel,

Thanks for sharing further details. It seems Aspose.Pdf for Java is unable to find fonts on your system. Please note on non-Windows OSs Aspose.Pdf for Java looks fonts in system default font path or specified local font path for custom font directory.

Please note most of the PDF documents that we convert are created by people using Windows or Mac OS operating systems with fonts that are installed with Microsoft Windows or with Microsoft Office. To resolve your issue either you can install Microsoft fonts on your system or copy fonts from your windows OS and paste to your system default font path.

Furthermore, if you want to use custom fonts from other than system default font path then you need to add that folder path into LocalFontPath as following. You can use following methods to get system folder of fonts or set font path to font folders.

  • Document.getLocalFontPath () - shows the system folder in which project will look for fonts.
  • Document.setLocalFontPath (String) - Setting font path to custom folder
// Set font folder path
String path = "/home/fahad/fonts/";

// Adding a single font directory
// com.aspose.pdf.Document.addLocalFontPath(path);

// Setting the user list for standard font directories
java.util.List list = com.aspose.pdf.Document.getLocalFontPaths();
list.add(path);
com.aspose.pdf.Document.setLocalFontPaths(list);

We are sorry for the inconvenience caused.