When I manually enter the data, here is what shows:
When I fill the text field using the code shown below, here is what shows:
Why the difference?
Thank you for any assistance you can give.
Here are some notes that may or may not be relevant:
The template pdf is available at:
https://www.irs.gov/pub/irs-pdf/f1065sk1.pdf
Getting this warning:
WARNING: Using fallback font Helvetica for HelveticaLTStd-Roman
The IRS forms are created with Adobe LiveCycle Designer, an application that only runs on Windows and costs many thousands of dollars. As such I cannot edit the PDF with Acrobat Pro.
Here is minimal code to duplicate the problem.
package util.pdf.box;
import util.file.BytesToFile;
import util.file.FileToBytes;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import java.io.ByteArrayOutputStream;
public class PdfFormFiller {
public byte[] fill(
byte[] template,
String key,
String value
) {
byte[] pdfBytes = new byte[]{};
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (
PDDocument doc = PDDocument.load(template)
) {
PDDocumentCatalog pdDocCatalog = doc.getDocumentCatalog( );
PDAcroForm acroForm = pdDocCatalog.getAcroForm();
PDField field = acroForm.getField(key);
field.setValue(value);
doc.save(baos);
} catch ( Exception e ) {
System.out.println( e.getMessage( ) );
}
pdfBytes = baos.toByteArray( );
return pdfBytes;
}
public static void main(String[] args) {
String filePath = "/Users/johndoe/f1065sk1.pdf";
byte[] pdfBytes = FileToBytes.readFile( filePath );
PdfFormFiller pdfFormFiller = new PdfFormFiller( );
byte[] pdfFilled = pdfFormFiller.fill(
pdfBytes,
"topmostSubform[0].Page1[0].LeftCol[0].f1_7[0]",
"NamenAddressnCity, State Zip"
);
String outDirName = "/Users/johndoe";
String outFileName = "f1065sk1.filled.pdf";
BytesToFile.writeToDirFile( pdfFilled, outDirName, outFileName );
}
}