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 Nov 19, 2024 3:42 pm

Hi,
Is it possible to set default fonts as in MS Word?
default_font.jpg


Thanks.

andrei.chorin
 
Posts: 68
Joined: Sun Jan 08, 2023 2:36 pm

Wed Nov 20, 2024 3:54 am

Hi,

Thank you for your inquiry.
Sorry, we don't have a direct way to set the default font. However, you can refer to the following code to set the font for paragraphs. After configuring it, any new paragraphs you add will automatically display in the font you specified. The output results of the following code can be referenced by the screenshot below, which shows all text in Times New Roman, 18 points.

Code: Select all
        Document doc = new Document();
        Section section1 = doc.addSection();
        // Set font
        Paragraph para1 = section1.addParagraph();
        para1.getStyle().getCharacterFormat().setFontName("Times New Roman");
        para1.getStyle().getCharacterFormat().setFontSize(15);

        para1.appendText("Hello World");
        para1.appendText("123241241");
        para1.appendText("Spire.Doc");
        section1.addParagraph().setText("TestFont");
        section1.addParagraph().appendText("E-iceblue.Test");
        doc.addSection().addParagraph().setText("TestSpire.DocFont");
        doc.saveToFile("testFont.docx");


Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 460
Joined: Mon Nov 07, 2022 8:10 am

Sun Nov 24, 2024 7:18 am

Hi, but these settings defined default fonts which will be in use in case of missing or not supported.
For example in displaying, printing, PDF conversion.
How I can set this in SpireDoc library?

Thanks.

andrei.chorin
 
Posts: 68
Joined: Sun Jan 08, 2023 2:36 pm

Mon Nov 25, 2024 8:29 am

Hi,

Thank you for your reply and clarification.
The default font setting in Microsoft Word does not take effect when the font is missing or unsupported. These settings are typically applied when a new document is created, and any text entered into the document will be displayed in the default font. You can achieve this effect using the code I provided earlier.
Regarding your need of "setting a font as a substitute when converting, displaying or printing documents with missing fonts," our product does not support such a feature. When converting or printing, our product prioritizes using the fonts used in the Word document. In cases where the required fonts are not available in the environment, our product will automatically search for similar fonts in the environment to use as substitutes. Therefore, it is recommended that you install the fonts used in the Word document before performing conversion or printing operations. Alternatively, you can place the uninstalled font files in a folder and use the document.SetCustomFontsFolders("FontFolderPath") method to specify the path.
If you have any further questions, please feel free to leave a message again.

Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 460
Joined: Mon Nov 07, 2022 8:10 am

Return to Spire.Doc

cron