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

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