2010-06-01 180 views
5

是否可以在圖像中添加標題(包含文本和圖像)和頁腳(包含頁碼)。我寫下面的代碼來創建一個顯示png圖像的PDF文檔。如何使用PDF :: API2 :: Lite添加頁眉,頁腳和圖像?

如果這可以很容易地與任何其他模塊,請建議。真正明白反應與示例代碼。

use strict; 
use PDF::API2::Lite; 
use Getopt::Long; 

my $outfile; 
my $path; 

my $options = GetOptions("outfile=s" => \$outfile, 
          "images=s" => \$path,); 

my @images = sort glob("$path") or die "No Files\n"; 

my $pdf = PDF::API2::Lite->new(); 
for my $png (sort @images) { 
     my $image = $pdf->image_png("$png"); 
     $pdf->page(1150,450); 
     $pdf->image($image, 10, 10); 
} 

$pdf->saveas($outfile); 

回答

4

在SO上等了一天,節省了10分鐘讀取模塊文檔。這並不難,太空。

use PDF::API2 qw(); 

{ 
    my $pdf = PDF::API2->open('input.pdf'); 

    for my $index (1 .. $pdf->pages) { 
     my $page = $pdf->openpage($index); 
     my $txt = $page->text; 
     $txt->textlabel(300, 700, $pdf->corefont('Helvetica Bold'), 12, 'some Header text'); 

     my $gfx = $page->gfx; 
     $gfx->image($pdf->image_png('Header_image.png'), 150, 700); 

     $txt->textlabel(300, 100, $pdf->corefont('Helvetica Bold'), 12, "Page: $index"); 
    } 

    $pdf->saveas('output.pdf'); 
    $pdf->end; 
} 
+0

感謝Daxim:這段代碼是編輯現有的文檔。你能建議我如何使用它來創建帶有圖像的新文檔 – Space 2010-06-04 13:02:26

+0

使用['new'構造函數](http://p3rl.org/PDF::API2#GENERIC_METHODS)。請閱讀文檔。 – daxim 2010-06-04 15:04:44

+0

感謝daxim:你能否請我也建議我如何添加PDF文檔的屬性。 – Space 2010-06-09 07:08:14

1

看看PDF::API2::Simple代替。此CPAN模塊提供了一些圍繞PDF::API2的便利幫手方法,包括頁眉和頁腳。

下面是一個簡單的工作頁眉/頁腳例如:

use 5.012; 
use warnings; 
use PDF::API2::Simple; 

our $PageNo; 

my $pdf = PDF::API2::Simple->new(
    file => 'file.pdf', 
    header => \&header, 
    footer => \&footer, 
); 

$pdf->add_font('Verdana'); 

for my $page (1..3) { 
    $pdf->add_page; 
    $pdf->image('image.png', x => 300, y => 300); 
} 
$pdf->save; 


sub header { shift->text('Header text here') } 
sub footer { shift->text('page: ' . ++$PageNo, x => 10, y => 10) } 

/I3az/

4

PDF::API2是我對這樣的事情主力。

一旦我需要對現有的PDF文檔進行任何上傳或重新處理,我幾乎總是會使用importPageIntoForm方法。

作爲一個通用的解決方案,我創建一個新的PDF,逐頁,導入我想要鋪設的元素,然後添加額外的文本或圖形。

#!/usr/bin/perl 
use common::sense; 

use PDF::API2; 

my $infile = shift (@ARGV); 
my $outfile = shift (@ARGV); 

die "usage $0: infile outfile" 
unless $infile && $outfile; 

my $pdf_in = PDF::API2->open($infile); 
my $pdf_out = PDF::API2->new; 

foreach my $pagenum (1 .. $pdf_in->pages) { 

    my $page_in = $pdf_in->openpage($pagenum); 
    # 
    # create a new page 
    # 
    my $page_out = $pdf_out->page(0); 

    my @mbox = $page_in->get_mediabox; 
    $page_out->mediabox(@mbox); 

    my $xo = $pdf_out->importPageIntoForm($pdf_in, $pagenum); 

    # 
    # lay up the input page in the output page 
    # note that you can adjust the position and scale, if required 
    # 
    my $gfx = $page_out->gfx; 

    $gfx->formimage($xo, 
      0, 0, # x y 
      1); # scale 

    # 
    # add page number text 
    # 
    my $txt = $page_out->text; 

    $txt->strokecolor('#000000'); 

    $txt->translate(
      my $_x = 200, 
      my $_y = 50 
); 

    my $font = $pdf_out->corefont('Courier'); 
    $txt->font($font, 12); 
    $txt->text('Page: '.$pagenum); 

    # 
    # add header image 
    # 

    my $header_img = $pdf_out->image_png('SomeHeader.png'); 
    $gfx->image($header_img, 0, 400); 
} 

$pdf_out->saveas($outfile); 
+0

這是一個非常好的方法 - 而不是修改PDF的地方,使用它作爲模板來創建一個新的PDF。感謝您告訴我們關於importPageIntoForm()! – 2011-08-31 09:24:03

1

我已經在線查看關於使用PDF :: AP12的詳細信息,但細節非常詳細。在CPAN網站上看過,但是沒有說明在哪裏閱讀文檔,那麼您在哪裏找到詳細信息? (目前,CPAN命令行甚至不會識別PDF :: AP12 =或AP13 =作爲合法模塊!而且還沒有完全掌握下載tar文件並手動安裝到草莓Perl中)

我很好奇這個「importPageIntoForm」項目:如果我設計了一個使用DTP程序的主PDF頁面(對於標籤製作,即3寬8深),並在每個單元的右側放置了一個虛擬圖形(?),我是否可以交換這些出來用這個? (如果你喜歡,一個在線「郵件合併」替換圖形與個人QR碼C/O GD :: BARCODE :: QRcode)

或者最好從頭開始創建每個頁面與主程序?

+0

這是PDF :: API2不是PDF :: AP12 – 2016-12-17 22:56:20