News Category

Font

Font (4)

Java: Set the Font in Word

2023-06-09 01:08:10 Written by support iceblue

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.4.6</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();
    }
}

Java: Set the Font in Word

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.

In certain circumstances, you may need to insert superscripts and subscripts in Microsoft Word. For instance, when you are creating an academic document involving scientific formulas. In this article, you will learn how to insert superscripts and subscripts into Word documents in Java using Spire.Doc for Java library.

Install Spire.Doc for Java

First, 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.4.6</version>
    </dependency>
</dependencies>
    

Insert Superscripts and Subscripts into Word using Java

The following are the main steps to insert a superscript or subscript into a Word document using Spire.Doc for Java:

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Get the specific section using Document.getSections().get(sectionIndex) method.
  • Add a paragraph to the section using Section.addParagraph() method.
  • Add normal text to the paragraph using Paragraph.appendText() method.
  • Add superscript or subscript text to the paragraph using Paragraph.appendText() method.
  • Apply superscript or subscript formatting to the superscript or subscript text through TextRange.getCharacterFormat().setSubSuperScript() method.
  • Save the result document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.SubSuperScript;
import com.spire.doc.fields.TextRange;

public class InsertSuperscriptAndSubscript {
    public static void main(String[] args){
        //Create a Document instance
        Document document = new Document();
        //Load a Word document
        document.loadFromFile("Sample.docx");

        //Get the first section
        Section section = document.getSections().get(0);

        //Add a paragraph to the section
        Paragraph paragraph = section.addParagraph();

        //Add normal text to the paragraph
        paragraph.appendText("E = mc");
        //Add superscript text to the paragraph
        TextRange superscriptText = paragraph.appendText("2");
        //Apply superscript formatting to the superscript text
        superscriptText.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);

        //Start a new line
        paragraph.appendBreak(BreakType.Line_Break);

        //Add normal text to the paragraph
        paragraph.appendText("H");
        //Add subscript text to the paragraph
        TextRange subscriptText = paragraph.appendText("2");
        //Apply subscript formatting to the subscript text
        subscriptText.getCharacterFormat().setSubSuperScript(SubSuperScript.Sub_Script);
        //Add normal text to the paragraph
        paragraph.appendText("O");

        //Set font size for the text in the paragraph
        for(Object item : paragraph.getItems())
        {
            if (item instanceof TextRange)
            {
                TextRange textRange = (TextRange)item ;
                textRange.getCharacterFormat().setFontSize(36f);
            }
        }

        //Save the result document
        document.saveToFile("InsertSuperscriptAndSubscript.docx", FileFormat.Docx_2013);
    }
}

Java: Insert Superscripts and Subscripts into Word

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.

Java: Change Font Color in Word

2022-03-25 09:14:00 Written by support iceblue

Changing the font color of a certain paragraph or text can help you make the paragraph or text stand out in your Word document. In this article, we will demonstrate how to change the font color in Word 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.4.6</version>
    </dependency>
</dependencies>
    

Change Font Color of a Paragraph in Java

The following are the steps to change the font color of a paragraph in a Word document:

  • Create a Document instance.
  • Load the Word document using Document.LoadFromFile() method.
  • Get the desired section using Document.getSections().get(sectionIndex) method.
  • Get the desired paragraph that you want to change the font color of using Section.getParagraphs().get(paragraphIndex) method.
  • Create a ParagraphStyle instance.
  • Set the style name and font color using ParagraphStyle.setName() and ParagraphStyle.getCharacterFormat().setTextColor() methods.
  • Add the style to the document using Document.getStyles().add() method.
  • Apply the style to the paragraph using Paragraph.applyStyle() method.
  • Save the result document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ParagraphStyle;

import java.awt.*;

public class ChangeFontColorForParagraph {
    public static void main(String []args){
        //Create a Document instance
        Document document = new Document();
        //Load a Word document
        document.loadFromFile("Sample.docx");

        //Get the first section
        Section section = document.getSections().get(0);

        //Change text color of the first Paragraph
        Paragraph p1 = section.getParagraphs().get(0);
        ParagraphStyle s1 = new ParagraphStyle(document);
        s1.setName("Color1");
        s1.getCharacterFormat().setTextColor(new Color(188, 143, 143));
        document.getStyles().add(s1);
        p1.applyStyle(s1.getName());

        //Change text color of the second Paragraph
        Paragraph p2 = section.getParagraphs().get(1);
        ParagraphStyle s2 = new ParagraphStyle(document);
        s2.setName("Color2");
        s2.getCharacterFormat().setTextColor(new Color(0, 0, 139));;
        document.getStyles().add(s2);
        p2.applyStyle(s2.getName());

        //Save the result document
        document.saveToFile("ChangeParagraphTextColor.docx", FileFormat.Docx);
    }
}

Java: Change Font Color in Word

Change Font Color of a Specific Text in Java

The following are the steps to change the font color of a specific text in a Word document:

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Find the text that you want to change font color of using Document.findAllString() method.
  • Loop through all occurrences of the searched text and change the font color for each occurrence using TextSelection.getAsOneRange().getCharacterFormat().setTextColor() method.
  • Save the result document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.TextSelection;

import java.awt.*;

public class ChangeFontColorForText {
    public static void main(String []args){
        //Create a Document instance
        Document document = new Document();
        //Load a Word document
        document.loadFromFile("Sample.docx");

        //Find the text that you want to change font color for
        TextSelection[] text = document.findAllString("Spire.Doc for .NET", false, true);
        //Change the font color for the searched text
        for (TextSelection seletion : text)
        {
            seletion.getAsOneRange().getCharacterFormat().setTextColor(Color.red);
        }

        //Save the result document
        document.saveToFile("ChangeCertainTextColor.docx", FileFormat.Docx);

    }
}

Java: Change Font Color in Word

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.

Embedding and applying private fonts in Word or PDF documents can make your documents unique. This article demonstrates how to embed fonts in a Word document as well as a converted PDF document, by using Spire.Doc for Java.

Embed Font in Word Document

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.PrivateFontPath;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;

public class EmbedPrivateFontInWord {

    public static void main(String[] args) {

        //create a Word document
        Document document = new Document(false);
        
        //add a paragraph
        Paragraph paragraph = document.addSection().addParagraph();
        
        //embed a private font in Word document
        document.setEmbedFontsInFile(true);
        PrivateFontPath fontPath = new PrivateFontPath();
        fontPath.setFontName("Otto");
        fontPath.setFontPath("C:\\Users\\Administrator\\Desktop\\Otto.ttf");
        document.getPrivateFontList().add(fontPath);

        //add text to the paragraph and apply the embedded font 
        TextRange tr = paragraph.appendText("Spire.Doc for Java is a professional Java Word API that enables "+
                        "Java applications to create, convert, manipulate and print Word documents without " +
                        "using Microsoft Office.");
        tr.getCharacterFormat().setFontName("Otto");
        tr.getCharacterFormat().setFontSize(26f);

        //save to file
        document.saveToFile("output/EmbedFont.docx", FileFormat.Docx);
    }
}

Embed Private Fonts When Saving Word to DOCX and PDF in Java

Embed Font in Converted PDF Document

import com.spire.doc.Document;
import com.spire.doc.PrivateFontPath;
import com.spire.doc.ToPdfParameterList;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;

import java.util.*;

public class EmbedPrivateFontInConvertedPDF {

    public static void main(String[] args) {

        //create a Word document
        Document document = new Document(false);
        //add a paragraph
        Paragraph paragraph = document.addSection().addParagraph();

        //initialize PrivateFontPath object
        PrivateFontPath fontPath = new PrivateFontPath("Otto","C:\\Users\\Administrator\\Desktop\\Otto.ttf");

        //add text to paragraph and apply the private font
        TextRange tr = paragraph.appendText("Spire.Doc for Java is a professional Java Word API that enables "+
                "Java applications to create, convert, manipulate and print Word documents without " +
                "using Microsoft Office.");
        tr.getCharacterFormat().setFontName("Otto");
        tr.getCharacterFormat().setFontSize(26f);

        //create a ToPdfParameterList object
        ToPdfParameterList toPdfParameterList = new ToPdfParameterList();

        //set private fonts as a parameter for converting Word to PDF
        List pathList = new LinkedList<>();
        pathList.add(fontPath);
        toPdfParameterList.setPrivateFontPaths(pathList);

        //save to PDF file
        document.saveToFile("output/EmbedFont.pdf",toPdfParameterList);
    }
}

Embed Private Fonts When Saving Word to DOCX and PDF in Java