Last active
April 6, 2019 07:01
-
-
Save wpietri/7cae917d73bcbf69c8a60353630a9fea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment