我正在寻找一个工具来提取1页PDF文件的给定矩形区域(通过坐标),并生成具有指定区域的1页PDF文件:
# in.pdf is a 1-page pdf file
extract file.pdf 0 0 100 100 > out.pdf
# out.pdf is now a 1-page pdf file with a page of size 100x100
# it contains the region (0, 0) to (100, 100) of file.pdf
我可以将PDF转换为图像并使用转换,但这意味着生成的PDF将不再是矢量,这是不可接受的(我希望能够缩放).
理想情况下,我希望使用命令行工具或Python库来执行此任务.
谢谢!
解决方法:
找到以下脚本
http://snipplr.com/view.php?codeview&id=18924
将pdf的每一页拆分为2.
#!/usr/bin/env perl
use strict; use warnings;
use PDF::API2;
my $filename = shift;
my $oldpdf = PDF::API2->open($filename);
my $newpdf = PDF::API2->new;
for my $page_nb (1..$oldpdf->pages) {
my ($page, @cropdata);
$page = $newpdf->importpage($oldpdf, $page_nb);
@cropdata = $page->get_mediabox;
$cropdata[2] /= 2;
$page->cropbox(@cropdata);
$page->trimbox(@cropdata);
$page->mediabox(@cropdata);
$page = $newpdf->importpage($oldpdf, $page_nb);
@cropdata = $page->get_mediabox;
$cropdata[0] = $cropdata[2] / 2;
$page->cropbox(@cropdata);
$page->trimbox(@cropdata);
$page->mediabox(@cropdata);
}
(my $newfilename = $filename) =~ s/(.*)\.(\w+)$/$1.clean.$2/;
$newpdf->saveas('destination_path/myfile.pdf');