如何使用 perl (CAM::PDF, PDF::API2) 移动 PDF 页面?

发布于 2024-08-28 05:00:48 字数 110 浏览 6 评论 0原文

我有一个 PDF 文档,我需要将页面向右移动几英寸。即就像在页面的左侧添加页边距。

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

谢谢。

I have a PDF document which I need to shift the pages to the right several inches. I.e like putting a margin on the left hand side of the page.

Can either CAM::PDF or PDF::API2 do it?
Or is there anyone have experience with it?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷你仙 2024-09-04 05:00:48

我是 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');

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

I'm the author of CAM::PDF. The following little program shifts the page contents right by 100 points.

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');

I used "use Data::Dumper; print Dumper($page);" to remind myself of the $page data structure.

起风了 2024-09-04 05:00:48

这是我在 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 中的一个或多个,但这在大多数情况下应该有效。

Here's how I would do it in 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');

Another way that should work is to adjust the coordinates for the mediabox (and possibly other boxes):

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');

If you run into issues with content getting cut off, you may need to get and set one or more of cropbox, bleedbox, trimbox, and artbox as well, but this should work in most cases.

你与昨日 2024-09-04 05:00:48

您也可以使用 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 <<... :在命令行上传递的 PostScript 代码片段,以实现页面转换
  4. -f ... :指定输入 PDF (<使用-c后需要code>-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

You can do that also with Ghostscript. I'll give you some example commands for Windows (when using Unix, just replace gswin32c.exe by 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 : specifies the output file. Implicitly also uses -dNOPAUSE -dBATCH -dSAFER.
  2. -sDEVICE=... : asks Ghostscript to output PDF.
  3. -c <<... : a PostScript code snippet passed on the commandline to make happen the page shift
  4. -f ... : specifies the input PDF (-f is required after using -c).

The units used by /PageShift are PostScript points. 72 pt == 1 inch. The value [-72 0] shifts 72pt==1in to the left and 0in to top/bottom. Now you know how to shift 2 inches to the right:

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

Wanna shift 0.5 in to the bottom and 1 inch to the right?

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文