How to center image when creating images from PDF using GhostScript

送分小仙女□ 提交于 2020-02-08 08:37:27

问题


I have several pdf files with different sizes and different width to height ratios. Now I want to create fixed-size thumbnails from 1st page of these files. I do this using this command:

gs -dNOPAUSE -sDEVICE=jpeg -dFirstPage=1 -dLastPage=1 -sOutputFile=d:\test\a.jpeg -dJPEGQ=100 -g509x750 -dUseCropBox=true  -dPDFFitPage=true -q  d:\test\a.pdf -c quit

Since the original files are of different widths and heights but thumbnails should be of the same size, there will be white margins in the right side or top of the thumbnails. But I want to have equal margins on top and bottom (or right and left) of the thumbnail (just like thumbnail displayed in windows explorer).

Is there any way to do it using GhostScript?


回答1:


Yes, but not with a single switch, and not while using -dPDFFitPage.

PDFFitPage will scale the content isomorphicallly (ie the same in each direction), so you will either have white margins at the top or the right of the output.

In order to centre the content, you need to duplicate the functionality of PDFFitPage, and also translate the origin in either the x or y direction, by half the 'excess' in whichever direction has space left over.

You can find the code which performs the scaling in /ghostpdl/gs/Resource/Init/pdf_main.ps, look for /pdf_PDF2PS_matrix and then:

  //systemdict /PDFFitPage known {
    PDFDEBUG { (Fiting PDF to imageable area of the page.) = flush } if
    currentpagedevice /.HWMargins get aload pop
    currentpagedevice /PageSize get aload pop
    % Adjust PageSize and .HWMargins for the page portrait/landscape orientation

Note that as far as I can see, the current implementation already does centre the output:

% stack: savedCTM <pdfpagedict> [Box] scale XImageable YImageable XBox YBox
3 index 2 index 6 index mul sub 2 div 3 index 2 index 7 index mul sub 2 div
PDFDEBUG { (    Centering translate by [ ) print 1 index =print (, ) print dup =print ( ]) = flush } if
translate pop pop pop pop


来源:https://stackoverflow.com/questions/28170597/how-to-center-image-when-creating-images-from-pdf-using-ghostscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!