如何更改PDF的页面方向? (需要 Ghostscript 或 PostScript 解决方案)
给定一个 PDF 文档,如何更改单个页面方向?
我正在使用最新版本的 Ghostscript。
Given a PDF document, how do I change individual page orientation?
I'm using latest version of Ghostscript.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么您需要使用 Ghostscript?使用在命令行上运行的其他免费开源软件工具(例如
pdftk
)是否可以接受?不管怎样,这里是如何使用 Ghostscript 旋转页面。但是,这可能无法满足您的意图,因为您无法仅强制单个页面的特定方向。它依赖于内部 Ghostscript 算法,该算法尝试根据 PDF 内的文本流自动旋转页面:
*
-dAutoRotatePages=/None
-- 保留每个页面的方向;*
-dAutoRotatePages=/All
-- 根据某种“多数决定”旋转所有页面(或不旋转);*
-dAutoRotatePages=/PageByPage
-- 单独自动旋转页面。将其中之一添加到您正在使用的 Ghostscript 命令行中。
如果页面上没有文本(或者自动页面旋转设置为
/None
),则 Ghostscript 使用setpagedevice
设置。您可以使用-c
开关在 Ghostscript 命令行上传递此类setpagedevice
参数,如下所示:*
-c "<> setpagedevice"
-- 设置横向方向;*
-c "<> setpagedevice"
-- 设置纵向方向;*
-c "<> setpagedevice"
-- 设置上下颠倒方向;*
-c "<> setpagedevice"
-- 设置海景方向。提取页面时,您可能需要设置每个页面的方向。我认为将它们合并回统一文档时不会起作用(我从未测试过这一点)。
无论如何,我建议您也看看
pdftk
(它也适用于 Windows)。它是一个命令行工具,可以旋转 PDF 的页面等等。比 Ghostscript 更容易用于您指定的目的,而且速度也更快。特别是,它可以旋转 PDF 文档中的各个页面,而保持其他页面不变。 示例:此命令将输出第 1、2 和 3 页以及最后未旋转的第 5、6、... 页,但会将第 4 页旋转 90 度(因此页眉面向“西”)。 (但是,请注意,此命令可能会导致意外结果,具体取决于输入页面的原始方向:您应该通过运行
pdfinfo -l 1000 input 检查输入 PDF 的所有页面的方向。 pdf
,然后检查rot
输出的值:如果您看到的值与0
不同,例如90
、180
和270
,这些页面已预先旋转...)请参阅此处了解更多详细信息:http://www.accesspdf.com/pdftk/ 。
Why do you require usage of Ghostscript? Would it be acceptable to use another Free, Open Source Software tool running on the commandline, such as
pdftk
?Anyway, here is how to rotate pages with Ghostscript. However, this may not work for your intentions, because you cannot force a certain orientation for an individual page only. It relies on an internal Ghostscript algorithm that tries to rotate pages automatically, depending on the flow of text inside the PDFs:
*
-dAutoRotatePages=/None
-- retains orientation of each page;*
-dAutoRotatePages=/All
-- rotates all pages (or none) depending on a kind of "majority decision";*
-dAutoRotatePages=/PageByPage
-- auto-rotates pages individually.Add one of these to the Ghostscript commandline you're using.
If there is no text on a page (or if there is an automatic page rotation set to
/None
), then Ghostscript uses thesetpagedevice
settings. You can pass suchsetpagedevice
parameters on the Ghostscript commandline using the-c
switch like this:*
-c "<</Orientation 3>> setpagedevice"
-- sets landscape orientation;*
-c "<</Orientation 0>> setpagedevice"
-- sets portrait orientation;*
-c "<</Orientation 2>> setpagedevice"
-- sets upside down orientation;*
-c "<</Orientation 1>> setpagedevice"
-- sets seascape orientation.Probably you need to set the orientation for each page when extracting the pages. I don't think it would work when merging them back to the unified document (I have never tested this).
In any case, I'd recommend to look at
pdftk
too (which is also available for Windows). It is a commandline tool that can rotate pages from PDFs, and much more. Easier to use than Ghostscript for your stated purpose, and much faster as well. Especially, it can rotate individual pages inside a PDF document, leaving the other pages untouched. Example:This command will output pages 1, 2 and 3 as well as pages 5, 6, ... last un-rotated, but will rotate page 4 by 90 degrees (so the page header faces to the "west"). (However, be aware that this command can lead to unexpected results, depending on the original orientation of your input pages: You should check the orientation of all pages of your input PDF by running
pdfinfo -l 1000 input.pdf
and then check for the value of therot
output: if you see values different from0
, like90
,180
and270
, these pages are already pre-rotated...)See here for more details: http://www.accesspdf.com/pdftk/ .
除了 -dNORANGEPAGESIZE 之外,没有什么比它更适合我了。
Nothing else than -dNORANGEPAGESIZE worked perfectly for me.
我可以确认我尝试了几种不同的输入参数组合,唯一对我有用的组合是这两者的组合:
I can confirm that I tried several different combinations of input arguments and the only one that worked for me was the combination of these two: