Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Add text field to PDF

by vr (Curate)
on Nov 26, 2019 at 10:45 UTC ( [id://11109242]=note: print w/replies, xml ) Need Help??


in reply to Add text field to PDF

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' );

Replies are listed 'Best First'.
Re^2: Add text field to PDF
by Anonymous Monk on Nov 26, 2019 at 14:50 UTC

    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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11109242]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-19 09:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found