I'm trying to fill in interactive form annotations by appending the PDF with updated versions of the annotation objects. I'm following the example in the appendix of the PDF spec for adding/modifying annotations. My code seems to work fine for PDFs that use XRef tables, but for PDFs that use XRef streams it comes up as corrupted when I try to open it with a reader. I've checked over the file pretty thoroughly and everything seems correct and all the pointers point to the right place, so I'm now wondering if it's simply illegal to append a Xref table to a PDF that is using Xref streams? Do I have to create an XRef stream myself in this case?
Here's the output file that comes up corrupted: http://www.qotile.net/temp/corrupted_PDF.pdf
And here's the code at the end of the file that I'm appending to modify the annotation. All I do is replace object 27 0 R with a new version that includes a value (/V) string, plus the cross reference table and trailer:
startxref
116
%%EOF
27 0 obj % Widget annotation<<
/AP<<
/N 14 0 R>>
/DA (/Helv 0 Tf 0 g)
/DR<<
/Encoding<<
/PDFDocEncoding 38 0 R>>
/Font<<
/Helv 37 0 R>>>>
/F 4
/FT/Tx
/P 13 0 R
/Rect [ 107.760000 267.840000 368.160000 296.640000 ]
/Subtype/Widget
/T (name)
/TU (name)
/Type/Annot
/V (paul slocum) % I'm adding this value string to the widget>>
endobj
xref
0 1
0000000000 65535 f
27 1
0000013182 00000 n
trailer
<<
/ID [ <DCC8F2378338A373925EA4CF44E98821> <D18CF3E84123490DB66BC2F30B3C2C7C> ]
/Info 10 0 R
/Prev 116
/Root 12 0 R
/Size 40>>
startxref
13463
%%EOF