Im using the PDFbox library to write a script that fills the textfields in a pre-existing PDF.
for whatever reason when i try to use the setValue() method to alter the text in the field i will get an error (copied below)
When printing the exact appearance information of the field it says the font is listed as //Helvetica so its not null but for whatever reason it just doesn't want to accept
i've tried refreshing the default appearances before attempting to set the value and manually setting the appearance both to no effect. Any assistance would be greatly appreciated
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDCheckBox;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
File file = new File(pathToFile);
PDDocument document = PDDocument.load(file);
PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
PDTextField field = (PDTextField) acroForm.getField( "Name" );
field.setValue("test");
document.save(PathToSaveFile);
}
}
exact error message
Exception in thread "main" java.lang.IllegalArgumentException: font is null, check whether /DA entry is incomplete or incorrect at pdfbox.app@2.0.22 (1)/org.apache.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper.insertGeneratedAppearance(AppearanceGeneratorHelper.java:438) at pdfbox.app@2.0.22 (1)/org.apache.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper.setAppearanceContent(AppearanceGeneratorHelper.java:392) at pdfbox.app@2.0.22 (1)/org.apache.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper.setAppearanceValue(AppearanceGeneratorHelper.java:225) at pdfbox.app@2.0.22 (1)/org.apache.pdfbox.pdmodel.interactive.form.PDTextField.constructAppearances(PDTextField.java:264) at pdfbox.app@2.0.22 (1)/org.apache.pdfbox.pdmodel.interactive.form.PDTerminalField.applyChange(PDTerminalField.java:228) at pdfbox.app@2.0.22 (1)/org.apache.pdfbox.pdmodel.interactive.form.PDTextField.setValue(PDTextField.java:219) at freshpdfedit.Main.main(Main.java:35)
Edit: Figured it out!, feel very foolish for not seeing this earlier but the problem was that while the Acroform had the default appearance declared automatically, the fields i was creating don't have it declared automatically, a simple line to assign the Acroforms default appearance on to the fields and everything worked fine.
field.setDefaultAppearance(acroForm.getDefaultAppearance());
Figured it out!, feel very foolish for not seeing this earlier but from what i can tell the problem was that while the Acroform had the default appearance declared automatically, the fields i was creating don't have it declared automatically, a simple line to assign the Acroforms default appearance on to the fields and everything worked fine.
the magic line was
field.setDefaultAppearance(acroForm.getDefaultAppearance());