I'm able to fill a textbox annotation with the following code, but the text won't appear in certain readers like Adobe Acrobat, though it does appear in Chrome and other Webkit-based browsers. The PDFs I'm trying to fill do not use AcroForms or FDF. I'm using Apache PDFBox, but I don't believe there is much difference in PDF libraries, even across languages/platforms.
// edited for brevity
PDAnnotation annotation = doc.getPages().get(0).getAnnotations().get(0);
COSDictionary cosObject = annotation.getCOSObject();
cosObject.setString(COSName.V, content);
An example document is IRS form W-4.
What I've tried so far
I've tried comparing my PDF output against a document filled in Chrome, but the only difference I see is in the default appearance (DA) property. I've tried to set the default appearance text content like this, but to no avail:
COSString defaultAppearance = (COSString)cosObject.getItem(COSName.DA);
COSString newAppearance = new COSString(defaultAppearance.getString() + "0 0 Td (" + value + ") Tj");
cosObject.setItem(COSName.DA, newAppearance);
I've also messed around with a few flags that sounded promising:
int FLAG_PRINT = 4;
int FLAG_READ_ONLY = 64;
annotation.setAnnotationFlags(annotation.getAnnotationFlags() | FLAG_PRINT | FLAG_READ_ONLY);
I've also tried other properties:
cosObject.setString(COSName.CONTENTS, content);
I believe the relevant section in the PDF 1.7 spec is 12.7.4.3.
What am I missing?