使用GhostScript从PDF创建图像时如何居中图像 [英] How to center image when creating images from PDF using GhostScript

查看:167
本文介绍了使用GhostScript从PDF创建图像时如何居中图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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).

使用GhostScript有什么方法吗?

Is there any way to do it using GhostScript?

推荐答案

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

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

PDFFitPage将同构缩放内容(即,在每个方向上都是相同的),因此您将或者在输出的顶部的空白处保留空白.

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.

要使内容居中,您需要复制PDFFitPage的功能,并且还要在x或y方向上平移原点,将多余"的一半平移到剩下的空间.

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.

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

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

这篇关于使用GhostScript从PDF创建图像时如何居中图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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