Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Filling PDF Form Checkboxes

by steve (Deacon)
on Feb 27, 2009 at 19:45 UTC ( [id://747009]=perlquestion: print w/replies, xml ) Need Help??

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

I am essentially working toward he same end posted by merlyn some time ago in Filling out PDF forms with data from DBI?. The reply posted in Re^2: Filling out PDF forms with data from DBI? is very helpful, but indicates that for checkboxes:
On this particular form I noticed that the check boxes were not form fields. Randal, does your client require a check mark on the document, e.g. for "sole proprietor" or "Corporation?" The only reason I ask is because I don't see an easy means to add a check mark using CAM::PDF. PDF::API2 or PDF::Reuse can do that easily. However, they don't have the easy interface into forms the way CAM::PDF does. It might be a bit of a kludge, but running through CAM::PDF to fill in the forms and then running through PDF::API2 to add any checkmarks (or any kind of glyphs or graphics) would be pretty easy.

I would like very much to avoid this method of populating checkboxes because it effectively breaks the PDF form -- that is to say that once something is printed over the top of a checkbox it can no longer be operated as a checkbox.

I am not certain if it is because CAM::PDF or the PDF format has changed significantly since then, but getFormFieldList within CAM::PDF actually does return checkbox keys which are (to some degree) accessible through the same CAM::PDF object.

I have used CAM::PDF to access the dictionary objects for these checkboxes using the W-9 at http://www.irs.gov/pub/irs-pdf/fw9.pdf as a test case. I have compared the objects for each checkbox from a form that is saved with and without the boxes checked. The checked boxes appear like so (the following example is from the first checkbox in the form, using Data::Dumper):
$VAR1 = { 'FT' => bless( { 'value' => 'Btn', 'gennum' => '0', 'type' => 'label', 'objnum' => '31' }, 'CAM::PDF::Node' ), 'DA' => bless( { 'value' => '/ZaDb 9.0 Tf 1.000 0.660 0.000 +0.180 k ', 'gennum' => '0', 'type' => 'string', 'objnum' => '31' }, 'CAM::PDF::Node' ) };
and the checked boxes seem to have a small portion added:
$VAR1 = { 'FT' => bless( { 'value' => 'Btn', 'gennum' => '0', 'type' => 'label', 'objnum' => '31' }, 'CAM::PDF::Node' ), 'DA' => bless( { 'value' => '/ZaDb 9.0 Tf 1.000 0.660 0.000 +0.180 k ', 'gennum' => '0', 'type' => 'string', 'objnum' => '31' }, 'CAM::PDF::Node' ), 'V' => bless( { 'gennum' => '0', 'value' => 'Yes', 'type' => 'label', 'objnum' => '31' }, 'CAM::PDF::Node' ) };
I have started with the following code that modifies the object to include the missing section, however I am still not seeing it in the form.
#!/usr/bin/perl # pdf-filler-test.pl use strict; use warnings; use CAM::PDF; use Data::Dumper; my $infile = 'fw9.pdf'; my $outfile = 'modified_fw9.pdf'; my $pdf = CAM::PDF->new($infile) or die "Cannot open $infile"; my @FIELDS = $pdf->getFormFieldList(); use Data::Dumper; foreach my $field ( @FIELDS ) { if ($field =~ /^c/) { my $ff_obj = $pdf->getFormField($field); my $dict = $pdf->getFormFieldDict($ff_obj); $dict->{V} = CAM::PDF::Node->new('label', 'Yes', $ff_obj->{obj +num}, $ff_obj->{gennum}); print Dumper $dict; } else { $pdf->fillFormFields($field => $field); } } $pdf->fillFormFields( 'f1_01(0)' => 'name', 'f1_02(0)' => 'test', ); $pdf->cleanoutput($outfile);
I am not sure if there is just something I am missing or if this really is not feasible, but would gladly welcome suggestions.

Replies are listed 'Best First'.
Re: Filling PDF Form Checkboxes
by ruzam (Curate) on Feb 28, 2009 at 17:55 UTC

    I once spent a little time with CAM::PDF (after reading the same sources you did). I intended to use it for form field replacement (as you are). My experience was that the documentation and examples didn't quite match the layout that was coming from my PDF form source. At the time I was using open source PDF editors to create the forms and there were problems just generating a valid form to start with (form fields were just too advanced and buggy for the edit tool). In the end, I gave up on using forms, and simply created my PDFs from scratch with PDF::ReportWriter, eventually contributing more formatting features which can be found on Daniel's website http://entropy.homelinux.org/axis/PDF-ReportWriter/.

    My failure make CAM::PDF work was due to invalid PDF form sources. With valid form sources, I'm sure it will work fine, but maybe not exactly the way the examples show it. This probably doesn't help you much, but I hated to see the question go unanswered.

Re: Filling PDF Form Checkboxes
by jwagon (Initiate) on Jun 17, 2009 at 01:57 UTC

    Just came across this while searching for same--I'm using the Federal W4 form, and it's set up similarly.

    First of all, trying to modify the object returned by getFormFieldDict() isn't going to get you anywhere. It says right in the docs that it's supposed to be treated as a read-only construct; it's not really a pointer to the dictionary object.

    Second, after much futzing around, I figured out that something like this works (snipping from your code above):

    foreach my $field ( @FIELDS ) { if ($field =~ /^c/) { my $ff_obj = $pdf->getFormField($field); $ff_obj->{value}->{value}->{AS}->{value} = checkThisBox() ? 'Y +es' : 'Off'; } else { $pdf->fillFormFields($field => $field); } }

    I found that when a checkbox is checked manually, there are three properties that different from one that's not checked:

    1. $ff_obj->{value}->{value}->{AS}
    2. $ff_obj->{value}->{value}->{DV}
    3. $ff_obj->{value}->{value}->{V} (the one you found)

    However, changing the value of the 'value' key on 'DV' and 'V' didn't seem to do anything for me. That is, these statements didn't seem to do anything:

    • $ff_obj->{value}->{value}->{DV}->{value} = checkThisBox() ? 'Yes' : 'Off';
    • $ff_obj->{value}->{value}->{V}->{value} = checkThisBox() ? 'Yes' : 'Off';

    Reason being . . . 'DV' and 'V' stand for 'Default Value' and 'Value', respectively. In the inner workings of a PDF form, these values are useful for processing and whatnot, but unlike in the DOM or something like that, the *value* of the checkbox and whether an actual checkmark appears in that box are not necessarily the same (if you're setting them programmatically).

    Hence, the 'AS' key, which stands for 'Appearance State'. Setting the value AS's 'value' key thus places a checkmark there.

    So, depending on what you're doing with this form, you may also want to set 'DV' and/or 'V' the same way, like if you're actually presenting it as a web form complete with some Javascript or other logic. In my case, I'm just filling the forms in with data from a database and sending them straight to the printer, so I don't care about the actual checkbox values.

    Hope this helps.

Re: Filling PDF Form Checkboxes
by Anonymous Monk on Jul 29, 2020 at 14:47 UTC
    follwing code works for me though there is an error message "Unknown resource key 'Encoding' in form field dictionary at /usr/local/share/perl/5.26.1/CAM/PDF.pm line 2914."
    #!/usr/bin/perl -w use strict; use CAM::PDF; my $pdf = CAM::PDF->new('tenant.pdf') or die "Could not open PD +F ($!)!"; my @fields = $pdf->getFormFieldList(); foreach my $field ( @fields ) { if ($field =~ /Female/) { my $ff_obj = $pdf->getFormField($field); $ff_obj->{value}->{value}->{AS}->{value} = "On"; } else { $pdf->fillFormFields($field => $field); } } $pdf->cleanoutput('afilled.pdf');

      Upon further research on the checkbox issue, I just realized that {AS}->{value} is where you can control the option

      In the same PDF file that I worked in above instance, there is another kind of check box in the dictionary that is using indirectObject

      which points to a parent dictionary which has {AS}->{value}. In this instance {AS}->{value} was set "Off" initially but I had to use {D}->{value} which is inside of the same parent dictionary to set {AS}->{value} to turn check box on,in my case, it is "Yes_2", that is {AS}-{value}="Yes_2"

      I have used following improved code to mark my checkbox in pdf

      #!/usr/bin/perl -w  use strict; use CAM::PDF;  my $pdf = CAM::PDF->new('tenant.pdf') or die "Could not open PDF ($!)!";  $pdf->getFormFieldList();  my $field ="Female";  my $ff_obj    = $pdf->getFormField($field);        $ff_obj->{value}->{value}->{AS}->{value} = "On";   $pdf->cleanoutput('afilled.pdf');

Log In?
Username:
Password:

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

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

    No recent polls found