Is there a way to set multiple fonts for a cell or table or section or pdf?

Hi

I am using "aspose.pdf-3.0.1.jdk16" to display data in PDF in tabular format.

The data to be printed into a cell (String) can be US-ASCII or(and) Non-US-ASCII (Say Japanese characters).

As there are some specific fonts which can display Both ASCII as well as NON-ASCII characters, say "MS Mincho" font, I am setting font name to it using following code.

TextInfo info = new TextInfo();
info.setFontName("MS Mincho");
Cell cell = row.getCells().add(" " + data); // data is the string to be printed in cell
cell.setDefaultCellTextInfo(info);

What I want is, To set multiple font (Instaed of setting only "MS Mincho") and achieve following
- If the First font (say "Times New Roman") is available for the string (data to be put into cell) it should use it, else the second font will be used (Say "MS Mincho")
- Related rquirement is: To use font supporting ASCII if there is no Non-ASCII character.

Currently I am achieving it by following code.
TextInfo info = new TextInfo();
if (!isPureAscii(data.toString())) {
info.setFontName("MS Mincho");
}
Cell cell = row.getCells().add(" " + data);
cell.setDefaultCellTextInfo(info);
// isPureAscii(..) method
public static boolean isPureAscii(String v) {
return Charset.forName("US-ASCII").newEncoder().canEncode(v);
}

Is there a way to set multiple fonts for a cell or table or section or pdf ?

Hi Shashi,

Thanks for your interest in our products.

I tested the scenario with below sample source code using Aspose.Pdf for Java 3.1.0. Resultant PDF documents are attached for your reference. Kindly use the following code with latest version and check if it fits your need.

[Java]
Pdf pdf1 = new Pdf();
Section sec1 = pdf1.getSections().add();
Table table = new Table(sec1);
Row row = table.getRows().add();
Cell cell = row.getCells().add("Check Font Name");
TextInfo info = new TextInfo();
info.setFontName("Times New Roman");
if(info.getFontName().equals("Times New Roman"))
{
cell.getDefaultCellTextInfo().setFontName("Times New Roman");
}
else
{
cell.getDefaultCellTextInfo().setFontName("MS Mincho");
}
cell.setDefaultCellTextInfo(info);
sec1.getParagraphs().add(table);
pdf1.save("d:\\pdffiles\\TimesNewRoman.pdf");

Please feel free to contact support in case you need any further assistance.

Thanks & Regards,

Hi Rashid,

I used the code you suggested.

1. info.setFontName("Times New Roman");
2. if (info.getFontName().equals("Times New Roman")) {
3. cell.getDefaultCellTextInfo().setFontName("Times New Roman");
4. } else {
5. cell.getDefaultCellTextInfo().setFontName("MS Mincho");
6. }
7. cell.setDefaultCellTextInfo(info);

As line 1 is setting "Times New Roman" font for all 'data', so the if() at line 2 is always true.
So line 3 is always getting executed, else block is never executed.

I tested it with Japanese,chinese,Spanish,Russian,Portuguese and German.
Worked for all except Japanese and chinese character.

Explaining what I want
- My requirement is to pick appropriate font to write text into cells from the font set(or added).
- I don't know what all characters will come to be written into (ASCII, Non-ASCII etc..),
- I want to set list of font from which aspose should take appropriate font (for the text to be written into cell)

Hi Shashi,

Thanks for your feedback, as per my understanding, you want to set a specific font say "Times New Roman" or "Courier New" when inserting data in a Cell and if your data has a different font name then default "MS Machino" font should be set. Kindly correct me if I am wrong.

In the below code if you set info.setFontName("Font Name"); to any other font except "Times New Roman" or "Courier New" the default font "MS Mincho" font will be set. You can also ignore this line of code info.setFontName("Arial"); as per your requirement.

[Java]

info.setFontName("Arial");
if(info.getFontName().equals("Times New Roman"))
{
cell.getDefaultCellTextInfo().setFontName("Times New Roman");
}
else if(info.getFontName().equals("Courier New"))
{
cell.getDefaultCellTextInfo().setFontName("Courier New");
}
else
{
cell.getDefaultCellTextInfo().setFontName("MS Mincho");
}

Please feel free to contact support in case you need any further assistance.

Thanks & Regards,

shashi2587:
What I want is, To set multiple font (Instaed of setting only “MS Mincho”) and achieve following
- If the First font (say “Times New Roman”) is available for the string (data to be put into cell) it should use it, else the second font will be used (Say “MS Mincho”)

Hello Shashi,

Thanks for contacting support.

Please allow me to explain what I have understood from your requirement. As per my understanding from above statements, you need to set the font for specific table cell based over the type of contents being placed inside it. Let’s say if the contents being placed inside table cell contain any ASCII character, then any font supporting such contents should be set i.e. Times New Roman. And in case the contents being placed inside table Cell contain any ASCII character, then a font which supports ASCII characters should be applied i.e. MS Mincho.

shashi2587:
- Related rquirement is: To use font supporting ASCII if there is no Non-ASCII character.

You need to use ASCII supporting font when contents being placed inside table cell do not have any Non-ASCII character.

shashi2587:
Currently I am achieving it by following code.
TextInfo info = new TextInfo();
if (!isPureAscii(data.toString())) {
info.setFontName(“MS Mincho”);
}
Cell cell = row.getCells().add(" " + data);
cell.setDefaultCellTextInfo(info);
// isPureAscii(…) method
public static boolean isPureAscii(String v) {
return Charset.forName(“US-ASCII”).newEncoder().canEncode(v);
}

Is there a way to set multiple fonts for a cell or table or section or pdf ?

As far as I can understand from your above code snippet, you are checking the contents being placed inside table cell that either they contain any ASCII character or not. However you need to know if Aspose.Pdf for Java supports the capability to specify multiple fonts for Table/Cell/Section and in case Times New Roman is applicable, it should be applied and in case there are ASCII characters/Unicode characters, specific font should be applied automatically. Please correct me if I have not properly understood your requirement. We are really sorry for your inconvenience.