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 Sep 06, 2022 1:32 pm

Hi guys,
I use spire.doc for JAVA to generate PDF documents from Word documents.

In Word document template is Calibri font used as default. But in the generated PDF I want to use CARLITO instead of CALIBRI font in all document without the need to apply new font to all document paragraphs, tables, list format styles.

So I am trying to tell spire.doc to use CARLITO as CALIBRI font, so adding font CARLITO font with fake CALIBRI font name.

I am using this code:
Code: Select all
ToPdfParameterList params = new ToPdfParameterList();

List<PrivateFontPath> fonts = new ArrayList<>();
// here is the fake calibri font name used with carlito font path
fonts.add(new PrivateFontPath("Calibri", carlitoPath));

params.setPrivateFontPaths(fonts);
document.saveToStream(fileStream, params);


Currently it doesn't work, the default TimesNewRoman font is used when PDF is generated. Is it possible to achieve the described use case? Is there any other simple way how to achieve it?

Thank you for your help

matus.vandak
 
Posts: 18
Joined: Fri Sep 02, 2022 10:00 pm

Wed Sep 07, 2022 8:37 am

Hello,

Thanks for your inquiry.

The "setPrivateFontPaths" method is used to tell Spire.Doc the path to the local font file so that it can be used if needed, rather than specifying the default font name used. That's why it doesn't work as you expected.
When executing the conversion process from Word to PDF, we will search for the corresponding font files in the current system font library according to the font name specified by the text in Word. If you used the "setPrivateFontPaths" method to add font files other than the system font library, they will also be included in the search. So if you need to specify the same font for all the text in the document in the conversion, I'm afraid you can only modify the font name of all the text in turn and then start the conversion. Of course, the premise is that your system font library contains this font or you have used the "setPrivateFontPaths" method to tell Spire.Doc the location of this font.
Given the complex structure of the document (such as containing nested tables), the code logic to iterate through all the paragraphs in the document and set the font of text is cumbersome. Here is a relatively simple code (still need to iterate through all document elements) to modify the font name of all text in the whole document. The following code is for your reference. Please test and adjust according to your own situation. Hope it helps you.

Code: Select all
        Document doc = new Document();
        doc.loadFromFile("testInput.docx");

        Queue<DocumentObject> queue = new LinkedList<>();
        queue.offer(doc);
        while (!queue.isEmpty()) {
            DocumentObject obj = queue.poll();
            if (obj instanceof TextRange) {
                TextRange range = (TextRange) obj;
                range.getCharacterFormat().setFontName("carlito");
            } else if (obj.getChildObjects().getCount() > 0) {
                for (int j = 0; j < obj.getChildObjects().getCount(); j++) {
                    queue.offer(obj.getChildObjects().get(j));
                }
            }
        }
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Wed Sep 07, 2022 11:25 am

Thank you for your help. Understood.

E-iceblue support is really great.

Best regards

matus.vandak
 
Posts: 18
Joined: Fri Sep 02, 2022 10:00 pm

Thu Sep 08, 2022 1:33 am

Thanks for your appreciation.
Have a nice day! :D
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Thu Sep 08, 2022 7:12 am

I guess if there is another option. Currently if calibri font not found, default font (TimesNewRoman) is used.

Is it possible to set that another font (carlito) will be used as default font when generating pdf and required font not found?

matus.vandak
 
Posts: 18
Joined: Fri Sep 02, 2022 10:00 pm

Thu Sep 08, 2022 9:52 am

Hi,

Sorry, Spire.Doc currently does not provide a similar method.

If the required font cannot be found during conversion, Spire.Doc will search the system for a font which has a similar glyph and can display the current character as a replacement. This is similar to how Microsoft Word performs docx to PDF conversion. So for the idea you mentioned, I think it also has some problems. For example, when there is text in multiple languages in the document, you cannot set the same font for them. At this time, if the fonts that are not found are replaced with a font specified by the user, then there will be characters that are destined to be displayed because garbled characters in the PDF as no font is compatible with all the languages in the world.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Return to Spire.Doc