PhilterPaper / Perl-PDF-Builder

Extended version of the popular PDF::API2 Perl-based PDF library for creating, reading, and modifying PDF documents
https://www.catskilltech.com/FreeSW/product/PDF%2DBuilder/title/PDF%3A%3ABuilder/freeSW_full
Other
6 stars 7 forks source link

Annotation lost during page import #186

Open PhilterPaper opened 1 year ago

PhilterPaper commented 1 year ago

As reported by @sciurius in ssimms/pdfapi2/issues/51, an annotation is lost during page import.

#!/usr/bin/perl

use strict;
use warnings;

my $package = 'B'; # A=PDF::API2, B=PDF::Builder
if ($package eq 'A') {
  use PDF::API2;
} else {
  use PDF::Builder;
}

# Create document with annotation.
my $pdf;
if ($package eq 'A') {
  $pdf = PDF::API2->new;
} else {
  $pdf = PDF::Builder->new;
}
my $page = $pdf->page;
my $text = $page->text;
my $font = $pdf->corefont('Times-Roman');
$text->font($font,20);
if ($package eq 'A') {
  $text->transform( translate => [ 200, 200 ] );
} else {
  $text->transform( -translate => [ 200, 200 ] );
  #$text->translate( 200, 200 );
}
$text->text("Hello, World!");
my $ann = $page->annotation;
$ann->border( 0, 0, 1 );
$ann->rect( 100, 100, 200, 200 );
if ($package eq 'A') {
  $ann->uri("https://chordpro.org");
  $pdf->save('x1-51A.pdf');
} else {
  $ann->url("https://chordpro.org");
  $pdf->saveas('x1-51B.pdf');
}

## The document has text and an annotation that opens a site.

# Re-open document.
if ($package eq 'A') {
  $pdf = PDF::API2->open('x1-51A.pdf');
} else {
  $pdf = PDF::Builder->open('x1-51B.pdf');
}

# Create new document and import.
my $dst;
if ($package eq 'A') {
  $dst = PDF::API2->new;
} else {
  $dst = PDF::Builder->new;
}
$page = $dst->import_page( $pdf, 1, 1 );
if ($package eq 'A') {
  $dst->save('x2-51A.pdf');
} else {
  $dst->saveas('x2-51B.pdf');
}

## The document has text but no annotation.

Note that the transform() call in PDF::Builder has not yet been updated to accept "translate" (does support the old "-translate").