PDF::Builder::Annotation(3pm) | User Contributed Perl Documentation | PDF::Builder::Annotation(3pm) |
PDF::Builder::Annotation - Add annotations to a PDF
my $pdf = PDF::Builder->new(); my $font = $pdf->font('Helvetica'); my $page1 = $pdf->page(); my $page2 = $pdf->page(); my $content = $page1->text(); my $message = 'Go to Page 2'; my $size = 18; $content->distance(1 * 72, 9 * 72); $content->font($font, $size); $content->text($message); my $annotation = $page1->annotation(); my $width = $content->text_width($message); $annotation->rect(1 * 72, 9 * 72, 1 * 72 + $width, 9 * 72 + $size); $annotation->link($page2); $pdf->save('sample.pdf');
Note that the handling of annotations can vary from Reader to Reader. The available icon set may be larger or smaller than given here, and some Readers activate an annotation on a single mouse click, while others require a double click. Not all features provided here may be available on all PDF Readers.
$annotation = PDF::Builder::Annotation->new()
It is normally not necessary to explicitly call this method (see examples).
link
$annotation->link($page, %opts)
Note that $page is not a simple page number, but is a page structure such as "$pdf->openpage(page_number)", or a Named Destination defined elsewhere.
pdf, pdfile, pdf_file
$annotation->pdf($pdffile, $page_number, %opts)
$page_number is the physical page number, starting at 1: 1, 2,...
Alternate names: "pdfile" and "pdf_file"
Originally this method was named "pdfile", and then "pdf_file" but a recent PDF::API2 change made it "pdf". For compatibility, it has been changed to "pdf", with "pdfile" and "pdf_file" still available as aliases.
launch, file
$annotation->launch($file, %opts)
Alternate name: "file"
Originally this method was named "file", but a recent PDF::API2 change made it "launch". For compatibility, it has been changed to "launch", with "file" still available as an alias.
uri, url
$annotation->uri($url, %opts)
Alternate name: "url"
Originally this method was named "url", but a recent PDF::API2 change made it "uri". For compatibility, it has been changed to "uri", with "url" still available as an alias.
text
$annotation->text($text, %opts)
The option "text" is the popup's label string, not to be confused with the main $text.
The icon appears in the upper left corner of the "rect" selection rectangle, and its active clickable area is fixed by the icon (it is not equal to the rectangle). The icon size is fixed, and its fill color set by "color".
Additional options:
A reference to an icon may be passed instead of a name.
markup
$annotation->markup($text, $PointList, $highlight, %opts)
"text" is the popup's label string, not to be confused with the main $text.
There is no icon. Instead, the annotated text marked by $PointList is highlighted in one of four ways specified by $highlight.
Multiple sets of quadrilateral corners may be given, such as for highlighted text that wraps around to new line(s). The minimum is one set (8 numbers). Any AP dictionary entry will override the $PointList setting. Finally, the "Rect" selection rectangle is created just outside the convex bounding box defined by $PointList.
movie
$annotation->movie($file, $contentType, %opts)
The "rect" rectangle also serves as the area where the movie is played, so it should be of usable size and aspect ratio. It does not use a separate popup player. It is known to play .avi and .wav files -- others have not been tested. Using Adobe Reader, it will not play .mpg files (unsupported type). More work is probably needed on this annotation method.
file_attachment
$annotation->file_attachment($file, %opts)
The file, along with its name, is embedded in the PDF document and may be extracted for viewing with the appropriate viewer.
This differs from the "file" method in that "file" looks for and launches a file already on the Reader's machine, while "file_attachment" embeds the file in the PDF, and makes it available on the Reader's machine for actions of the user's choosing.
Note 1: some Readers may only permit an "open" action, and may also restrict file types (extensions) that will be handled. This may be configurable with your Reader's security settings.
Note 2: the displayed file name (pop-up during mouse rollover of the target rectangle) is given with the path trimmed off (file name only). If you want the displayed name to exactly match the path that was passed to the call, including the path, give the "notrimpath" option.
Options:
A reference to an icon may be passed instead of a name.
Note that while PDF permits different specifications (paths) to DOS/Windows, Mac, and Unix (including Linux) versions of a file, and different format copies to be embedded, at this time PDF::Builder only permits a single file (format of your choice) to be embedded. If there is user demand for multiple file formats to be referenced and/or embedded, we could look into providing this, although separate OS version paths may be considered obsolescent!.
rect
$annotation->rect($llx,$lly, $urx,$ury)
The default clickable area is the icon itself.
Defining option. Note that this "option" is actually required.
border
$annotation->border(@b)
A border is used in annotations where text or some other material is put down, and a clickable rectangle is defined over it (rect). A border is not shown when an icon is being used to mark the clickable area.
A PDF Reader normally defaults to [0 0 1] (solid line of width 1, with sharp corners) if no border ("/Border") is specified. Keeping compatibility with PDF::API2's longstanding practice, PDF::Builder defaults to no visible border "[0 0 0]" (solid line of width 0, and thus invisible).
Defining option:
Set annotation border style of horizontal and vertical corner radii "CRh" and "CRv" (value 0 for squared corners) and width "W" (value 0 for no border). The PDF::Builder default is no border (while a PDF Reader typically defaults to no border ([0 0 0]), if no /Border entry is given). Optionally, a dash pattern array may be given ("on" length, "off" length, as one or more pairs). The default is a solid line.
The border vector seems to ignore the first two settings (corner radii), but the line thickness works, on basic Readers. The corner radii may work on some other Readers.
content
$annotation->content(@lines)
open
$annotation->open($bool)
Both are editable; the "open" form brings up the page with the entry area already open for editing, while "closed" has to be clicked on to edit it.
Defining option:
dest
$annotation->dest($page, I<fit_setting>)
"xyz" is the default fit setting, with position (left and top) and zoom the same as the calling page's ([undef, undef, undef]).
$annotation->dest($name)
Color
$annotation->Color(@color)
For link and url annotations, this is the color of the rectangle border (border given with a width of at least 1).
If an invalid array length or numeric value is given, a medium gray ( [0.5] ) value is used, without any message. If no color is given, the usual fill color is black.
Defining option:
Named colors (e.g., 'black') are not supported at this time.
text string
text => string
2023-12-15 | perl v5.36.0 |