【问题标题】:How to shift PDF page using perl (CAM::PDF, PDF::API2)?如何使用 perl (CAM::PDF, PDF::API2) 移动 PDF 页面?
【发布时间】:2010-04-09 03:20:17
【问题描述】:

我有一个 PDF 文档,我需要将页面向右移动几英寸。即喜欢在页面的左侧放置一个边距。

CAM::PDF 或 PDF::API2 都可以吗? 或者有没有人有这方面的经验?

谢谢。

【问题讨论】:

  • 可以通过设置 Margins 和 HWMargin 使用 ghostscript 来完成,但我想在 Perl 中完成,而不是通过系统调用外部命令。

标签: perl pdf ghostscript


【解决方案1】:

我是CAM::PDF 的作者。以下小程序将页面内容右移 100 点。

use CAM::PDF;
my $pdf = CAM::PDF->new('my.pdf');
my $page = $pdf->getPage(1);
$page->{MediaBox}->{value}->[0]->{value} -= 100;
$page->{MediaBox}->{value}->[2]->{value} -= 100;
$pdf->cleanoutput('out.pdf');

我使用了“使用 Data::Dumper;打印 Dumper($page);”提醒自己 $page 数据结构。

【讨论】:

    【解决方案2】:

    这就是我在 PDF::API2 中的做法:

    use PDF::API2;
    
    my $in  = PDF::API2->open('/path/to/file.pdf');
    my $out = PDF::API2->new();
    
    # Choose your margin (72 = one inch)
    my $x_offset = 72;
    my $y_offset = 0;
    
    foreach my $page_num (1 .. $in->pages()) {
        # Take the source page and import it as an XObject
        my $xobject = $out->importPageIntoForm($in, $page_num);
    
        # Add the XObject to the new PDF
        my $page = $out->page();
        my $gfx = $page->gfx();
        $gfx->formimage($xobject, $x_offset, $y_offset);
    }
    $out->saveas('/path/to/new.pdf');
    

    另一种可行的方法是调整媒体框(可能还有其他框)的坐标:

    use PDF::API2;
    
    my $pdf = PDF::API2->open('/path/to/file.pdf');
    
    # Choose your margin (72 = one inch)
    my $x_offset = 72;
    my $y_offset = 0;
    
    foreach my $page_num (1 .. $pdf->pages()) {
        my $page = $pdf->openpage($page_num);
    
        # Get the coordinates for the page corners
        my ($llx, $lly, $urx, $ury) = $page->get_mediabox();
    
        # Add the margin by shifting the mediabox in the opposite direction
        $llx -= $x_offset;
        $lly -= $y_offset;
        $urx -= $x_offset;
        $ury -= $y_offset;
    
        # Store the new coordinates for the page corners
        $page->mediabox($llx, $lly, $urx, $ury);
    }
    
    $pdf->saveas('/path/to/new.pdf');
    

    如果您遇到内容被截断的问题,您可能还需要获取并设置cropboxbleedboxtrimboxartbox 中的一个或多个,但这应该适用于大多数情况案例。

    【讨论】:

    • 我一直认为 PDF::API2 的 API 比我自己的模块 CAM::PDF 更好。如果我有大量空闲时间,我想将它们合并到单个超级 PDF 库中......
    【解决方案3】:

    您也可以使用Ghostscript 做到这一点。我会给你一些 Windows 的示例命令(使用 Unix 时,只需将 gswin32c.exe 替换为 gs):

    gswin32c.exe ^
       -o input-shifted-pages-1-inch-to-left.pdf ^
       -sDEVICE=pdfwrite ^
       -c "<</PageOffset [-72 0]>> setpagedevice" ^
       -f /path/to/input.pdf
    
    1. -o :指定输出文件。也隐式使用-dNOPAUSE -dBATCH -dSAFER
    2. -sDEVICE=... : 要求 Ghostscript 输出 PDF。
    3. -c &lt;&lt;... :在命令行上传递 PostScript 代码 sn-p 以实现页面移位
    4. -f ... :指定输入PDF(使用-c后需要-f)。

    /PageShift 使用的单位是 PostScript 点。 72 磅 == 1 英寸。值[-72 0] 将 72pt==1in 向左移动,0in 向顶部/底部移动。现在您知道如何向右移动 2 英寸了:

    gswin32c ^
       -o input-shifted-pages-2-inches-to-right.pdf ^
       -sDEVICE=pdfwrite ^
       -c "<</PageOffset [144 0]>> setpagedevice" ^
       -f /path/to/input.pdf
    

    想将 0.5 移到底部并向右移 1 英寸吗?

    gswin32c.exe ^
       -o input-shifted-pages-1-inch-to-right-half-inch-down.pdf ^
       -sDEVICE=pdfwrite ^
       -c "<</PageOffset [72 -36]>> setpagedevice" ^
       -f /path/to/input.pdf
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多