Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Feb 13, 2024 2:44 pm

I am trying to covert a file that is exclusively written in Times New Roman, although the font varies in size throughout the document. Furthermore, the Times New Roman is sometimes bold, other times italicized and other times bold and italicized (and the rest of the time it is normal with no such modifications). I wish to use Java code to convert this .docx file to a pdf format and I understand that there is a nice tutorial to specifiy the path of the font in .ttf file format given by the developer ("Font" under "Program Guide" titled: Embed Private Fonts When Saving Word to DOCX and PDF in Java). However since my file uses multiple different fonts effectively (i.e. bold Times New Roman, italicized Times New Roman, etc...), would you please tell me how do I adapt/modify the tutorial code to accomodate such multiple different fonts (all in .ttf file format) in my one conversion program?

My present code looks like this:

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import java.io.File;

public class App
{
public static void main( String[] args )
{
String cwd = System.getProperty("user.dir"); String fileName = null;
if (args.length >= 1) {
fileName = args[0];
}

Document doc = new Document(cwd + File.separator + fileName);
doc.saveToFile("convertedToPDF.pdf", FileFormat.PDF);
}
}


and I get the following error:

java.lang.IllegalStateException: Cannot find any fonts in specified font sources.

If it makes a difference, there is a table containing such text in my Word/.docx file. Thanks for any pointers you can give.

Bill

bill_mcintyre
 
Posts: 3
Joined: Tue Feb 13, 2024 2:22 pm

Wed Feb 14, 2024 3:32 am

Hello,

Thanks for your inquiry.
Please kindly note that our Word to PDF conversion feature reads corresponding font data from the system-installed font library based on the font name used in the Word document and then draws the content with the fonts to the PDF page, while embedding the font data into the PDF document by default. Based on the error you are currently encountering, it appears that the fonts used in the document is not installed on your server. I suggest two solutions to address this issue:
Solution1: Install the fonts used in the Word document on your system.
Solution2: Instead of installing the font, place the font files in any folder on the server, and then use the document.setCustomFontsFolders(fontFolder) method to read it.
The complete sample code is as follows:
Code: Select all
Document document = new Document();
document.loadFromFile(input);
document.setCustomFontsFolders(fontFolder);
document.saveToFile(output, FileFormat.PDF);


If you have any questions, please let us know.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 203
Joined: Mon Dec 27, 2021 2:23 am

Wed Feb 14, 2024 5:55 am

Hello William,

It worked. Thanks.

Now, the only thing is that on my converted document has a message in red up at the top that says "Warning: The document was created with Spire.Doc for JAVA" - is there a way to remove this?

Also, my original .docx file had some of its text non-italicized, non-bolded, but the coverted pdf made all the text italicized (and some italicized and bolded too). If I only include the standard font (just plain Times New Roman in my folder and get rid of the other stylized fonts such as italicized Times New Roman), will my document come out identically as it looks in the original .docx file (i.e. some plain text, some italicized, some bold italicized, some bold - but not all italicized)?

Thanks.

Bill

bill_mcintyre
 
Posts: 3
Joined: Tue Feb 13, 2024 2:22 pm

Wed Feb 14, 2024 6:45 am

Hello,

Thanks for your reply.

For your first issue,kindly note that the evaluation watermark will generate when you use our commercial version without applying a valid license. Have you purchased our product? If not, you can apply for a temporary authorization that is valid for one month by clicking on this link:https://www.e-iceblue.com/TemLicense.html.
For your second issue, please first check if all the fonts used in the document have been placed in the folder. If still does'nt work, please provide us with your word document to help us further investigate. Thank you for your assistance.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 203
Joined: Mon Dec 27, 2021 2:23 am

Thu Feb 15, 2024 10:25 am

Hello,

No, I have not purchased a license. Is there not then a limited features version that doesn't have the water mark? All I need is word/docx to pdf, I do not need the other special features.

If not, I do appreciate your help and prompt answers. Thank you.

Bill

bill_mcintyre
 
Posts: 3
Joined: Tue Feb 13, 2024 2:22 pm

Fri Feb 16, 2024 1:34 am

Hello,

Thank you for your feedback.
Our free version of Spire.Doc also supports converting Word files to PDF and this version results in files without the red warning watermark. However, there is a 3-page content limit and we do not maintain the free version. Therefore, we recommend that you test it with the commercial version, which is regularly maintained and contains many new features.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1652
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc