如何使用Ghostscript裁剪多页pdf和特定于页面的裁剪框数组 [英] How to crop a multipage pdf using ghostscript with an array of page specific cropboxes

查看:106
本文介绍了如何使用Ghostscript裁剪多页pdf和特定于页面的裁剪框数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此帖子是如何使用ghostscript在多页pdf中裁剪第3& 4页,但是它将输入更改为数据数组。

This post is a follow-up of "How do I crop pages 3&4 in a multipage pdf using ghostscript", but it changes the input to an array of data.

任务:我有一个pdf文件(一组图纸),其中所有页面的大小相同,我想用一种方式裁剪一些页面和其他人不同。以下屏幕快照显示了我如何生成以下数据进行裁剪:电子表格左侧,底部,右侧,顶部将移交给后记 / CropBox [934 350 3318 2034] 命令。页码只是连续的数字,因此可能不需要集会。

The quest: I have a pdf file (a set of drawings) where all pages have the same size and I want to crop some pages in one way and others differently. The following screenshot shows how I generated the data below for cropping:spreadsheet The "left,bottom,right,top" are to be handed over to the postscript /CropBox [934 350 3318 2034] command. Pagenumbers are just consecutive numbers, so they may not rally be needed.

page#,left,bottom,right,top

1   0   0   4252    2384
2   0   0   4252    2384
3   0   0   4252    2384
4   0   0   4252    2384
5   934 350 3318    2034
6   934 350 3318    2034
7   441 0   3811    2384
8   441 0   3811    2384

有了上述问题的解决方案,我就可以在多页pdf中裁剪特定页面,这可能是解决该问题的一个很好的起点。我只是自己想不通。

With the solution in the above mentioned question I was able to crop a specific page in an multipage pdf and it probably is a good starting point for a solution to this question. I just didn't figure it out on my own.

试图作为解决此问题的基础的相关后记代码是(由于KenS):

The relevant postscript code which tried to use as a base to solve this problem is (thanks to KenS):

<<
  /EndPage {
    0 eq {
      pop /Page# where {
        /Page# get
        3 eq {
          (page 3) == flush
          [/CropBox [0 0 1612 1792] /PAGE pdfmark 
          true
        }
        {
          (not page 3) == flush
          [/CropBox [500 500 612 792] /PAGE pdfmark
          true
        } ifelse
      }{
        true
      } ifelse
    }
    {
      false
    }
    ifelse
  }
>> setpagedevice

我想我们需要一些测试数字才能对我的疯狂页面大小字典进行现实的页面大小。 ..只是为了一些有趣的测试。

I guess we need some test numbers for "realistic" page sizes for my crazy page size dictionary... just for some fun testing.

/MyCrazyPageSizeDictionary begin
/PageSizeArray [
[0 0 595 842]       % original A4 portrait
[87 123 508 719]    % cut to A5
[149 210 446 631]   % cut to A6
[192 272 403 570]   % cut to A7
[223 316 372 526]   % cut to A8
] def


推荐答案

我建议您将每个页面的裁剪信息放置在一个数组中,然后将每个数组添加到一个封闭的数组中。问题可能是保留信息。

I'd suggest you place the cropping information for each page in an array, and then add each array to an enclosing array. The problem is likely to be retaining the information.

做到这一点的最佳方法可能是将页面信息数组创建为特定字典中的命名对象。 。如果您不创建自己的字典,那么将使用userdict。

The best way to do this, probably, is to create the array of page information as a named object in a specific dictionary. If you don't create your own dictionary, then userdict will be used instead.

然后在EndPage过程中,您只需拉出封闭数组的相关索引即可,您有多种作物大小:

Then in your EndPage procedure you simply pull the relevant index of the enclosing array, which gives you an array of crop sizes:

例如,

%!
/MyCrazyPageSizeDictionary 1 dict def
/MyCrazyPageSizeDictionary begin
/PageSizeArray [
[   0   0   4252    2384]
[   0   0   4252    2384]
[   0   0   4252    2384]
[   0   0   4252    2384]
[   934 350 3318    2034]
[   934 350 3318    2034]
[   441 0   3811    2384]
[   441 0   3811    2384]
] def
end

<<
  /EndPage {
    0 eq {
      pop /Page# where {
        /Page# get                                    % stack - pagenum
        /MyCrazyPageSizeDictionary /PageSizeArray get % stack - pagenum [[]]
        exch                                          % stack - [[]] pagenum
        get                                           % stack - []
        [ /CropBox                                    % stack - [] [ /CropBox
        3 -1 roll                                     % stack - [ /CropBox []
        /Page pdfmark
        true
      }{
        true
      } ifelse
    }
    {
      false
    }
    ifelse
  }
>> setpagedevice

如果将其放在文件(例如crop.ps)中,则通过Ghostscript,但将 crop.ps作为输入文件之一之前您的PDF文件:

If you put that in a file (named eg crop.ps) then run your PDF file through Ghostscript but put 'crop.ps' as one of the input files before your PDF file:

gs <options....> crop.ps input.pdf

然后它应该做您想做的事情。需要说明的是,我还没有以任何方式测试过该程序……

Then it should do what you want. With the caveat that I haven't tested this program in any way......

/MyCrazyPageSizeDictionary 1 dict def
MyCrazyPageSizeDictionary begin
/PageSizeArray [
[   0   0   4252    2384]
[   0   0   4252    2384]
[   0   0   4252    2384]
[   0   0   4252    2384]
[   934 350 3318    2034]
[   934 350 3318    2034]
[   441 0   3811    2384]
[   441 0   3811    2384]
] def
end

<<
  /EndPage {
    0 eq {
      pop /Page# where {
        /Page# get                                    % stack - pagenum
        1 sub                                         % array index is 0 based, page numbers start at 1
        MyCrazyPageSizeDictionary /PageSizeArray get  % stack - pagenum [[]]
        exch                                          % stack - [[]] pagenum
        1 index length mod                            % get array, find length, clamp page number to length
        get                                           % stack - []
        [ /CropBox                                    % stack - [] [ /CropBox
        3 -1 roll                                     % stack - [ /CropBox []
        /PAGE pdfmark
        true
      }{
        true
      } ifelse
    }
    {
      false
    }
    ifelse
  }
>> setpagedevice

这篇关于如何使用Ghostscript裁剪多页pdf和特定于页面的裁剪框数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆