I'm trying to find out a way to create pdf with emoji fonts support by using itextpdf 8.0.3.
I only found an example that came from itextpdf official website; https://kb.itextpdf.com/itext/pdfhtml-using-emojis-in-itext but it is talking about create pdf from html source.
I found an other example on Internet such as: https://kodejava.org/how-do-i-display-emoji-in-a-pdf-document-using-itext-8/.
There are some code as:
package org.kodejava.itext;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.colors.DeviceRgb;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import java.io.IOException;
public class DocumentWithEmoji {
public static void main(String[] args) throws IOException {
PdfWriter writer = new PdfWriter("emoji.pdf");
PdfDocument pdf = new PdfDocument(writer);
try (Document document = new Document(pdf)) {
PdfFont fontEmoji = PdfFontFactory.createFont("seguiemj.ttf", PdfEncodings.IDENTITY_H);
Paragraph paragraph = new Paragraph()
.add(new Paragraph("Hello ").setFont(fontEmoji).setFontSize(20))
.add(new Paragraph("\uD83D\uDE00").setFont(fontEmoji).setFontSize(20)
.setFontColor(new DeviceRgb(243, 58, 106)));
document.add(paragraph);
} catch (Exception e) {
e.printStackTrace();
}
}
}
expected return should be:
but I got it as:
As you can see, it shows as red round dish instead of a red smile face.
Does anyone have such experience? if you do, please share some idea with me.
😀
├── pom.xml
└── src
└── main
├── java
│  └── com
│  └── example
│  └── itext
│  └── DocumentWithEmoji.java
├── lib-jar-itext
│  ├── barcodes-8.0.4.jar
│  ├── bouncy-castle-adapter-8.0.4.jar
│  ├── bouncy-castle-connector-8.0.4.jar
│  ├── bouncy-castle-fips-adapter-8.0.4.jar
│  ├── commons-8.0.4.jar
│  ├── font-asian-8.0.4.jar
│  ├── forms-8.0.4.jar
│  ├── hyph-8.0.4.jar
│  ├── io-8.0.4.jar
│  ├── kernel-8.0.4.jar
│  ├── layout-8.0.4.jar
│  ├── pdfa-8.0.4.jar
│  ├── pdftest-8.0.4.jar
│  ├── pdfua-8.0.4.jar
│  ├── sign-8.0.4.jar
│  ├── styled-xml-parser-8.0.4.jar
│  └── svg-8.0.4.jar
└── resources
└── fonts
└── NotoEmoji-SemiBold.ttf
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.itext</groupId>
<artifactId>itext8-emoji-fonts</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>itext8-emoji-fonts</name>
<description>itext8-emoji-fonts</description>
<properties>
<java.version>17</java.version>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.13</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>barcodes</artifactId>
<version>8.0.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/barcodes-8.0.4.jar</systemPath>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>bouncy-castle-adapter</artifactId>
<version>8.0.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/bouncy-castle-adapter-8.0.4.jar</systemPath>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>bouncy-castle-connector</artifactId>
<version>8.0.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/bouncy-castle-connector-8.0.4.jar</systemPath>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>bouncy-castle-fips-adapter</artifactId>
<version>8.0.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/bouncy-castle-fips-adapter-8.0.4.jar</systemPath>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>commons</artifactId>
<version>8.0.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/commons-8.0.4.jar</systemPath>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>font-asian</artifactId>
<version>8.0.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/font-asian-8.0.4.jar</systemPath>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>forms</artifactId>
<version>8.0.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/forms-8.0.4.jar</systemPath>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>hyph</artifactId>
<version>8.0.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/hyph-8.0.4.jar</systemPath>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>8.0.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/io-8.0.4.jar</systemPath>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>8.0.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/kernel-8.0.4.jar</systemPath>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>8.0.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/layout-8.0.4.jar</systemPath>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>pdfa</artifactId>
<version>8.0.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/pdfa-8.0.4.jar</systemPath>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>pdfua</artifactId>
<version>8.0.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/pdfua-8.0.4.jar</systemPath>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>sign</artifactId>
<version>8.0.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/sign-8.0.4.jar</systemPath>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>styled-xml-parser</artifactId>
<version>8.0.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/styled-xml-parser-8.0.4.jar</systemPath>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>svg</artifactId>
<version>8.0.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/svg-8.0.4.jar</systemPath>
</dependency>
</dependencies>
</project>
COPY AND MODIFY FROM YOUR EXAMPLE CODE
package com.example.itext;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.colors.DeviceRgb;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import java.io.FileNotFoundException;
public class DocumentWithEmoji {
public static void main(String[] args) throws FileNotFoundException {
PdfWriter writer = new PdfWriter("emoji.pdf");
PdfDocument pdf = new PdfDocument(writer);
try (Document document = new Document(pdf)) {
//PdfFont fontEmoji = PdfFontFactory.createFont("seguiemj.ttf", PdfEncodings.IDENTITY_H);
PdfFont fontEmoji = PdfFontFactory.createFont("fonts/NotoEmoji-SemiBold.ttf", PdfEncodings.IDENTITY_H);
Paragraph paragraph = new Paragraph()
.add(new Paragraph("Hello "))
.add(new Paragraph("\uD83D\uDE00").setFont(fontEmoji).setFontSize(20)
.setFontColor(new DeviceRgb(243, 58, 106)));
document.add(paragraph);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I only changed the font from seguiemj.ttf
to NotoEmoji-SemiBold.ttf
NotoEmoji-SemiBold.ttf
Download Noto_Emoji.zip from https://fonts.google.com/noto/specimen/Noto+Emoji
Select , and Download All(1)
Unzip Noto_Emoji.zip ,
Noto_Emoji
├── NotoEmoji-VariableFont_wght.ttf
├── OFL.txt
├── README.txt
└── static
├── NotoEmoji-Bold.ttf
├── NotoEmoji-Light.ttf
├── NotoEmoji-Medium.ttf
├── NotoEmoji-Regular.ttf
└── NotoEmoji-SemiBold.ttf
copy NotoEmoji-SemiBold.ttf
to Project Directory: src/main/resources/fonts/
open https://github.com/itext/itext-java/releases/tag/8.0.4
download iText-Core-8.0.4-only-jars.zip
unzip iText-Core-8.0.4-only-jars.zip
put all jar to src/main/lib-jar-itext
I Test it in Idea.