I've been trying for days to get a CheckBox or Radio Button to render using PDF::API2 and haven't been able to.
I've poured over the PDFMark reference, PDF specification, and any examples I've been able to find. I can get simple Widget annotations to render, but haven't been able to get anything that requires an appearance stream or appearance dictionary to work correctly. Below is a selection of test code attempting to set up a checkbox:
#!/usr/bin/perl
use PDF::API2;
use PDF::API2::Basic::PDF::Utils;
# set up pdf
my $pdfOptions = {};
my $pdf = PDF::API2->new( \$pdfOptions );
my $page = $pdf->page();
$page->mediabox( 'Letter' );
my $AcroForm = PDFDict();
$AcroForm->{NeedAppearances} = PDFBool( 'true' );
$AcroForm->realise;
my @Annots;
my @Fields;
my $resourceObj = PDFDict();
$resourceObj->{Type} = PDFName( 'Font' );
$resourceObj->{Subtype} = PDFName( 'Type1' );
$resourceObj->{Name} = PDFName( 'ZaDb' );
$resourceObj->{BaseFont} = PDFName( 'ZapfDingbats' );
$resourceObj->realise();
$AcroForm->{DR} = PDFDict();
$AcroForm->{DR}->{Font} = PDFDict();
$AcroForm->{DR}->{ZaDb} = $resourceObj;
$AcroForm->realise();
my $item = PDFDict();
$item->{P} = $page;
$item->{Type} = PDFName( 'Annot' );
$item->{Subtype} = PDFName( 'Widget' );
$item->{FT} = PDFName( 'Btn' );
my $yes = PDFName( 'Yes' );
my $off = PDFName( 'Off' );
$item->{P} = $page;
$item->{Type} = PDFName( 'Annot' );
$item->{Subtype} = PDFName( 'Widget' );
$item->{Rect} = PDF::API2::Basic::PDF::Literal->new( "[100 300 200 400]" );
$item->{FT} = PDFName( 'Btn' );
$item->{T} = PDFStr( 'Urgent' );
$item->{V} = PDFName( 'Yes' );
$item->{AS} = PDFName( 'Yes' );
$item->{AP} = PDFDict();
$item->{AP}->{N} = PDFDict();
# My understanding is that these would be nulled to be used with NeedAppearances
$item->{AP}->{N}->{$yes} = PDFNull();
$item->{AP}->{N}->{$off} = PDFNull();
$item->realise();
push @Annots, $item;
push @Fields, $item if( $AcroForm );
$page->{Annots} = PDFArray( @Annots );
$AcroForm->{Fields} = PDFArray(@Fields) if( $AcroForm );
$pdf->{Root}->{AcroForm} = $AcroForm if( $AcroForm );
print $pdf->stringify();
exit;
I would expect to see a checkbox rendered towards the middle of this page, instead I get an empty, unusable annotation. I'm trying to get the NeedAppearances flag to work, as I'd given up attempting a proper appearance stream/appearance dictionary, but I would be grateful for solutions that use either method.