Skip to main content
Skip table of contents

How to create a pushbuttonfield with double byte character text?

I need a pushbuttonfield with double byte characters (DBCS) as the text.

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(
            pdfDoc,
            new Rectangle(36, 780, 108, 26),
            "japanese",
            JAPANESE,
            font,
            PdfFormField.DEFAULT_FONT_SIZE);
    PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
    form.addField(pushButton);
    pdfDoc.close();
}

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 How to create a pushbuttonfield with double byte character text? if you want to see how to answer this question in iText 5.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.