Skip to content

Instantly share code, notes, and snippets.

@bengolder
Last active April 17, 2021 12:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bengolder/529ee55addad590b9f95e74d78ebe9a9 to your computer and use it in GitHub Desktop.
Save bengolder/529ee55addad590b9f95e74d78ebe9a9 to your computer and use it in GitHub Desktop.
examples from people using unicode in itext
// from http://osdir.com/ml/java.lib.itext.general/2006-03/msg00662.html
PdfReader pdf = new PdfReader("C:\\VorlageOnline.pdf");
PdfStamper stp = new PdfStamper(pdf, new FileOutputStream("c:\\out.pdf"));
AcroFields af = stp.getAcroFields();
BaseFont bf = BaseFont.createFont("c:\\windows\\fonts\\l_10646.ttf", BaseFont.IDENTITY_H, true);
af.addSubstitutionFont(bf);
af.setField("field1","This is a Test Müller");
stp.close();
// from http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-8#328-textfieldfonts.java
/*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/
package part2.chapter08;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.TextField;
public class TextFieldFonts {
/** The resulting PDF. */
public static final String RESULT1 = "results/part2/chapter08/unicode_field_1.pdf";
/** The resulting PDF. */
public static final String RESULT2 = "results/part2/chapter08/unicode_field_2.pdf";
/** The resulting PDF. */
public static final String RESULT3 = "results/part2/chapter08/unicode_field_3.pdf";
/** The resulting PDF. */
public static final String RESULT4 = "results/part2/chapter08/unicode_field_4.pdf";
/** The resulting PDF. */
public static final String RESULT5 = "results/part2/chapter08/unicode_field_5.pdf";
/** The resulting PDF. */
public static final String RESULT6 = "results/part2/chapter08/unicode_field_6.pdf";
/** The resulting PDF. */
public static final String RESULT7 = "results/part2/chapter08/unicode_field_7.pdf";
/** The resulting PDF. */
public static final String RESULT8 = "results/part2/chapter08/unicode_field_8.pdf";
/** A String containing Chinese characters/ */
public static final String TEXT = "These are the protagonists in 'Hero', a movie by Zhang Yimou:\n"
+ "\u7121\u540d (Nameless), \u6b98\u528d (Broken Sword), "
+ "\u98db\u96ea (Flying Snow), \u5982\u6708 (Moon), "
+ "\u79e6\u738b (the King), and \u9577\u7a7a (Sky).";
/** A String containing Korean characters. */
public static final String BINJIP = "The Korean title of the movie 3-Iron is \ube48\uc9d1 (Bin-Jip)";
/**
* Creates a PDF document.
* @param filename the path to the new PDF document
* @param appearacnes sets the need appearances flag if true
* @param font adds a substitution font if true
* @throws DocumentException
* @throws IOException
*/
public void createPdf(String filename, boolean appearances, boolean font)
throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
writer.getAcroForm().setNeedAppearances(appearances);
TextField text = new TextField(writer, new Rectangle(36, 806, 559, 780), "description");
text.setOptions(TextField.MULTILINE);
if (font) {
BaseFont unicode =
BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
text.setExtensionFont(BaseFont.createFont());
ArrayList<BaseFont> list = new ArrayList<BaseFont>();
list.add(unicode);
text.setSubstitutionFonts(list);
}
text.setText(TEXT);
writer.addAnnotation(text.getTextField());
// step 5
document.close();
}
/**
* Manipulates a PDF file src with the file dest as result
* @param src the original PDF
* @param dest the resulting PDF
* @throws IOException
* @throws DocumentException
*/
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
form.setField("description", BINJIP);
stamper.close();
reader.close();
}
/**
* Manipulates a PDF file src with the file dest as result
* @param src the original PDF
* @param dest the resulting PDF
* @throws IOException
* @throws DocumentException
*/
public void manipulatePdfFont1(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
BaseFont unicode =
BaseFont.createFont("HYSMyeongJoStd-Medium", "UniKS-UCS2-H", BaseFont.NOT_EMBEDDED);
form.setFieldProperty("description", "textfont", unicode, null);
form.setField("description", BINJIP);
stamper.close();
reader.close();
}
/**
* Manipulates a PDF file src with the file dest as result
* @param src the original PDF
* @param dest the resulting PDF
* @throws IOException
* @throws DocumentException
*/
public void manipulatePdfFont2(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
BaseFont unicode =
BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
form.addSubstitutionFont(unicode);
form.setField("description", BINJIP);
stamper.close();
reader.close();
}
/**
* Main method
* @param args no arguments needed
* @throws IOException
* @throws DocumentException
*/
public static void main(String[] args) throws IOException, DocumentException {
TextFieldFonts example = new TextFieldFonts();
example.createPdf(RESULT1, false, false);
example.createPdf(RESULT2, true, false);
example.createPdf(RESULT3, false, true);
example.manipulatePdf(RESULT1, RESULT4);
example.manipulatePdf(RESULT2, RESULT5);
example.manipulatePdf(RESULT3, RESULT6);
example.manipulatePdfFont1(RESULT3, RESULT7);
example.manipulatePdfFont2(RESULT3, RESULT8);
}
}
@bengolder
Copy link
Author

bengolder commented May 28, 2016

From Filling in data with text fields (iText 5)

The Chinese characters in TEXT aren’t shown in the appearance of the text field because iText doesn’t know where to find a font file containing those characters.

You can work around this by setting the NeedAppearances flag. When set, this flag instructs the PDF viewer to create the appearances for the widget annotations. This way you pass the responsibility for rendering the text correctly to the application that’s used to view the PDF. The same mechanism is triggered when the end user clicks the text field to change the text. This won’t work on all systems: the PDF viewer needs to have access to a font with the Chinese glyphs.

offers another workaround. With the setExtensionFont() method, you define the main font that should be used for the field. In this case, the default font Helvetica. Helvetica doesn’t know how to draw Chinese characters, so you use the setSubstitutionFonts() method to add Arial Unicode. Whenever iText detects a character that can’t be rendered with the extension font, it will go through the list of substitution fonts. The first font that has a glyph definition for the needed character will be used (see the third window in figure 8.3). In this case, iText will embed a subset of Arial Unicode in the PDF file.

ADDING UNICODE TO TEXT FIELDS

As soon as you try to fill out the form with other Asian characters than the ones that were in the TEXT string, you’ll run into trouble. That’s shown in the first and third windows of figure 8.4, which replace the English-Chinese text with a text containing some Korean characters. The upper three windows correspond to the three windows from figure 8.3. As you can see, the workaround still works (for me, on my OS), but fails because iText doesn’t know where to find a font containing the Korean glyphs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment