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.