About us | News | Contact
iText ® Licenses Support   
You are here: Home > Forums > iText in Action — Second Edition > Part 3: Essential iText skills > Chapter 11: Choose the right font
User login
  • Request new password

Can't get Japanese characters to display in a form

Submitted by draskin on Thu, 07/05/2012 - 19:30

I have recently purchased the book and am doing very lite iText work. All I am doing is reading in an existing PDF document that has fields. Then I fill those fields with text supplied by my application. This works just fine with English text.

I am referencing itextpdf-5.3.0.jar, text-xtra-5.3.0.jar, and itextasian-1.5.2.jar

Here's the issue I am seeing:

1. I am developing on the Mac (OS X Lion)
2. When the field text is English or Japanese (I suspect same will happen for an CJK language) I am seeing the field text in the preview just fine.
3. When the PDF file is transferred to Windows and previewed using Acrobat Reader, the Japanese text disappears. English fields show up fine.
4. Here's the code I am using to update the document (try/catch and close() calls omitted):

PdfReader reader = new PdfReader(pdfFilename);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(updatedPdfFilePath));

final AcroFields stamperFields = stamper.getAcroFields();

for (String key : this.fields.keySet())
{
if (stamperFields.getFieldItem(key) != null)
{
String value = this.fields.get(key);
stamperFields.setField(key, value);
}
}

5. I suspect it has to do with the fact that Japanese font is not embedded in the document and Acrobat on Windows can't find the proper font to display.
6. I read the section on fonts (admittedly I am a total newbie when it comes to fonts) and have tried a view things.
7. When I add this code before the above code, the preview on the Mac goes totally blank:

BaseFont unicode = BaseFont.createFont("HeiseiMin-W3", "UniJIS-UCS2-H", BaseFont.NOT_EMBEDDED);

8. I also tried this call, but then Japanese text doesn't show up on the Mac preview either:

stamper.setFormFlattening(true);

Please help! Thanks in advance!

Dave Raskin
Rimage Corporation

ligature implementation for Indian languages / Devanagari script ›
  • Login to post comments

This is explained in the book

Submitted by Bruno Lowagie on Sat, 07/07/2012 - 13:42.

First this: you don't need itext-xtra.

Second: you have bought the book, now please read section 8.3.3. In this section, there's a literal description of the problem you're describing. There's also a list of different strategies to solve this problem. Which solution is best depends on the context of your own application. Just read the pros and cons of every strategy and decide which one is best for you.

Please try the solutions explained in the book first; then return to the forum if you have any further questions.

  • Login to post comments

Having issues on Mac OS X

Submitted by draskin on Mon, 07/09/2012 - 18:57.

I did read the chapter. I removed the reference to itext-xtra jar. I tried the following code:

BaseFont japanese = BaseFont.createFont("/Library/Fonts/ fontList = new ArrayList();
fontList.add(japanese);
stamperFields.setSubstitutionFonts(fontList);

I found out that on OS X iText doesn't like .ttf fonts. So I found a free Japanese .otf font - AdobeHeitiStd-Regular.otf - downloaded it, installed it, and it loaded fine.

BaseFont japanese = BaseFont.createFont("/Users/draskin/Library/Fonts/AdobeHeitiStd-Regular.otf",
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

BTW, I tried EMBEDDED and NOT_EMBEDDED - made no difference.

BUT, when I called stamper.close(), it threw a NullPointerException. Here's the stack trace:

java.lang.NullPointerException
at com.itextpdf.text.pdf.CFFFontSubset.ReconstructPrivateSubrs(CFFFontSubset.java:1534)
at com.itextpdf.text.pdf.CFFFontSubset.Reconstruct(CFFFontSubset.java:1406)
at com.itextpdf.text.pdf.CFFFontSubset.BuildNewFile(CFFFontSubset.java:1147)
at com.itextpdf.text.pdf.CFFFontSubset.Process(CFFFontSubset.java:378)
at com.itextpdf.text.pdf.TrueTypeFontUnicode.writeFont(TrueTypeFontUnicode.java:369)
at com.itextpdf.text.pdf.FontDetails.writeFont(FontDetails.java:290)
at com.itextpdf.text.pdf.PdfWriter.addSharedObjectsToBody(PdfWriter.java:1292)
at com.itextpdf.text.pdf.PdfStamperImp.close(PdfStamperImp.java:180)
at com.itextpdf.text.pdf.PdfStamper.close(PdfStamper.java:192)

Also, can I use any of the OS X built-in JCK fonts? If not, what do I do?
thanks,
dave raskin
Rimage Corporation

  • Login to post comments

Some counter-questions

Submitted by Bruno Lowagie on Mon, 07/16/2012 - 12:42.

This doesn't look right:
BaseFont japanese = BaseFont.createFont("/Library/Fonts/ fontList = new ArrayList();
It won't compile, so I guess something went wrong when you copy/pasted the code.

I found out that on OS X iText doesn't like .ttf fonts. TTF was supposed to be a standard, but Apple and MS decided to interpret the standard differently. iText doesn't have any problem with Open Type fonts with extension .ttf. However, .ttf files containing a TrueType font for Mac won't work; you need a True Type font that works on MicroSoft Windows.

Why don't you just use a font such as arialuni.ttf?

  • Login to post comments

OS X ttf fonts

Submitted by trobolson on Tue, 01/15/2013 - 18:10.

I'm concerned about your comment regarding "OS X iText doesn't like .ttf fonts". I recently upgraded from 2.0.6 (!) to 5.3.5 and what had previously been working with a .ttc file on the Mac now shows completely blank in Preview. Is this iText dislike a new feature in the latest versions? I'm using English text only so I'm not certain the discussion in 8.3.3 applies.

Thanks,

  • Login to post comments
Content © 2010 1T3XT BVBA