Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Jun 02, 2021 11:00 am

Hello,

I have a "Lato" font installed on my system and want to replace other fonts in already existing PDF document with it. However, there are some problems which I couldn't resolve by myself nor find any fix.

I do it by iterating already existing fonts from document. For each found PdfUsedFont I extract style and size, and then I use it to create the java.awt.Font, which is needed to create the PdfTrueTypeFont. Then I just replace the already used font with the new PdfTrueTypeFont.
Code: Select all
for (PdfUsedFont usedFont : pdfDocument.getUsedFonts()) {
                try {
                    EnumSet<PdfFontStyle> pdfFontStyles = usedFont.getStyle();
                    int stylesOrSum = pdfFontStyles.stream().reduce(0, (orSum, nextStyle) -> (orSum | nextStyle.getValue()), CommonUtils::bitwiseOr);
                    Font awtFontToReplace = new Font(
                            latoFontName, //latoFontName is equal to "Lato"
                            stylesOrSum,
                            (int) usedFont.getSize());
                    PdfTrueTypeFont pdfTrueTypeFontToReplace = new PdfTrueTypeFont(awtFontToReplace);
                    usedFont.replace(pdfTrueTypeFontToReplace);
                } catch (Exception e) {
                    logger.error("Font replacement has been failed. The old ones will be used.");
                    CommonUtils.logStackTrace(e);
                }
            }


Unfortunately, as I researched the topic, embedded fonts cannot be replaced. This is the first problem I met with, but it's not critical and I can ignore this or find other solutions. However, even if font is of "Standard" type, I can't replace it - NullPointerException occurs:
java.lang.NullPointerException
at com.spire.pdf.graphics.PdfTrueTypeFont.<init>(Unknown Source)
at com.spire.pdf.graphics.PdfTrueTypeFont.spr (Unknown Source)
at com.spire.pdf.graphics.fonts.PdfUsedFont.replace(Unknown Source)
[...]


What can be the reason of this?

Lato font is installed properly, it can be found with such code:

Code: Select all
String latoFontName = Arrays.stream(GraphicsEnvironment
                .getLocalGraphicsEnvironment()
                .getAvailableFontFamilyNames())
                .filter(s->s.equalsIgnoreCase("Lato"))
                .findFirst()
                .orElse(null);


I got it from here: https[://]fonts[.]google[.]com/specimen/Lato

My environment is:
- zulu8.40.0.25-ca-fx-jdk8.0.222-win_i686 JRE (but I checked Oracle Java 8 and Java 11 distributions and problem was the same)
- org.eclipse.swt.win32.win32.x86 SWT 4.3 version
- spire.pdf.free from e-iceblue repository, 4.4.1 version

granerosEye
 
Posts: 2
Joined: Wed Jun 02, 2021 10:17 am

Thu Jun 03, 2021 11:15 am

Hello,

Thanks for your inquiry.
To help us investigate your issue more quickly and accurately, please provide your input file. You could send it to us (support@e-iceblue.com) via email. Thanks in advance.

Sincerely,
Brian
E-iceblue support tea
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Sun Jun 06, 2021 3:19 pm

Hello,

I've sent the email with attached input file.

granerosEye
 
Posts: 2
Joined: Wed Jun 02, 2021 10:17 am

Mon Jun 07, 2021 6:01 am

Hello Mikołaj,

Thanks for providing your input file via email.
I am sorry that some fonts in your input file are not standard type 1 fonts or non-embedded TrueType fonts, and our Spire.PDF for Java does not support replacing such fonts, thus causing an exception. Apologize for the inconvenience caused.

Besides, don’t worry, we have removed the file you provided and all data connected to it.

If you have any other questions related to our products, please feel free to contact us.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.PDF