-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for filling form fields programmatically #132
Comments
The following is some debugging information, in case it's helpful.
Calling
However, for
|
Hi @liammcdermott , thank you for reporting the issue. I am happy to help you with this. The appearance streams for the form fields likely need to be created manually - assuming Pdfium offers a sufficient interface to do so, I need to check that - and the work could potentially be related to #89, which also touches appearance streams. |
Great to hear you'd like to help with this, thanks @ajrcarey! Regarding whether Pdfium supports appearance streams for form fields: I'm not sure I'm looking in the right place, but there is CPDF_GenerateAP::GenerateFormAP(), is that the interface we're looking for? |
That interface is private, unfortunately. We're limited to the public FPDF_* functions. The |
(That said, the |
I followed the usages of that private Maybe we could:
Side note: (1) is necessary, since (2) only generates appearance streams for annotations that don't already have them. This solution relies on Pdfium's internal implementation recreating the appearance streams when they don't exist, however, I'm sanguine about that, since page 678 of the PDF spec says:
The spec is pretty clear, Pdfium must regenerate the appearance stream if it finds there isn't one. Then we could send a patch upstream, adding a function like What do you think? |
Huh, I just did an experiment, adding this to my code above, just before the call to b.FPDFAnnot_SetAP(annotation_handle, PdfAppearanceMode::Normal as i32, null()); By 'it works' I mean, when I open the filled PDF in Chrome or Evince, the form fields are filled out. So, thanks to you pointing out (I still want to make a PR for this) |
Superb work. Did you need to use To answer your earlier question: yes, mutation of form field values would be a valuable addition to I'm curious as to whether the |
Nope! I just needed to add that one line. It's something of a hack, since AFAICT it leads to the annotations having no appearance streams in the resulting PDF. That forces clients (like Chrome and Evince) to generate the appearance streams themselves. I'm thinking a better implementation would be triggering Pdfium to regenerate the appearance streams.
That's great to hear. Those are the traits I was looking at and taking ideas from, so yes, I'm assuming the functionality should be added to them.
I suspect the If you don't mind, I'd like to submit PRs for both bindings and trait updates. The only reason I used the bindings directly was to avoid forking pdfium-render (eventually I failed even at that!), and while working on the problem I got a good feel for how mutation of form field values could be added to the traits. If I can't get both done by the end of next week, I'll pass it back to you, does that sound okay? Let me know if you have any particular requirements for the implementation of this (beyond the usual, like code style and whatnot). |
Many thanks. By all means, take as much time as you like. I'm not in any hurry and could not realistically start work on this for a couple of weeks anyway. |
Well I said 'by the end of next week', but work had other plans. My hope is to work on it this weekend. BTW: I did meet the deadline, and the form filling is working well in testing, so I'm hoping this will be a good addition. However, there is one major caveat: my itch was text fields, and that's what I've scratched. I'd love to implement filling check boxes and so forth, but that will have to come later (AFAICT it's not that different from filling text fields). |
Great that you met your deadline and text boxes only is perfectly fine, I'm happy to implement the rest based on your template. |
Hi @liammcdermott , any updates on this? |
@ajrcarey I have made a start, and have blocked off some more time tomorrow to work on this. I'll let you know how I get on! |
Made good progress on this today. I should have a PR for you sometime tomorrow @ajrcarey |
Merged pull request. Made some small adjustments to doc comments and imports. Updated README. Added new |
I know there are usually better solutions than pdfium-render for this (pdftk for example). However, a convoluted set of circumstances have led me to attempt filling PDF forms using pdfium-render.
Specifically, I have a PDF file that includes a form with text fields, and I am attempting to fill those text fields with values programmatically, then save a copy of the PDF with those fields filled.
Here is what I have so far:
Right now this code just fills the value of every widget field with 'TEST', but it does fill the values of the form widgets successfully. However, when opening the PDF, programmatically set values do not appear on the page until the user sets the focus on the form field containing that value. Meaning, if the form field is a text box, the user has to set the focus inside it before the value will appear.
I did some debugging, and found that while this code successfully updates form field values in the
obj
/Type /Annot
, when I checked the corresponding appearance stream, the text and related text drawing commands were absent.At that point I realised it's time to stop trying to figure this out myself, and get some help. Here are my questions:
FORM_OnBeforeClosePage()
for example); so with that in mind, would you be interested in a PR?Obviously, any code I submitted in a PR would be less messy than the example above! Thank you for your consideration.
The text was updated successfully, but these errors were encountered: