Post without Account — your post will be reviewed, and if appropriate, posted under Anonymous. You can also use this link to report any problems registering or logging in.

RT 117942 - Add file attachment annotations

  • 10 Replies
  • 2668 Views
*

Offline Phil

  • Global Moderator
  • Sr. Member
  • *****
  • 430
    • View Profile
RT 117942 - Add file attachment annotations
« October 20, 2016, 08:09:37 PM »
Sun Sep 18 11:17:55 2016 jv [...] cpan.org - Ticket created
Subject:    Add file attachment annotations
 
The following patch against 2.028 adds file attachment annotations.
Subject:    PDF-API2-2.028-Annotation-attachment.patch
 
Code: [Select]
*** PDF/API2/Annotation.pm~ 2016-06-09 00:24:09.000000000 +0200
--- PDF/API2/Annotation.pm 2016-09-18 17:14:50.732315208 +0200
***************
*** 204,209 ****
--- 204,254 ----
      return($self);
  }
 
+ =item $ant->fileattachment $file, %opts
+
+ Defines the annotation as a file attachment with file $file and
+ options %opts (-rect, -border, -content (type), -icon (name)).
+
+ =cut
+
+ sub fileattachment {
+     my ( $self, $file, %opts ) = @_;
+
+     # https://books.google.nl/books?id=1TpDgwNGIK8C&pg=PA97&lpg=PA97&dq=pdf+embeddedfile+fileattachment&source=bl&ots=oGyUXkDBUT&sig=aftnbcBQlkOwcHLPdYxE4pcBIP4&hl=en&sa=X&ved=0ahUKEwjW2vnmjIrPAhXIfxoKHVsACTAQ6AEIQjAF#v=onepage&q=pdf%20embeddedfile%20fileattachment&f=false
+     #
+     # /Annots
+     #  [ << /FS << /EF << /F 8 0 R >> /F (attachment.txt) /Type /F >>
+     #    /Subtype /FileAttachment
+     #    /Contents (attachment.txt)
+     #    /Rect [ 18 796 45 823 ]
+     #  >> ]
+
+     my $contentype = delete $opts{-content};
+     my $icontype   = delete $opts{-icon};
+
+     $self->{Subtype} = PDFName('FileAttachment');
+     if ( is_utf8($file)) {
+         # URI must be 7-bit ascii
+         utf8::downgrade($file);
+     }
+     $self->{Contents} = PDFStr($file);
+     $self->{Name} = PDFName( $icontype || 'None');
+
+     $self->{FS} = PDFDict();
+     $self->{FS}->{F} = PDFStr($file);
+     $self->{FS}->{Type} = PDFName('F');
+     $self->{FS}->{EF} = PDFDict($file);
+     $self->{FS}->{EF}->{F} = PDFDict($file);
+     $self->{' apipdf'}->new_obj($self->{FS}->{EF}->{F});
+     $self->{FS}->{EF}->{F}->{Type} = PDFName('EmbeddedFile');
+     $self->{FS}->{EF}->{F}->{SubType} = PDFName($contentype) if $contentype;
+     $self->{FS}->{EF}->{F}->{' streamfile'}=$file;
+
+     $self->rect(@{$opts{-rect}}) if (defined $opts{-rect});
+     $self->border(@{$opts{-border}}) if (defined $opts{-border});
+     return($self);
+ }
+
  =item $ant->rect $llx, $lly, $urx, $ury
 
  Sets the rectangle of the annotation.

*

Offline Phil

  • Global Moderator
  • Sr. Member
  • *****
  • 430
    • View Profile
Re: RT 117942 - Add file attachment annotations
« Reply #1: November 03, 2016, 10:40:21 PM »
Thu Nov 03 19:16:32 2016 steve [...] deefs.net - Correspondence added

Merged, thanks.  This will be in v2.031.
#
Thu Nov 03 19:16:33 2016 The RT System itself - Status changed from 'new' to 'open'
#
Thu Nov 03 19:16:33 2016 steve [...] deefs.net - Status changed from 'open' to 'patched'
#
Thu Nov 03 19:36:13 2016 steve [...] deefs.net - Correspondence added

... with a couple of changes:

- renamed fileattachment to file_attachment

- renamed -content to -type

*

Offline Phil

  • Global Moderator
  • Sr. Member
  • *****
  • 430
    • View Profile
Re: RT 117942 - Add file attachment annotations
« Reply #2: November 04, 2016, 10:20:29 AM »
Fri Nov 04 03:16:38 2016 jv [...] cpan.org - Correspondence added

I forgot about this one... I've done a lot of development since and I have attached a much improved version. This version has proper appearance handling, and handles custom icons.

"file_attachment" means to me that it is going to file away (action) an attachment. "fileattachment" is a noun, which corresponds better to what it is. At least I think so, but it is probably not worth the discussion.
Subject:    fileattachment.pl

Code: [Select]
sub fileattachment {
    my ( $self, $file, %opts ) = @_;

    my $icon = $opts{-icon} || 'PushPin';
    my @r = @{ $opts{-rect}   } if defined $opts{-rect};
    my @b = @{ $opts{-border} } if defined $opts{-border};

    $self->{Subtype} = PDFName('FileAttachment');

    if ( is_utf8($file)) {
# URI must be 7-bit ascii
utf8::downgrade($file);
    }

    # 9 0 obj <<
    #    /Type /Annot
    #    /Subtype /FileAttachment
    #    /Name /PushPin
    #    /C [ 1 1 0 ]
    #    /Contents (test.txt)
    #    /FS <<
    #        /Type /F
    #        /EF << /F 10 0 R >>
    #        /F (test.txt)
    #    >>
    #    /Rect [ 100 100 200 200 ]
    #    /Border [ 0 0 1 ]
    # >> endobj
    #
    # 10 0 obj <<
    #    /Type /EmbeddedFile
    #    /Length ...
    # >> stream
    # ...
    # endstream endobj

    $self->{Contents} = PDFStr($file);
    # Name will be ignored if there is an AP.
    $self->{Name} = PDFName($icon) unless ref($icon);
    # $self->{F} = PDFNum(0b0);
    $self->{C} = PDFArray( map { PDFNum($_) } 1, 1, 0 );

    # The File Specification.
    $self->{FS} = PDFDict();
    $self->{FS}->{F} = PDFStr($file);
    $self->{FS}->{Type} = PDFName('F');
    $self->{FS}->{EF} = PDFDict($file);
    $self->{FS}->{EF}->{F} = PDFDict($file);
    $self->{' apipdf'}->new_obj($self->{FS}->{EF}->{F});
    $self->{FS}->{EF}->{F}->{Type} = PDFName('EmbeddedFile');
    $self->{FS}->{EF}->{F}->{' streamfile'} = $file;

    # Set the annotation rectangle and border.
    $self->rect(@r) if @r;
    $self->border(@b) if @b;

    # Set the appearance.
    $self->appearance($icon, %opts) if $icon;

    return($self);
}

sub appearance {
    my ( $self, $icon, %opts ) = @_;

    return unless $self->{Subtype}->val eq 'FileAttachment';

    my @r = @{ $opts{-rect}} if defined $opts{-rect};
    die "insufficient -rect parameters to annotation->appearance( ) "
      unless(scalar @r == 4);

    # Handle custom icon type 'None'.
    if ( $icon eq 'None' ) {
        # It is not clear what viewers will do, so provide an
        # appearance dict with no graphics content.

# 9 0 obj <<
#    ...
#    /AP << /D 11 0 R /N 11 0 R /R 11 0 R >>
#    ...
# >>
# 11 0 obj <<
#    /BBox [ 0 0 100 100 ]
#    /FormType 1
#    /Length 6
#    /Matrix [ 1 0 0 1 0 0 ]
#    /Resources <<
#        /ProcSet [ /PDF ]
#    >>
# >> stream
# 0 0 m
# endstream endobj

$self->{AP} = PDFDict();
my $d = PDFDict();
$self->{' apipdf'}->new_obj($d);
$d->{FormType} = PDFNum(1);
$d->{Matrix} = PDFArray( map { PDFNum($_) } 1, 0, 0, 1, 0, 0 );
$d->{Resources} = PDFDict();
$d->{Resources}->{ProcSet} = PDFArray( map { PDFName($_) } qw(PDF));
$d->{BBox} = PDFArray( map { PDFNum($_) } 0, 0, $r[2]-$r[0], $r[3]-$r[1] );
$d->{' stream'} = "0 0 m";
$self->{AP}->{N} = $d; # normal appearance
# Should default to N, but be sure.
$self->{AP}->{R} = $d; # Rollover
$self->{AP}->{D} = $d; # Down
    }

    # Handle custom icon.
    elsif ( ref $icon ) {
        # Provide an appearance dict with the image.

# 9 0 obj <<
#    ...
#    /AP << /D 11 0 R /N 11 0 R /R 11 0 R >>
#    ...
# >>
# 11 0 obj <<
#    /BBox [ 0 0 1 1 ]
#    /FormType 1
#    /Length 13
#    /Matrix [ 1 0 0 1 0 0 ]
#    /Resources <<
#        /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
#        /XObject << /PxCBA 7 0 R >>
#    >>
# >> stream
# q /PxCBA Do Q
# endstream endobj

$self->{AP} = PDFDict();
my $d = PDFDict();
$self->{' apipdf'}->new_obj($d);
$d->{FormType} = PDFNum(1);
$d->{Matrix} = PDFArray( map { PDFNum($_) } 1, 0, 0, 1, 0, 0 );
$d->{Resources} = PDFDict();
$d->{Resources}->{ProcSet} = PDFArray( map { PDFName($_) } qw(PDF Text ImageB ImageC ImageI));
$d->{Resources}->{XObject} = PDFDict();
my $im = $icon->{Name}->val;
$d->{Resources}->{XObject}->{$im} = $icon;
# Note that the image is scaled to one unit in user space.
$d->{BBox} = PDFArray( map { PDFNum($_) } 0, 0, 1, 1 );
$d->{' stream'} = "q /$im Do Q";
$self->{AP}->{N} = $d; # normal appearance

# Should default to N, but be sure.
$self->{AP}->{R} = $d; # Rollover
$self->{AP}->{D} = $d; # Down
    }

    return $self;
}

*

Offline Phil

  • Global Moderator
  • Sr. Member
  • *****
  • 430
    • View Profile
Re: RT 117942 - Add file attachment annotations
« Reply #3: January 26, 2017, 05:56:11 PM »
Thu Jan 26 13:20:37 2017 steve [...] deefs.net - Correspondence added

I'm about to do a release, so I've backed out your original patch and my changes, and am switching the status back to "open" so I can review your new patch in more detail later.
#
Thu Jan 26 13:20:43 2017 steve [...] deefs.net - Status changed from 'patched' to 'open'

*

Offline sciurius

  • Jr. Member
  • **
  • 67
    • View Profile
    • Website
Re: RT 117942 - Add file attachment annotations
« Reply #4: February 10, 2017, 07:49:37 AM »
I'll provide a new, heavily extended version of the fileattachment patch.

*

Offline Phil

  • Global Moderator
  • Sr. Member
  • *****
  • 430
    • View Profile
Re: RT 117942 - Add file attachment annotations
« Reply #5: February 10, 2017, 09:06:23 AM »
As I am trying to keep all of the "official" PDF::API2 (v2.xxx) fix stream incorporated into v3.xxx, please try to stay coordinated with whatever Steve does with 2.xxx (if you're still working with him). I'd like to have a common version that behaves identically, rather than two somewhat different versions. If you have submitted the new version to him, and he indicates that he is reviewing it, I would prefer to await his decision before "going it alone" on v3.xxx. If he rejects it, and does not want to add file attachment annotations, then I will strongly consider your new version. Thank you for keeping up on this!

P.S. Welcome aboard as the first non-spammer participant in this forum!

P.P.S. I will be looking for volunteers to try out and comment on the new file attachment annotation code, before committing it. Of course, I expect the author to test thoroughly, and I will eyeball the code and test as I can, but more eyes always help.

*

Offline sciurius

  • Jr. Member
  • **
  • 67
    • View Profile
    • Website
Re: RT 117942 - Add file attachment annotations
« Reply #6: February 11, 2017, 01:09:59 PM »
I now see that you copied the updated patch from RT, so the actual patch is already in reply #2.

*

Offline Phil

  • Global Moderator
  • Sr. Member
  • *****
  • 430
    • View Profile
Re: RT 117942 - Add file attachment annotations
« Reply #7: February 11, 2017, 07:05:49 PM »
Yes, it's there, but neither version of the patch is in 3.001, or the in-progress 3.002. I want to wait to see if Steve puts this in 2.032, and if not, what his reasons are. Of course, if he drops the ball and forgets about it, we will have to consider going on without him. I'd really like to get one or two other people to independently test this thing and pronounce themselves happy with it, before releasing it, as I won't claim much familiarity with annotations or attachments.

*

Offline Phil

  • Global Moderator
  • Sr. Member
  • *****
  • 430
    • View Profile
Re: RT 117942 - Add file attachment annotations
« Reply #8: February 16, 2017, 10:13:12 AM »
Wed Feb 15 13:38:42 2017 jv [...] cpan.org - Correspondence added

Any progress?

*

Offline Phil

  • Global Moderator
  • Sr. Member
  • *****
  • 430
    • View Profile
Re: RT 117942 - Add file attachment annotations
« Reply #9: December 25, 2017, 03:36:08 PM »
 PhilterPaper commented on Sep 11

on Mon Sep 11 03:51:37 2017 jvromans [...] squirrel.nl - Correspondence added

Please find attached the most recent version of the fileattachment method. This one has been in production for some six months and may be considered pretty stable and solid.

Regards, Johan

117942_fileattachment.pl  <not added>

 PhilterPaper commented on Nov 16

Anyone out there with "Border" experience? Per Johan's code contribution, I am trying to get a "-border" setting working, but it seems to have no effect. There is a /Border [ rx ry lw ] entry in what appears to be the right record, but it doesn't have visual effect at any point. I even tried directly editing a PDF file to move the /Rect [ llx lly urx ury ] entry (within the record) to just before the Border (as in examples in the PDF spec), but it didn't change anything.

Is the /Border entry something that some readers simply ignore, or is PDF::Builder producing bad code?

 PhilterPaper commented on Nov 16

I just pushed out a major update to incorporate Johan's new code, plus a bunch of related fixes and enhancements.

I am leaving this issue open for now, as several things don't seem quite right:

  • -border seems to have no effect
  • icons on "text" annotations are fixed size (quite small), although you can select an icon
  • "None" icon on "text" annotations ignored
  • I would like to be able to control the icon size. Right now, file_attachment stretches the icon to the full rectangle size, while text uses tiny fixed-size icons. Ideally, -iconsize=>[width, height], with either <0 meaning "default size", either =0 meaning "use rectangle size", and both >0 giving the actual icon size. Or, -iconsize=>'default' and -iconsize=>'rectangle' if that's feasible.
  • I have not tested reference icons, nor have I tested other annotation types.
   

*

Offline Phil

  • Global Moderator
  • Sr. Member
  • *****
  • 430
    • View Profile
Re: RT 117942 - Add file attachment annotations
« Reply #10: November 11, 2018, 06:05:15 PM »
I just pushed out a number of fixes to annotations, although I still can't get text and file_attachment icons to behave the same way. In general, everything's working a lot better. Even "movie" is somewhat working now, although it could use some more work.