Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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 :-)


In reply to Re: Add a Form Field in PDF by vr
in thread Add a Form Field in PDF by Arik123

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-26 01:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found