Free Support Forum - aspose.com

Rendering of Custom Fonts in Word Document

中文乱码.png (14.2 KB)
Under Linux, this is the problem I encountered. Your problem should be similar to mine. Is your problem solved? I am also seeking help.

@pang679,

Have you tried the latest version of Aspose.Words for .NET i.e. 19.11 on your end? In case the problem still remains, please ZIP and upload your input Word document and Aspose.Words generated output document showing the undesired behavior here for testing. We will then investigate the issue on our end and provide you more information.

I tested in a Java environment
my generated output picture
中文乱码.jpg (64.6 KB)
my Word document
中文乱码.zip (10.1 KB)

I tried the code but it didn’t work
LoadOptions opt = new LoadOptions();
opt.setEncoding(Encoding.CHARSET_GB2312);
Document doc = new Document(inputstream,opt);
FontInfoCollection fonts = doc.getFontInfos();
fonts.setEnbedTrueTypeFonts(true);
fonts.setEmbedSystemFonts(false);
fonts.setSaveSusetFonts(false);
FontSettings fontsettings = FontSettings.getDefaultInstance();
fontsettings.setFontsFolder("/static/font/chinese/",true);
doc.setFontSettings(fongsettings);
doc.save(outputstream,SaveFormat.PNG);

thanks

@pang679,

Please copy the following fonts from Windows machine and place them inside a custom folder in Linux environment and then instruct Aspose.Words for Java to use that folder containing fonts during rendering Word documents to Images/PDF etc.

  • Calibri
  • 宋体

Please also refer to the following section of documentation:
How to Specify True Type Fonts Location

Hope, this helps.

I tried using the code but it didn’t work?
Is there any other way?
docx document
中文乱码.zip (10.1 KB)
converted jpg file
中文乱码.jpg (64.6 KB)

LoadOptions opt = new LoadOptions();
opt.setEncoding(Encoding.CHARSET_GB2312);
Document doc = new Document(inputstream,opt);
FontInfoCollection fonts = doc.getFontInfos();
fonts.setEnbedTrueTypeFonts(true);
fonts.setEmbedSystemFonts(false);
fonts.setSaveSusetFonts(false);
FontSettings fontsettings = FontSettings.getDefaultInstance();
fontsettings.setFontsFolder("/static/font/chinese/",true);
doc.setFontSettings(fongsettings);
doc.save(outputstream,SaveFormat.PNG);

/static/font/chinese/ This folder is the required fonts under my application

thanks!!!

@pang679,

Please also use the code from the following article to see if you get any notifications of missing fonts on your end?

Please also make sure that the absolute path you specify in setFontsFolder is correct.

Must setFontFolder be an absolute path?

@pang679,

Yes, please provide absolute path to the fonts folder inside setFontsFolder method. Hope, this helps.

Document doc = new Document("E:\\temp\\205152\\in.docx");
FontSettings fs = new FontSettings();
File f = new File("");
fs.setFontsFolder(f.getAbsolutePath() + "\\static\\fonts\\", true);
doc.setFontSettings(fs);
doc.save("E:\\temp\\205152\\19.11-static.pdf");

@ awais.hafeez
The project deployed to the Linux server should be run as a jar file. Can the code you give get the absolute path? I have also consulted your people before, he said that relative and absolute paths can be used. I used classLoader.getSystemResource (“static / font / chinese /”). GetPath () to get the absolute path and deploy it to the linux server. Use new ClassResource (“static / font / chinese /”) to get the relative path to deploy to the server or it is garbled, how to solve it?

@pang679,

If my previous code is working fine on your development machine then it should also work when deployed as a JAR file i.e. f.getAbsolutePath() + "\\static\\fonts\\" should calculate the absolute path to fonts folder. You can also print the path on console window to verify what actual path is being calculated after deployment.

System.out.println(f.getAbsolutePath() + "\\static\\fonts\\");

You may also try experimenting as to how different standard Java APIs can help you in calculating the absolute path. If we can help you with anything else, please feel free to ask.

I tried to use your method but it didn’t work. When I deployed to the server to get the absolute path, I got an error. I used ClassLoader.getSystemResource (“static / font / chinese /”). GetPath (); , But I get an error when deploying to the server to get the absolute path, how to solve it?

@pang679,

You can also try copying the Font file(s) in the resources folder of your project. Rebuilding the project should embed the Font file(s) into application’s .jar file. After that you can load the Font files into stream by using the following code:

Program.class.getResourceAsStream("font.ttf")

Please also refer to the MemoryFontSource Class:

hello,I have looked at this class MemoryFontSource,
byte[] fontBytes = Files.readAllBytes(Paths.get(getMyDir() + “Alte DIN 1451 Mittelschrift.ttf”));
The parameters in Path.get () are also absolute paths. However, the absolute path cannot be obtained by deploying it as a jar file to the server, and the relative path will not work on my development machine. I would like to ask how to obtain the absolute path in the linux server or jar file.

@pang679,

Please note that FontSettings.setFontsFolder method of Aspose.Words for Java 19.11 accepts both the absolute paths and the relative paths.

Absolute path example:

Document doc = new Document("E:\\temp\\205152\\in.docx");
FontSettings fs = new FontSettings();
fs.setFontsFolder("C:\\Users\\Awais\\Documents\\IdeaProjects\\TestApp\\static\\fonts\\chinese", true);
doc.setFontSettings(fs);
doc.save("E:\\temp\\205152\\19.11.pdf");

Relative path example:

Document doc = new Document("E:\\temp\\205152\\in.docx");
FontSettings fs = new FontSettings();
fs.setFontsFolder("static/fonts/chinese", true);
doc.setFontSettings(fs);
doc.save("E:\\temp\\205152\\19.11.pdf");

Another option that you can try is to please copy your ‘static’ folder (including the sub-folders and font files i.e. static/font/chinese) to inside the resources folder. Rebuilding the project should embed those folders and the Font file(s) into application’s .jar file. After that please try running the following code:

Document doc = new Document("E:\\temp\\205152\\in.docx");
FontSettings fs = new FontSettings();
String path = getClass().getResource("static/fonts/chinese").toString().replace("file:/", "");
fs.setFontsFolder(path, true);
doc.setFontSettings(fs);
doc.save("E:\\temp\\205152\\19.11.pdf");

Hope, this helps.

OK,I try it again ,thanks