手动编辑(a 的每一页)Postscript 文件
Edit (every page of a) Postscript file manually
我想(手动)在通过 ghostscript pdf2ps
从 PDF 文件转换而来的 postscript 文件中插入额外的 postscript 命令。出于测试目的,我使用 pdflatex
:
从以下文件创建了一个 PDF 文件
\documentclass[a4paper]{article}
\begin{document}
Mostly empty.
\end{document}
在转换后的 postscript 文件中,我进行了以下编辑:
...
%%Page: 1 1
%%PageBoundingBox: 0 0 595 841
%%BeginPageSetup
4 0 obj
<</Type/Page/MediaBox [0 0 595.28 841.89]
/Parent 3 0 R
/Resources<</ProcSet[/PDF]
/Font 8 0 R
>>
/Contents 5 0 R
>>
endobj
%%EndPageSetup
% BEGIN MANUAL EDIT
0 setgray 0 0 moveto 595 841 lineto stroke
% END MANUAL EDIT
5 0 obj
<</Length 257>>stream
q 0.1 0 0 0.1 0 0 cm
0 G
0 g
q
10 0 0 10 0 0 cm BT
/R6 9.9626 Tf
1 0 0 1 139.746 706.129 Tm
[(M)-0.699638(os)-0.399443(t)-0.900585(l)-0.798886(y)-333.819(e)-0.400668(m)-0.300195(p)-0.599165(t)26.0974(y)83.192(.)-0.800112]TJ
154.421 -615.691 Td
(1)Tj
ET
Q
Q
endstream
endobj
pagesave restore
%%PageTrailer
%%Trailer
end
%%EOF
postscipt/PDF 文件没有生成对角线,而是(看似)保持不变。但是,如果我将页面尺寸从 A4 更改为字母大小,则会显示该行:
%%Page: 1 1
%%PageBoundingBox: 0 0 612 792
%%BeginPageSetup
4 0 obj
<</Type/Page/MediaBox [0 0 612 792]
...
我显然在这里遗漏了一些东西(鉴于我对后记的基本了解,这并不奇怪)。我的问题是:如何在保持页面尺寸不变的情况下显示线条?
P.S.:我偶然发现的一条评论提到 pdftops
(来自 poppler-utils)在某种意义上优于 pdf2ps
。实际上,在 showpage
命令(使用 pdf2ps
时根本不存在)之前将命令插入到转换后的 postscript 文件中效果很好。所以我可能已经找到了解决问题的方法。但是,我想了解使用 pdf2ps
.
时页面尺寸与它有什么关系
解决方案
多亏了 KenS 的建议并参考了他对 this question 的回答,我才能够通过在 postscript 文件中添加一个 EndPage 程序来达到预期的效果:
<<
/EndPage
{
exch pop 2 lt
{
gsave
0 0 translate
0 setgray 0 0 moveto 596 842 lineto stroke
grestore
true
}{false} ifelse
} bind
>> setpagedevice
(假设页面大小为 a4。)
PostScript 是一种只写语言:-)
说真的,它是一种编程语言。为了了解发生了什么,您需要了解程序,对于 Ghostscript 的 ps2write 设备的输出,这显然是非常重要的。
语法基本上是 PDF,带有一个用 PostScript 术语解释它的序言程序。
程序将使用showpage,它在执行过程EndStream 时执行,这(基本上)是在页面流中遇到endobj 关键字时。你会看到它看起来像:
ET
Q
Q
Q
endstream
endobj
%%Page: 2 2
您可以在 endstream 和 endobj 之间放置任何您喜欢的东西,但您需要知道此时的图形状态是由已经发生的任何操作决定的。这可以包括缩放、演说、倾斜、翻转垂直轴等。因此,简单地向其中插入一些 PostScript 不太可能奏效。您可以执行 initgraphics,它至少会将图形状态重置为已知设置。
作为测试,我 运行 Ghostscript 的 ps2write 设备是这样的:
gs -sDEVICE=pdfwrite -o out.ps -c "showpage" -f
生成 PostScript 程序,其中(有效)内容为:
%%EndResource
%%EndProlog
%%Page: 1 1
%%PageBoundingBox: 0 0 595 842
%%BeginPageSetup
4 0 obj
<</Type/Page/MediaBox [0 0 595 842]
/Parent 3 0 R
/Resources<</ProcSet[/PDF]
>>
/Contents 5 0 R
>>
endobj
%%EndPageSetup
5 0 obj
<</Length 23>>stream
q 0.1 0 0 0.1 0 0 cm
Q
endstream
endobj
%%Trailer
end
%%EOF
然后我按照你的建议或多或少地修改了这个:
%%EndPageSetup
0 setgray 0 0 moveto 595 842 lineto stroke
5 0 obj
<</Length 23>>stream
q 0.1 0 0 0.1 0 0 cm
Q
endstream
endobj
%%Trailer
对我来说,这产生了从左下角到右上角的预期笔划。显然,如果没有您最初生成的 PostScript 文件,我无法告诉您为什么您的体验会有所不同。 (不,我无法 运行 latex 来制作这样的东西,即使我这样做了,我也无法知道你使用的是哪个版本的 Ghostscript 和其他工具)。
我的猜测是您的 PDF 文件中的 'something' 覆盖了整个页面,这并不少见。
我想(手动)在通过 ghostscript pdf2ps
从 PDF 文件转换而来的 postscript 文件中插入额外的 postscript 命令。出于测试目的,我使用 pdflatex
:
\documentclass[a4paper]{article}
\begin{document}
Mostly empty.
\end{document}
在转换后的 postscript 文件中,我进行了以下编辑:
...
%%Page: 1 1
%%PageBoundingBox: 0 0 595 841
%%BeginPageSetup
4 0 obj
<</Type/Page/MediaBox [0 0 595.28 841.89]
/Parent 3 0 R
/Resources<</ProcSet[/PDF]
/Font 8 0 R
>>
/Contents 5 0 R
>>
endobj
%%EndPageSetup
% BEGIN MANUAL EDIT
0 setgray 0 0 moveto 595 841 lineto stroke
% END MANUAL EDIT
5 0 obj
<</Length 257>>stream
q 0.1 0 0 0.1 0 0 cm
0 G
0 g
q
10 0 0 10 0 0 cm BT
/R6 9.9626 Tf
1 0 0 1 139.746 706.129 Tm
[(M)-0.699638(os)-0.399443(t)-0.900585(l)-0.798886(y)-333.819(e)-0.400668(m)-0.300195(p)-0.599165(t)26.0974(y)83.192(.)-0.800112]TJ
154.421 -615.691 Td
(1)Tj
ET
Q
Q
endstream
endobj
pagesave restore
%%PageTrailer
%%Trailer
end
%%EOF
postscipt/PDF 文件没有生成对角线,而是(看似)保持不变。但是,如果我将页面尺寸从 A4 更改为字母大小,则会显示该行:
%%Page: 1 1
%%PageBoundingBox: 0 0 612 792
%%BeginPageSetup
4 0 obj
<</Type/Page/MediaBox [0 0 612 792]
...
我显然在这里遗漏了一些东西(鉴于我对后记的基本了解,这并不奇怪)。我的问题是:如何在保持页面尺寸不变的情况下显示线条?
P.S.:我偶然发现的一条评论提到 pdftops
(来自 poppler-utils)在某种意义上优于 pdf2ps
。实际上,在 showpage
命令(使用 pdf2ps
时根本不存在)之前将命令插入到转换后的 postscript 文件中效果很好。所以我可能已经找到了解决问题的方法。但是,我想了解使用 pdf2ps
.
解决方案
多亏了 KenS 的建议并参考了他对 this question 的回答,我才能够通过在 postscript 文件中添加一个 EndPage 程序来达到预期的效果:
<<
/EndPage
{
exch pop 2 lt
{
gsave
0 0 translate
0 setgray 0 0 moveto 596 842 lineto stroke
grestore
true
}{false} ifelse
} bind
>> setpagedevice
(假设页面大小为 a4。)
PostScript 是一种只写语言:-)
说真的,它是一种编程语言。为了了解发生了什么,您需要了解程序,对于 Ghostscript 的 ps2write 设备的输出,这显然是非常重要的。
语法基本上是 PDF,带有一个用 PostScript 术语解释它的序言程序。
程序将使用showpage,它在执行过程EndStream 时执行,这(基本上)是在页面流中遇到endobj 关键字时。你会看到它看起来像:
ET
Q
Q
Q
endstream
endobj
%%Page: 2 2
您可以在 endstream 和 endobj 之间放置任何您喜欢的东西,但您需要知道此时的图形状态是由已经发生的任何操作决定的。这可以包括缩放、演说、倾斜、翻转垂直轴等。因此,简单地向其中插入一些 PostScript 不太可能奏效。您可以执行 initgraphics,它至少会将图形状态重置为已知设置。
作为测试,我 运行 Ghostscript 的 ps2write 设备是这样的:
gs -sDEVICE=pdfwrite -o out.ps -c "showpage" -f
生成 PostScript 程序,其中(有效)内容为:
%%EndResource
%%EndProlog
%%Page: 1 1
%%PageBoundingBox: 0 0 595 842
%%BeginPageSetup
4 0 obj
<</Type/Page/MediaBox [0 0 595 842]
/Parent 3 0 R
/Resources<</ProcSet[/PDF]
>>
/Contents 5 0 R
>>
endobj
%%EndPageSetup
5 0 obj
<</Length 23>>stream
q 0.1 0 0 0.1 0 0 cm
Q
endstream
endobj
%%Trailer
end
%%EOF
然后我按照你的建议或多或少地修改了这个:
%%EndPageSetup
0 setgray 0 0 moveto 595 842 lineto stroke
5 0 obj
<</Length 23>>stream
q 0.1 0 0 0.1 0 0 cm
Q
endstream
endobj
%%Trailer
对我来说,这产生了从左下角到右上角的预期笔划。显然,如果没有您最初生成的 PostScript 文件,我无法告诉您为什么您的体验会有所不同。 (不,我无法 运行 latex 来制作这样的东西,即使我这样做了,我也无法知道你使用的是哪个版本的 Ghostscript 和其他工具)。
我的猜测是您的 PDF 文件中的 'something' 覆盖了整个页面,这并不少见。