如何使用 pdftk 和 /MediaBox 裁剪 PDF 边距
我使用 pdftk
解压缩 PDF,然后将其作为文本文件打开。
我想编辑 /MediaBox 字段,在我的情况下,
/MediaBox [0 0 612 792]
我想减少边距,例如
/MediaBox [100 0 512 792]
不幸的是它不起作用。我可以将 0
更改为 2
或 9
,但我不能输入 100
。
知道为什么吗?
I used pdftk
to uncompress a PDF and then opened it as a text file.
I want to edit the /MediaBox field, which is in my case
/MediaBox [0 0 612 792]
I would like to reduce the margins, for instance
/MediaBox [100 0 512 792]
Unfortunately it doesn't work. I can change the 0
into a 2
or a 9
but I cannot put 100
for instance.
Any idea why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
字符串 100 中比 0 多了两个数字。当您使用文本编辑器并添加字符时,这会使文件更长。这就是为什么用 9 或 2 或任何其他单个数字替换效果很好。虽然理论上可以使用文本编辑器来编辑 pdf,但这并不简单,您必须尊重文件的内部结构。外部参照表是靠近 pdf 末尾的表格,它准确地告诉读者每个对象的位置。每当任何东西的长度或位置发生变化时,它都必须改变。
上面使用
pdftk
的手动方法不起作用的原因是您在文件的中心添加了两个字节。这会破坏外部参照
表。如果您手动更新所有外部参照
,这会起作用,但可能非常乏味。使用 sed 或任何其他文本编辑工具都无法解决问题。podofo
为您进行xref
计算。The string 100 has two more numbers in it than 0. When you use a text editor and add characters, that makes the file longer. That's why replacing with 9 or 2 or any other single digit works fine. While a text editor can theoretically be used to edit a pdf, it's not simple and you have to respect the internal structure of the file. The xref table is a table near the end of a pdf that tells the reader exactly where each object is located. It has to be changed whenever the length or location of anything is changed.
The reason the manual method above using
pdftk
doesn't work is that you are adding two bytes in the center of the file. This breaks thexref
table. If you manually update all thexref
s, this will work, but it is potentially very tedious. Usingsed
or any other text editing tool will not solve the problem.podofo
does thexref
calculation for you.使用 sed 替换任何出现
sed 's/MediaBox \[0 0 612 792*/MediaBox \[100 0 512 792 ]/g'out.pdf
或 podofobox(在 podofo utils 内)
无需先解压 pdf 流(根据需要with pdftk)
podofobox in.pdf out.pdf media 10000 0 51200 79200
如您所见,podofobox 使用 MediaBox 值乘以 100,因为它的比例是一个子倍数,所以,您只需添加两个零 (00) 表示您可以在 MediaBox 字段中读取的值
use sed to replace any occurrence
sed 's/MediaBox \[0 0 612 792*/MediaBox \[100 0 512 792]/g'<in.pdf >out.pdf
or podofobox (inside podofo utils)
without needing to uncompress pdf streams first (as needed with pdftk)
podofobox in.pdf out.pdf media 10000 0 51200 79200
as you can see, podofobox uses MediaBox values multiplied by 100, since its scale is a sub multiple, so, you need simply to add two zeroes (00) to values you can read in MediaBox field
有更好的方法可以更改 PDF 的边距:
希望您在发布后找到答案:-)
there are better ways to change the margin of a PDF:
hope you found an answer to that since posting :-)