【问题标题】:How to center image when creating images from PDF using GhostScript使用 GhostScript 从 PDF 创建图像时如何使图像居中
【发布时间】:2015-03-26 01:47:33
【问题描述】:

我有几个不同大小和不同宽高比的 pdf 文件。现在我想从这些文件的第一页创建固定大小的缩略图。 我使用以下命令执行此操作:

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

由于原始文件的宽度和高度不同,但缩略图的大小应相同,因此缩略图的右侧顶部会有白边.但我希望缩略图的顶部和底部(或左右)具有相等的边距(就像 Windows 资源管理器中显示的缩略图一样)。

有没有办法使用 GhostScript 来做到这一点?

【问题讨论】:

    标签: pdf centering ghostscript


    【解决方案1】:

    是的,但不是使用单个开关,也不是在使用 -dPDFFitPage 时。

    PDFFitPage 将同构地缩放内容(即在每个方向上都相同),因此您将在输出的顶部右侧有白色边距。

    为了使内容居中,您需要复制 PDFFitPage 的功能,并将原点在 x 或 y 方向上平移一半,无论哪个方向有剩余空间。

    您可以在 /ghostpdl/gs/Resource/Init/pdf_main.ps 中找到执行缩放的代码,查找 /pdf_PDF2PS_matrix 然后:

      //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
    

    请注意,据我所知,当前的实现已经将输出居中:

    % 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
    

    【讨论】:

    • 真可怜!我使用的是 8.7.1 版。当前版本(9.15)已修复此问题,谢谢
    猜你喜欢
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 2014-08-04
    • 2012-07-17
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    相关资源
    最近更新 更多