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.#!/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();
I cannot add bookmarks to a pdf if It already has bookmarks, nor can I delete all bookmarks.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 see what PDF::API2 try to tell me the right way to do modification# 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);
In reply to use PDF::API2 to add pdf bookmark by vincentaxhe
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |