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

Hello

I want to add a text field to an existing PDF. With text field I mean the square boxes you can add to your PDF and write text in it. I am experimenting with PDF::Builder which seems very easy to use and mantained. I have been succesful in adding a text to an existing PDF but nor a text field. I want the PDF user been able to move the field if necessary inside the PDF. In the PDF::Builder manual I could not find any referece to text fields.

use strict; use warnings; use PDF::Builder; my $pdf = PDF::Builder->open('in.pdf'); my $page_number=1; $page = $pdf->openpage($page_number); $font = $pdf->corefont('Helvetica-Bold'); # Add some text to the page my $text = $page->text(); #this creates text, I need a text box $text->font($font, 16); $text->translate(200, 700); $text->text('Hello World!'); # Save the PDF $pdf->saveas('out.pdf');

Replies are listed 'Best First'.
Re: Add text field to PDF
by vr (Curate) on Nov 26, 2019 at 10:45 UTC

    Your program adds text/graphics to page content. Page content is all about portability (hence "P" in "PDF"), it's (somewhat simplifying) translation from PostScript to PDF operators, and back if required, and guaranteed to produce same copy on hard medium i.e. paper. It's rather to be viewed as "etched in stone" and not supposed to be easily changed.

    What you want is interactive PDF capabilities, their support among PDF consumers is less portable. Though you say "text field", I don't think you mean a field as in fillable form -- if only for simple reason, that such field can not be dragged around a form (page) in Reader, as you request.

    More close would be a "text mark-up annotation", or "comment" in Reader UI. Historically, annotation of type "Text" is just a sticky note icon, with text in separate pop-up window. PDF::API2 (PDF::Builder) can add these "out of the box", maybe they are good enough for you, or even you like them better.

    Probably more close is "FreeType" annotation, which places editable/movable comment directly on page. It's not supported by PDF::API2, but requires just a couple "low level" touches. Note, neither annotation supplies their appearance stream (a bit more coding to do and docs to read, if you want them); fortunately Adobe Reader knows how to generate them on the fly, but other viewers can be clueless about you PDF, unless re-saved with e.g. Reader, but even then portability not guaranteed.

    use strict; use warnings; use PDF::API2; use PDF::API2::Basic::PDF::Utils; my $pdf = PDF::API2-> new; my $page = $pdf-> page; my $font = $pdf-> corefont( 'Helvetica-Bold' ); my $text = $page-> text; $text-> font( $font, 20 ); $text-> translate( 100, 700 ); $text-> text( 'Hello World!' ); my $sticky = $page-> annotation; $sticky-> text( 'Text in pop-up window', -rect => [ 100, 500, 100, 500 ], -open => 1 ); # not real +ly a bbox, # just cou +ple of coords $sticky-> { C } = PDFArray( map PDFNum( $_ ), 1, 0.65, 0 ); # orange, +make user happy my $free = $page-> annotation; $free-> { Subtype } = PDFName( 'FreeText' ); $free-> { DA } = PDFStr( '0 0 0 rg /Helv 12 Tf' ); # set text + size here $free-> content( 'This is a free text annotation' ); $free-> border( 0, 0, 0 ); # no borde +r $free-> rect( 100, 600, 300, 620 ); # real bbo +x this time $free-> { C } = PDFArray( map PDFNum( $_ ), 0.9, 1, 1 ); # faint bl +ue background $pdf->saveas( 'out.pdf' );

      awesome! This is perfect! Thank you. Question: I need to set the length of the rect. For this I need to measure the width of my text string. I normally used this:

      my $width = $text-> advancewidth( $string );

      Can you point me to how to measure the width of the text of inside the rect?

        Solved. Using

        my $width = $text-> advancewidth( $string );

        but remembering to use the same font in $text and $annotation works fine!