Skip to main content

Replace Fonts in PDF in Java

 

There may be a situation that you want to change the font(s) in your PDF document. In this article, I will show you how to replace fonts in PDF programmatically in Java using Free Spire.PDF for Java library.

Installation

If you use maven, you need to specify the dependencies for Free Spire.PDF for Java in your project’s pom.xml file.

  1. <repositories>   
  2.   
  3.         <repository>   
  4.   
  5.             <id>com.e-iceblue</id>   
  6.   
  7.             <name>e-iceblue</name>   
  8.   
  9.             <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>   
  10.   
  11.         </repository>   
  12.   
  13. </repositories>   
  14.   
  15. <dependencies>   
  16.   
  17.     <dependency>   
  18.   
  19.         <groupId>e-iceblue</groupId>   
  20.   
  21.         <artifactId>spire.pdf.free</artifactId>   
  22.   
  23.         <version>4.4.1</version>   
  24.   
  25.     </dependency>   
  26.   
  27. </dependencies>

For non-maven projects, download Free Spire.PDF for Java pack from this website and add Spire.Pdf.jar in the lib folder into your project as a dependency.

Implementation

The following are the steps to replace fonts in a PDF document:

  1. Create a PdfDocument instance
  2. Load the document using PdfDocument.loadFromFile(String) method
  3. Create a PDFFont instance
  4. Get the fonts used in the document using PdfDocument.getUsedFonts() method
  5. Replace font with the PDFFont instance using PdfUsedFont.replace(PdfFontBase) method
  6. Save the document using PdfDocument.saveToFile(String) method

Sample code

  1. import com.spire.pdf.PdfDocument;  
  2. import com.spire.pdf.graphics.*;  
  3. import com.spire.pdf.graphics.fonts.PdfUsedFont;  
  4.   
  5. import java.awt.*;  
  6.   
  7. public class ReplaceFonts {  
  8.     public static void main(String []args) throws Exception {  
  9.         //Create a PdfDocument instance  
  10.         PdfDocument pdf = new PdfDocument();  
  11.         //Load the PDF document  
  12.         pdf.loadFromFile("E:\\Program Files\\input.pdf");  
  13.         //Create a PdfFont instance  
  14.         PdfFont baseFont = new PdfFont(PdfFontFamily.Helvetica, 11f, PdfFontStyle.Italic);  
  15.         //Get the fonts used in the Pdf document  
  16.         PdfUsedFont[] usedFonts = pdf.getUsedFonts();  
  17.         //Replace the first font  
  18.         usedFonts[0].replace(baseFont);  
  19.         //Save the result file  
  20.         pdf.saveToFile("output.pdf");  
  21.     }  
  22. }  

The above example shows how to replace a font with a PDF base font, if you want to replace the font with a TrueType font or with a font file that is available on your system but is not installed, simply create a PdfTrueTypeFont instance instead of PdfFont instance.

For example, use

  1. //Create a PdfTrueTypeFont instance with font name  
  2. PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(new Font("Arial",Font.ITALIC, 14));  

or

  1. //Create a PdfTrueTypeFont instance with font file  
  2. PdfTrueTypeFont fontFile = new PdfTrueTypeFont("Arial.ttf"14);  

instead of

  1. PdfFont baseFont = new PdfFont(PdfFontFamily.Helvetica, 11f, PdfFontStyle.Italic);   

Besides, you can also replace the font with a CJK(Chinese, Japanese, Korean) font using PdfCjkStandardFont instance:

  1. PdfCjkStandardFont cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.Hanyang_Systems_Gothic_Medium, 14);  


That’s all. Thanks for taking time to read my article.

 

Comments

Popular posts from this blog

3 Ways to Generate Word Documents from Templates in Java

A template is a document with pre-applied formatting like styles, tabs, line spacing and so on. You can quickly generate a batch of documents with the same structure based on the template. In this article, I am going to show you the different ways to generate Word documents from templates programmatically in Java using Free Spire.Doc for Java library. Prerequisite First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file. <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>                   <url>http: //repo.e-iceblue.com/nexus/content/groups/public/</url>                </repository>       </repositories>       <dependencies>           <dependency>               <g

Simple Java Code to Convert Excel to PDF in Java

This article demonstrates a simple solution to convert an Excel file to PDF in Java by using free Excel API – Free Spire.XLS for Java . The following examples illustrate two possibilities to convert Excel to PDF:      Convert the whole Excel file to PDF     Convert a particular Excel Worksheet to PDF Before start with coding, you need to Download Free Spire.XLS for Java package , unzip it and import Spire.Xls.jar file from the lib folder in your project as a denpendency. 1. Convert the whole Excel file to PDF Spire.XLS for Java provides saveToFile method in Workbook class that enables us to easily save a whole Excel file to PDF. import com.spire.xls.FileFormat; import com.spire.xls.Workbook; public class ExcelToPDF {     public static void main(String[] args){         //Create a Workbook         Workbook workbook = new Workbook();         workbook.loadFromFile( "Sample.xlsx" );         //Fit to page         workbook.getConverterSetting().setShee

Insert and Extract OLE objects in Word in Java

You can use OLE (Object Linking and Embedding) to include content from other programs, such as another Word document, an Excel or PowerPoint document to an existing Word document. This article demonstrates how to insert and extract embedded OLE objects in a Word document in Java by using Free Spire.Doc for Java API.   Add dependencies First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file.     <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>                   <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>               </repository>       </repositories>       <dependencies>           <dependency>               <groupId>