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.
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 and manually add it as a dependency in your application.
<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.
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");
}
}
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.
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");
}
}
Top comments (0)