Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Add a Form Field in PDF

by Arik123 (Sexton)
on Oct 26, 2017 at 07:00 UTC ( #1202056=perlquestion: print w/replies, xml ) Need Help??

Arik123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Monks! I want to create a PDF in Perl. I start with a template PDF, but want to alter the values of the form fields, as well as add some new form fields (ListBoxes or ComboBoxes). The first task is easy - CAM::PDF can read the template, change the values of the fields, and save the result. But I didn't find a way to add a new ListBox or ComboBox. The newly-added field should be invisible (hidden). Its values will be availabe to some JavaScript which will be run once the user opens the file. How do I do that in Perl? Thanks a lot!

Replies are listed 'Best First'.
Re: Add a Form Field in PDF
by vr (Curate) on Oct 26, 2017 at 15:44 UTC

    With CAM::PDF, adding (vs. extracting or modifying something already present) usually means going as low level as you can endure. There's no easy way to create a PDF from scratch, to add a blank page, to populate it with new content, e.g. to import an image, etc. That's how it was designed, probably fulfilling its goals 100% (note: I'm only speculating).

    However, if you know your way around COS and/or are comfortable with consulting the "PDF Reference", your particular task is trivial. Speaking about task, the "hidden UI element" smells XY problem to me, but:

    Starting with PDF (test.pdf) exported from LibreOffice as template, with whatever content and some text form field (because I'm relying on AcroForm and Annots fields being available. Not too difficult to create them manually, though):

    use strict; use warnings; use feature 'say'; use CAM::PDF; sub _n { CAM::PDF::Node-> new( @_ )} sub _p { CAM::PDF-> parseAny( \$_[ 0 ])} my $doc = CAM::PDF-> new( 'test.pdf' ) or die; # let's create a ListBox dictionary; its reference to be # added to 2 arrays my $forms_node = $doc-> getRootDict-> { AcroForm } or die; my $forms_dict = $doc-> getValue( $forms_node ); my $fields_ary = $doc-> getValue( $forms_dict-> { Fields }); my $page_annots_node = $doc-> getPage( 1 )-> { Annots } or die; my $page_annots_ary = $doc-> getValue( $page_annots_node ); # it's funny how we code, below, half in Perl, half in kind of # DSL, i.e. write PDF syntax directly and make CAM::PDF to # parse it into whatever it thinks appropriate. Said "PDF # syntax" is not too difficult and actually quite fun. my $ref = _n( reference => $doc-> appendObject( undef, _n( # "1", below, is for "hidden"; # because of that, Rect is any, and Appearance is not even # dealt with here. # "Ch" is "choice field", "list box" object => _p( q(<< /Type /Annot /Subtype /Widget /F /1 /Rect [ 0 0 1 1 ] /FT /Ch /Opt [ (My sneaky item #1) (Item I'm very proud of) (Hidden item nobody will find) ] /T (MyTopSecretListBox) >>)) ), 0 )); push @$fields_ary, $ref; push @$page_annots_ary, $ref; $doc-> cleanoutput( 'new.pdf' ); # let's re-open the modified PDF and test it # by using buit-in CAM::PDF facilities to assign value # to the added form field $doc = CAM::PDF-> new( 'new.pdf' ); say for $doc-> getFormFieldList; $doc-> fillFormFields( MyTopSecretListBox => << 'END_OF_IT' Hidden item nobody will find But we are not limited to our silly list, if value is set programmatically END_OF_IT ); $doc-> cleanoutput( 'new2.pdf' );

    Let's somehow test what we did:

    $ perl acroform.pl Text Box 1 MyTopSecretListBox $ pdftk test.pdf dump_data_fields --- FieldType: Text FieldName: Text Box 1 FieldFlags: 0 FieldValue: FieldJustification: Left $ pdftk new.pdf dump_data_fields --- FieldType: Text FieldName: Text Box 1 FieldFlags: 0 FieldValue: FieldJustification: Left --- FieldType: Choice FieldName: MyTopSecretListBox FieldFlags: 0 FieldJustification: Left FieldStateOption: Hidden item nobody will find FieldStateOption: Item I'm very proud of FieldStateOption: My sneaky item #1 $ pdftk new2.pdf dump_data_fields --- FieldType: Text FieldName: Text Box 1 FieldFlags: 0 FieldValue: FieldJustification: Left --- FieldType: Choice FieldName: MyTopSecretListBox FieldFlags: 0 FieldValue: Hidden item nobody will find&#10;But we are not limited to + our silly list,&#10;if value is set programmatically#10; FieldJustification: Left FieldStateOption: Hidden item nobody will find FieldStateOption: Item I'm very proud of FieldStateOption: My sneaky item #1

    Update: Oops, sorry, of course the "/1" above must be changed to "1" (Integer, not Name). It works as it were only because of tolerance for arrogant fools. Sorry again :-)

      Thanks a lot!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1202056]
Approved by kcott
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2020-12-01 00:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?