How to create a pushbuttonfield with double byte character text?

Tags: formspushbutton fieldiText 5

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 JAPANESE = "\u3042\u304d\u3089";
public static final String FONT = "resources/fonts/FreeSans.ttf";
public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter writer =
        PdfWriter.getInstance(document, new FileOutputStream(dest));;
    PushbuttonField button = new PushbuttonField(
        writer, new Rectangle(36, 780, 144, 806), "japanese");
    BaseFont bf = BaseFont.createFont(
        FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

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.