gdelugre / origami

Origami is a pure Ruby library to parse, modify and generate PDF documents.
GNU Lesser General Public License v3.0
326 stars 110 forks source link

How to fill out an Acroform or XFA? #44

Open jesseshieh opened 6 years ago

jesseshieh commented 6 years ago

Hi, First off, thanks for an awesome library! I was reading through the docs and examples, but I couldn't figure out if it's possible to fill out an XFA or Acroform using Origami. I figured out how to extract XFA data from a pdf, but I'm still not sure how to fill one out. Can you point me in the right direction? Thanks!

rgerard commented 5 years ago

+1. I'd also like to know the answer to this question.

untoldone commented 5 years ago

Been poking around here comparing PDFs with forms filled vs PDFs without forms filled using pdfwalker.

If you do something like this you'll find you'll fill a field

pdf = Origami::PDF.read "input.pdf"
puts pdf.fields[0].T # Field Name
pdf.fields[0].V = "hello world"
pdf.write "output.pdf"

Though the problem I'm having is that when I write the pdf like this, mac preview appears visually to have not have the field filled in until i click on the field at which point the text is visible. When I used an online tool as an alternative to convert the PDF to a JPG the fields do show up...

I suspect another part of the PDF needs to be modified at the same time but I was also poking around other libraries which don't seem to make other changes when form filling? (though maybe theres another calculation that happens on save?) ... anyways I'll keep investigating this or eventually give up and switch to something PDFTK based.

untoldone commented 5 years ago

Adding a bit more ... super weird behavior -- If I manually set the form text content via Mac Preview + save and open the file -- I find the content in pdf.fields[0].V. If I then change it using setV save and open the file with preview again, the old content is still in the field ... except when I click on the field the new content shows up. I tried this with forms generated by Acrobat as well as as forms generated by open office.

@gdelugre Perhaps this is a bug? I really don't know enough about PDF to know if this is behaving as expected or if it is something else. I noticed you tagged this as both feature and question -- does that mean form filling isn't currently supported?

danielricecodes commented 5 years ago

I'm finding that the above snipped doesn't appear to work. When I write out the file after setting a Value via the V= setter, the file can't be opened. I am not 100% sure this library can fill in PDF fields?

fuel_—_-bash_—_204×56

I'm personally looking for a pure ruby implementation of something like PDFTK or FillablePDF because both of those require a lot of memory. In FillablePDF's case, it requires Java.

rngtng commented 4 years ago

any updates here? I've same issue and happy for any pointers.. thx

democlitos commented 10 months ago

First read this post.

Now basically after chanding the 'V' value you also need to change the AppearanceStream, and Origami does that automatically to you when you set:

pdf.Catalog.AcroForm.NeedAppearances=true