I write a perl script to add bookmarks to pdf as below
#!/bin/perl # add bookmark to pdf, at most two levels use strict; use PDF::Reuse; use File::Basename; my ($oldfile, $toc, $gap) = @ARGV; my $newfile = basename($oldfile, '.pdf') . '_new.pdf'; our $lastpage = 0; sub insertbookmark(@){ my ($chapter, @section) = @_ or return; my %items = (text => $chapter->{text}, act => $chapter->{act} ); @items{'close', 'kids'} = (1, \@section) if @section; prBookmark(\%items); } sub valid($){ my $line = shift; my ($text, $page) = $line =~ /^(.+)\t(\d+)$/ or die $line, 'is ill + formatted'; # ()have to escape $text =~ s/([()])/\\\1/g; warn $text, ' on page ', $page, ' must be wrong' if $page < $lastp +age; $lastpage = $page; return ($text, $page); } prFile($newfile); prDoc($oldfile) or die; open my $fh, '<', $toc or die; my @section; while (<$fh>){ chomp; my $flag = s/^\t//;# at most one tab my ($text, $page) = valid $_; $page += ($gap - 1); my $bookmark = { text => $text, act => "$page, 40, 700"}; unless ($flag){ insertbookmark @section; @section = (); } push @section, $bookmark; } insertbookmark @section; prEnd();
Lately I try use pdftk xxx.pdf dump_data_utf8 and to do some modification but I found the bookmarks are not encoded well, if I open with firefox or evince, the bookmarks of chinese characters are a mess, pdfs show fine only with zathura. I ask around, the given advice is to use PDF::API2 to do the work, but I can only add bookmarks to an bookmark-empty pdf as below, but the bookmark is well-encoded, show chinese characters well with firefox or evince.
use PDF::API2; use utf8::all; use File::Basename; my $oldfile = shift; my $newfile = basename($oldfile, '.pdf') . '_new.pdf'; my $pdf = PDF::API2->open($oldfile); my $item = $outline->outline(); $item->title('some chinese characters'); $item->destination($pdf->open_page(1)); $pdf->save($newfile);
I cannot add bookmarks to a pdf if It already has bookmarks, nor can I delete all bookmarks.
# perl bookmark writeruse PDF::API2; use PDF::API2; use utf8::all; use File::Basename; my $oldfile = shift; my $newfile = basename($oldfile, '.pdf') . '_new.pdf'; my $pdf = PDF::API2->open($oldfile); my $outline = $pdf->outline(); while (my $item = $outline->next()) { $item->delete(); print $i++, "\n"; } $pdf->save($newfile);
I cannot see what PDF::API2 try to tell me the right way to do modification

In reply to use PDF::API2 to add pdf bookmark by vincentaxhe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.