Replace Fonts in PDF in Java
Tags: java, pdf, replace, font
Published: 2021-12-17 09:02:11

---
title: Replace Fonts in PDF in Java
published: true
description: This article demonstrates how to replace fonts in PDF using Java.
tags: Java,PDF,replace,font
//cover_image: https://direct_url_to_image.jpg
---
One of the key factors in making successful documents is the use of appropriate fonts. For example, we usually apply different fonts for headings and paragraphs in order to achieve differentiation and a visually pleasing result. When you get a PDF document in which the fonts are not properly used, you may want to change the fonts being used. 

In this article, I am going to introduce how to programmatically **replace the used fonts in a PDF document** by using [Spire.PDF for Java](https://www.e-iceblue.com/Introduce/pdf-for-java.html).
##Installing Spire.Pdf.jar
If you use Maven, you can easily import the Spire.Pdf.jar in your application by adding the following code to your project’s pom.xml file. For non-Maven projects, download the jar file from [this link](https://www.e-iceblue.com/Download/pdf-for-java.html) and manually add it as a dependency in your application.
```java
<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.pdf</artifactId>
        <verson>4.11.2</version>
    </dependency>
</dependencies>
```
##Example 1. Replace All Fonts with Another Font
Spire.PDF for Java offers **PdfDocument.getUsedFonts()** to get the collection of fonts being used in the document. Loop through the fonts in the collection, and replace each of them with another font using **PdfUsedFont.replace()** method.
```java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.graphics.PdfFont;
import com.spire.pdf.graphics.PdfFontFamily;
import com.spire.pdf.graphics.PdfFontStyle;
import com.spire.pdf.graphics.fonts.PdfUsedFont;

public class ReplaceAllFonts {

    public static void main(String[] args) throws Exception {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Load a sample PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Fonts.pdf");

        //Get all used fonts in the document
        PdfUsedFont[] fonts = doc.getUsedFonts();

        //Create a font 
        PdfFont newfont = new PdfFont(PdfFontFamily.Times_Roman, 12f, PdfFontStyle.Italic);

        //Travers all fonts used in the document
        for (PdfUsedFont font: fonts
             ) {

            //Replace the current font with a new font
            font.replace(newfont);

        }

        //Save the changes to a different PDF file
        doc.saveToFile("output/ReplaceAllFonts.pdf");
    }
}
```
![ReplaceAllFonts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9touhp6v13ln3ihaqlhw.png)
##Example 2. Replace a Specific Font with a Different Font
After a certain font is obtained from the document, you can get the font name by **PdfUsedFont.getName()** method. If the font is exactly what you want to replace, then you can replace it with a different font.
```java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.graphics.PdfFont;
import com.spire.pdf.graphics.PdfFontFamily;
import com.spire.pdf.graphics.PdfFontStyle;
import com.spire.pdf.graphics.fonts.PdfUsedFont;

public class ReplaceSpecificFontWithNewFont {

    public static void main(String[] args) throws Exception {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Load a sample PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Fonts.pdf");

        //Get all used fonts in the document
        PdfUsedFont[] fonts = doc.getUsedFonts();

        //Create a font
        PdfFont newfont = new PdfFont(PdfFontFamily.Times_Roman, 12f, PdfFontStyle.Italic);

        //Travers all fonts used in the document
        for (PdfUsedFont font: fonts
        ) {

            //Determine if a font is "Calibri"
            if(font.getName().equals("Calibri")){

                //Replace the font "Calibri" with a new font
                font.replace(newfont);
            }

        }

        //Save the changes to a different PDF file
        doc.saveToFile("output/ReplaceSpecificFont.pdf");
    }
}
```
![ReplaceSpecificFont](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44cxqfzibr6fugbze1kh.png)