When it comes to creating professional documents, choosing the right font is crucial. Using multiple fonts in one document can help distinguish different types of content such as headings, body text or annotations, ultimately enhancing the document's readability. Moreover, different fonts have unique emotional tones and styles. For instance, handwritten fonts often convey warmth and intimacy while serif fonts are ideal for traditional and formal contexts. Although Microsoft Word offers a wide range of font capabilities, setting the font programmatically is also necessary. In this article, we will demonstrate how to set the font of Word document in Java using Spire.Doc for Java library.
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>12.11.0</version> </dependency> </dependencies>
Set the Font in Word
To set different fonts for different paragraphs in a Word document, you need to create multiple paragraph styles and then set a different font for each paragraph style. After that, apply these paragraph styles to specific paragraphs. The detailed steps are as follows:
- Create a Document instance.
- Add a section to this document using Document. addSection() method.
- Add three paragraphs to this section using Section.addParagraph() method, and then use Paragraph.appendText() method to append text for each of them.
- Create a ParagraphStyle instance.
- Set the paragraph style name using ParagraphStyle.setName method.
- Set the font name and size using ParagraphStyle.getCharacterFormat().setFontName() and ParagraphStyle.getCharacterFormat().setFontSize() method.
- Add the style to the document using Document.getStyles().add() method.
- Apply the desired paragraph style to the paragraph using the Paragraph.applyStyle() method.
- Repeat the above steps to create another ParagraphStyle instance, set its font, and apply it to other paragraphs.
- Save the result document using Document.saveToFile() method.
- Java
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.ParagraphStyle; public class SetFont { public static void main(String[] args){ //Create a Document instance Document document = new Document(); //Add a section to this document Section section = document.addSection(); //Add three paragraphs to this section and append text for each of them Paragraph para1 = section.addParagraph(); para1.appendText("Yellowstone National Park"); Paragraph para2 = section.addParagraph(); para2.appendText("Yellowstone National Park, or Yellowstone Park for short, is managed by the National Park Service of the United States. On March 1, 1872, it was officially named as a national park to protect wild animals and natural resources, and was included in the World Natural Heritage List in 1978. This is the first national park in the world."); Paragraph para3 = section.addParagraph(); para3.appendText("Yellowstone National Park covers an area of 898317 hectares, mainly located in Wyoming, USA, and partially located in Montana and Idaho. Yellowstone Park is divided into five areas: the Mammoth Hot Spring Area in the northwest is mainly composed of limestone steps, so it is also called hot step area."); //Create a ParagraphStyle instance ParagraphStyle style1 = new ParagraphStyle(document); //Set the first paragraph as the title and set its font style1.setName("titleStyle"); style1.getCharacterFormat().setFontName("Arial"); style1.getCharacterFormat().setFontSize(16f); document.getStyles().add(style1); para1.applyStyle("titleStyle"); //Create a ParagraphStyle instance ParagraphStyle style2 = new ParagraphStyle(document); //Set the other two paragraphs as the body and set their fonts style2.setName("paraStyle"); style2.getCharacterFormat().setFontName("Times New Roman"); style2.getCharacterFormat().setFontSize(10f); document.getStyles().add(style2); para2.applyStyle("paraStyle"); para3.applyStyle("paraStyle"); //Save the result docunmen document.saveToFile("output/setFont.docx", FileFormat.Docx); document.dispose(); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.