Displaying emojis using iText 8 requires you to use a font that supports the unicode characters for emojis. By default, the most common fonts don’t support colorful emojis, but you can use some fonts like Segoe UI Emoji
(which comes with Windows 10), Apple Color Emoji
, NotoColorEmoji
or FreeSerif
to display black and white outlined emoji characters.
Here is a simple example of how to add an emojis using Segoe UI Emoji
font. You can find the font file in C:\Windows\Fonts\
directory, and the file name is seguiemj.ttf
. You need to add the seguiemj.ttf
file to your project’s resources, in a Maven project, under the src/main/resources
directory. Then, modify your code as follows:
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();
}
}
}
Output:
In this example, “\uD83D\uDE00” is the Unicode surrogate pair for the grinning face emoji (😀). This code will result in a PDF document containing the text “Hello 😀”.
To depict different emojis, you would need their respective unicode characters. You can get those from various online sources such as the official Unicode website.
Remember, though, for colored emojis, you might need to use specific emoji fonts like Apple Color Emoji
or NotoColorEmoji
, and even then, support can be limited, since colored font rendering is not a standard PDF feature and the display can depend heavily on the viewer software.
Maven Dependencies
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-core</artifactId>
<version>8.0.4</version>
<type>pom</type>
</dependency>
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024
I tested your code on my MacOS with itext 8.0.4. I got different result.
it shows as red round dish instead of a red smile face.
Hi Cidy, thanks for stoping by. One thing that you need to check is to make sure that you have the font, in this case the
seguiemj.ttf
, and place the font under theresources
directory in your Maven project.And this is what I put on stack overflow: https://stackoverflow.com/questions/78599935/itextpdf-8-to-generate-pdf-with-emoji-font-support