How to create a pushbuttonfield with double byte character text?

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

31st May 2016
admin-marketing

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 this link if you want to see how to answer this question in iText 5.


Share this article

Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now