Skip to content

Instantly share code, notes, and snippets.

@wpietri
Last active April 6, 2019 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpietri/7cae917d73bcbf69c8a60353630a9fea to your computer and use it in GitHub Desktop.
Save wpietri/7cae917d73bcbf69c8a60353630a9fea to your computer and use it in GitHub Desktop.
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
public class IssueDemo {
public static void main(String[] args) throws IOException {
PDDocument doc = PDDocument.load(new File("i-130.pdf"));
PDResources res = doc.getDocumentCatalog().getAcroForm().getDefaultResources();
PDFont font = PDType0Font.load(doc, new FileInputStream(new File("NotoSansCJKsc-Medium.ttf")), false);
String fontName = res.add(font).getName();
long start = System.currentTimeMillis();
for (Iterator<PDField> it = doc.getDocumentCatalog().getAcroForm().getFieldIterator(); it.hasNext(); ) {
PDField field = it.next();
if (field instanceof PDTextField) {
PDTextField textField = (PDTextField) field;
textField.setDefaultAppearance("/" + fontName + " 0 Tf 0 g");
textField.setValue("中国");
long end = System.currentTimeMillis();
System.out.println("Filled " + textField.getFullyQualifiedName() + " in " + (end - start) + "ms");
start = end;
}
}
doc.save(new File("i-130-filled.pdf"));
}
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file has been truncated, but you can view the full file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment