How to create a pushbuttonfield with double byte character text?

Tags: formspushbutton fieldiText 7

I need a pushbuttonfield with double byte characters (DBCS) as the text. Currently if I set the button text as any double byte text, the text won't show up.

This works:

PushbuttonField myButton = new PushbuttonField(writer, rect, "JapaneseButton"); 
myButton.setText("japanese text here");
But if I replace "japanese text here" with real Japanese text, the text doesn't show.

Posted on StackOverflow on Mar 12, 2015 by Jason

Please take a look at the CreateJapaneseButton example. It creates a PDF with a button that looks like this:

Button with Japanese text
Button with Japanese text

Your code snippet is very short, but you probably have all the code you need, except the line that sets the font:

public static final String FONT = "src/main/resources/fonts/FreeSans.ttf";
public static final String JAPANESE = "\u3042\u304d\u3089";

public void createPdf(String dest) throws Exception {
    PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
    PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
    PdfButtonFormField pushButton = PdfFormField.createPushButton(
            new Rectangle(36, 780, 108, 26),
    PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);

If you don't set the font, the default font is used. The default font is the Standard Type 1 font Helvetica. This font doesn't know how to write Japanese. I used the font freesans.ttf. If you don't have that font, you can try arialuni.ttf or any other font that supports Japanese.

Click this link if you want to see how to answer this question in iText 5.