在ghost脚本中,裁剪标记没有设置在正确的位置。

问题描述 投票:0回答:1

我尝试在pdf文件中添加裁剪标记。但裁剪标记没有设置在右侧或顶部的适当角落。我试图通过postscript获取页面大小并设置标记,但我的postscript获取了新的页面宽度和高度,并设置了标记,但标记的位置不对,我的ghost命令是:- gswin64c. exe -o E:\output.pdf -dBATCH -dNOPAUSE -dDOPDFMARKS -sDEVICE=pdfwrite -dFIXEDMEDIA -dPDFFitPage -dDEVICEWIDTHPOINTS=396 -dDEVICEHEIGHTPOINTS=612 test.ps -f E:\comic.pdf。

既有输出文件链接,又有输入或输出文件链接输入PDF输出PDF

而我的裁剪标记后记为

    <<
/BeginPage {
    /count exch def % of previous showpage calls for this device
} bind
/EndPage {

    /pagewidth  currentpagedevice /PageSize get 0 get def
    /pageheight currentpagedevice /PageSize get 1 get def
    /y pageheight 9 sub def
    /x pagewidth 9 sub def

    newpath
    -1 -1 moveto
    0 9 rlineto
    10 0 rlineto
    0 -10 rlineto
    -10 0 rlineto
    closepath
    gsave
    grestore
    1 setlinewidth
    stroke


    newpath
    x -1 moveto
    0 9 rlineto
    10 0 rlineto
    0 -10 rlineto
    -10 0 rlineto
    closepath
    gsave
    1 setlinewidth
    grestore
    stroke


    newpath
    x y moveto
    0 10 rlineto
    10 0 rlineto
    0 -10 rlineto
    -10 0 rlineto
    closepath
    gsave
    grestore
    1 setlinewidth
    stroke

    newpath
    -1 y moveto
    0 10 rlineto
    10 0 rlineto
    0 -10 rlineto
    -10 0 rlineto
    closepath
    gsave
    grestore
    1 setlinewidth
    stroke


    % return (output=) true only for showpage.
    code 0 eq
}
>> setpagedevice
shell pdf ghostscript postscript
1个回答
1
投票

理想情况下,当你运行BeginPage或EndPage程序时,你不应该简单地在任何当前字典中创建新的键值对。原因是你不能完全依赖在执行这些程序时,哪个字典恰好在 dictioanry 堆栈的顶部,而且它可能是不同的。

这可能会导致你所期望的键没有被定义,并且(有可能)导致你覆盖已经在字典中的键值对。

事实上,在PostScript中,它通常被认为是更好的做法,根本不在字典中定义瞬时变量,而只是将它们存储在堆栈中,并从那里访问它们。这就避免了覆盖值的可能性,而且堆栈操作通常比字典操作快,所以也有一个(轻微的)性能优势。

你的EndPage存储过程执行了所有的计算和笔划,即使它不打算将页面传送到设备上,这显然是浪费的,你应该用if代替。你把gsave和grestore一起作为一对使用,这是毫无意义的,而且从性能上来说非常昂贵。这是对图形状态的保存和还原,如果你不在中间做任何事情,那么它就没有任何影响。

最后,你的BeginPage过程定义了count,但你似乎并没有真正用它来做任何事情!如果我是你,我会重写一遍。

如果我是你的话,我会把这些改写成。

/BeginPage {
    userdict /MyDict known    %% is MyDict alreadly present in userdict ?
    {
        userdict /MyDict get  %% If it is, then get it
    }
    {
        userdict begin        %% start userdict (makes it top element on dict stack)
        /MyDict               %% Put key /MyDict on stack
                              %%   stack - /MyDict
        5 dict                %% make a 5 element dictionary
                              %%   stack - /MyDict -dict-
        dup                   %% make a copy (duiplicates a pointer)
                              %%   stack - /MyDict -dict -dict-
        3 1 roll              %% rotate the stack
                              %%   stack -dict /MyDict -dict
        put                   %% Put the top element on the stack in the current
                              %% dictionary, using the key second top on the stack
                              %%    stack -dict-
        end                   %% close userdict
    } ifelse
    begin                     %% make the top element on the stack the current
                              %% dictionary.
    /count exch def           %% store the count ot pages in the currnt dictionary
                              %% using the key /count.
    end                       %% close the current dictionary
}

很明显,你可以删除所有的注释,它们纯粹是解释性的。从这一点开始,你可以通过doing找到与count相关的值。

userdict /MyDict get /count get

Then:

/EndPage {
                                 %% We enter with the reason code on the stack
    0 eq {                       %% We only want to take action for 0 (showpage)
      gsave
      currentpagedevice          %% Get the current page device dictionary
                                 %%    stack -dict-
      /PageSize get              %% get the value associated with the key /PageSize
                                 %% from that dictionary.
                                 %%    stack -array-
      dup dup                    %% make some copies of the array
                                 %%     stack -array- -array- -array-
      0 get                      %% get element 0 from the array
                                 %%     stack -array- -array- 'x'
      9 sub                      %% subtract 9 from the value
      0 put                      %% put that into array element 0
                                 %%      stack -array-
      dup dup 1 get              %% repaeat for element 1
      9 sub 1 put

      newpath
      -1 -1 moveto
      0 9 rlineto
      10 0 rlineto
      0 -10 rlineto
      -10 0 rlineto
      closepath

      dup 0 get                  %% duplicate the array, get the 0th element
      -1 moveto                  
      0 9 rlineto
      10 0 rlineto
      0 -10 rlineto
      -10 0 rlineto
      closepath

      dup 0 get                  %% duplicate the array, get the 0th element
                                 %% stack -array- 'x'
      1 index                    %% copy the element one down the stack to the top
                                 %% stack -array- 'x' -array-
      1 get                      %% get element 1 of the array
                                 %% stack -array- 'x' 'y'
      moveto
      0 10 rlineto
      10 0 rlineto
      0 -10 rlineto
      -10 0 rlineto
      closepath

      1 get                      %% get element 1 from the array
      -1 exch moveto
      0 10 rlineto
      10 0 rlineto
      0 -10 rlineto
      -10 0 rlineto
      closepath

      1 setlinewdith
      stroke

      grestore
      true
    }
    {
      false
    }ifelse
}

请注意,我没有测试这段代码,它只是为了说明问题。

现在真正的重点是,你的笔画错位的原因是你缩放了页面。

-dFitPage的工作原理是通过调整当前的变换矩阵来缩放页面内容,使它们适合新的页面尺寸。一切 之后的内容将被缩放,这包括EndPage过程中的路径构造。

为了证明这一点,请尝试执行命令行,不要使用-dFIXEDMEDIA等命令,你应该会看到页面的大小和笔画的位置都是正确的。

为了正确地放置笔画,你需要知道已经应用了什么比例因子,并将坐标值乘以该值的倒数。也就是说,如果比例系数是0.5(即一半大小),你需要将位置乘以2。


0
投票

这很有趣。测试了只放裁剪标记,没有错误却仍然没有反比例,所以不适合即时调整大小。决定是否转为两步重分将取决于需求,因为现在两步法可以工作,而未来额外的开发可以实现一体化的结果。

<<  /EndPage { exch pop   % page number unneeded

0 eq { pop       % ifelse

gsave

currentpagedevice /PageSize get
dup 0 get 9 sub exch 1 get 9 sub

2 array astore       % put in new array

newpath
-1 -1 moveto
0 9 rlineto
10 0 rlineto
0 -10 rlineto
-10 0 rlineto
closepath
1 setlinewidth
stroke

newpath
%    x
dup 0 get
-1 moveto
0 9 rlineto
10 0 rlineto
0 -10 rlineto
-10 0 rlineto
closepath
1 setlinewidth
stroke

newpath
%    x y 
dup aload pop
moveto
0 10 rlineto
10 0 rlineto
0 -10 rlineto
-10 0 rlineto
closepath
1 setlinewidth
stroke

newpath
1 get
%    -1 
%    y
-1 exch
moveto
0 10 rlineto
10 0 rlineto
0 -10 rlineto
-10 0 rlineto
closepath
1 setlinewidth
stroke

grestore

% return (output=) true only for showpage.
true}{pop false}ifelse

}
 >> setpagedevice

%showpage   % shortcut for testing

EDIT:这里是两步。

gs -o comic11-cropmarks.pdf -dBATCH -dNOPAUSE -dDOPDFMARKS -sDEVICE=pdfwrite cropmarks1.ps -f comic11.pdf
gs -o comic11-resized.pdf -dBATCH -dNOPAUSE -dDOPDFMARKS -sDEVICE=pdfwrite -dFIXEDMEDIA -dPDFFitPage -dDEVICEWIDTHPOINTS=396 -dDEVICEHEIGHTPOINTS=612 -f comic11-cropmarks.pdf

EDIT2:这些重新蒸馏会导致dpi的改变,所以如果需要300dpi的分辨率,那么也可以用ghostscript来改变。这里是关于这两个步骤的结果的一些信息。

$ pdfimages -list comic11.pdf tmp/
tmp/-0000.ppm: page=1 width=2063 height=3150 hdpi=300.00 vdpi=300.00 colorspace=DeviceRGB bpc=8
tmp/-0001.pgm: page=1 width=2063 height=3150 hdpi=300.00 vdpi=300.00 colorspace=DeviceGray bpc=8

$ pdfimages -list comic11-cropmarks.pdf tmp/
tmp/-0000.ppm: page=1 width=2063 height=3150 hdpi=300.00 vdpi=300.00 colorspace=DeviceRGB bpc=8
tmp/-0001.pgm: page=1 width=2063 height=3150 hdpi=300.00 vdpi=300.00 colorspace=DeviceGray bpc=8

$ pdfimages -list comic11-resized.pdf tmp/
tmp/-0000.ppm: page=1 width=2063 height=3150 hdpi=375.09 vdpi=375.09 colorspace=DeviceRGB bpc=8
tmp/-0001.pgm: page=1 width=2063 height=3150 hdpi=375.09 vdpi=375.09 colorspace=DeviceGray bpc=8

0
投票

这是一个一体化的解决方案。我不得不改变ghostscript命令行。

EDIT:这里是一个postscript全能漫画.ps,因为你说我之前的尝试页面大小不行。

%!
true setstrokeadjust
0.5 setlinewidth 1.0 0.9 0.9 setrgbcolor
[ 7.7 10 381 591 ] dup rectfill
1 0 0 setrgbcolor rectstroke 0.9 0.9 1.0 setrgbcolor
[ 15 18 366.5 575.5 ] dup rectfill
0.3 setlinewidth 0 0 1 setrgbcolor rectstroke
0 0 0 setrgbcolor
[ 7.7 10 -10 -10 ] rectstroke
[ 7.7 601 -10 10 ] rectstroke
[ 388.5 601 10 10 ] rectstroke
[ 388.5 10 10 -10 ] rectstroke

/centre { dup stringwidth pop 2 div 396 2 div exch sub } def
/Helvetica-BoldOblique 10 selectfont

(ARTWORK MUST EXTEND PAST RED LINES ON ALL PAGES.) centre 500 moveto show
(THIS IS THE TRIM LINE.) centre 478 moveto show
(ARTWORK BEYOND THE TRIM LINE WILL FALL INTO SPINE) centre 456 moveto show
(OR BE CUT OFF. THE ACTUAL CUT MAY VARY UP TO 1/8" FROM) centre 434 moveto show
(THIS LINE TO EITHER SIDE DURING PRODUCTION.) centre 412 moveto show
(BLUE AREA IS THE "SAFE" ZONE.) centre 390 moveto show
(BE SURE ALL TEXT AND IMPORTANT IMAGES ARE) centre 368 moveto show
(KEPT WITHIN THE BLUE AREAS.) centre 346 moveto show
(ALL NON-WHITE BORDERS AND FILLS) centre 324 moveto show
(SHOULD EXTEND TO EDGE OF PAGE. \(WHITE AREA\)) centre 302 moveto show
(TEMPLATE RESOLUTION IS SET TO 300 DPI.) centre 280 moveto show
(THIS IS PRINT-READY AND SHOULD NOT BE CHANGED.) centre 258 moveto show
1 0 0 setrgbcolor
(THESE NOTES ARE ON THE "TEMPLATE" LAYER) centre 136 moveto show
(AND SHOULD BE DELETED BEFORE EXPORTING) centre 114 moveto show
(FINAL FILES FOR PRINT.) centre 92 moveto show
showpage

这只是一个例子 后脚本可以通过编辑来解决其他问题。这是我的ghostscript命令行。

gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -r300 -dPDFFitPage -dFIXEDMEDIA -dDEVICEWIDTHPOINTS=396 -dDEVICEHEIGHTPOINTS=612 -sOutputFile=comic.pdf -f comic.ps

EDIT3: 把上面的postscript矩形叠加到重新调整过的comic11. pdf上 直到它们匹配为止,修正了发现的数字

EDIT4.将"<<<<<<<<<<<<<<<<<<<<<<<<<<。在上面的postscript开头添加"<< PageSize [396 612] >>setpagedevice",这样就可以用ps2pdf创建一个具有正确页面大小的pdf。

© www.soinside.com 2019 - 2024. All rights reserved.