有没有办法将一个旋转的PDF文档覆盖到另一个文档上?
我有SVG文档,在我的工作流程结束时通过ghostscript将其转换为PDF。现在我必须添加一个新功能。 我需要用PDF内容替换SVG中的某些元素。不是 仅覆盖,它可以按比例旋转和缩放,以下是一幅图像,例如: 范例 我的问题是有没有办法用ghostscript来做 目前,我只是通过SVG图像元素将其作为光栅化图像插入,但我需要将其作为矢量进行打印 注意:我只需要从插入的PDF中选取第一页有没有办法将一个旋转的PDF文档覆盖到另一个文档上?,pdf,pdf-generation,ghostscript,Pdf,Pdf Generation,Ghostscript,我有SVG文档,在我的工作流程结束时通过ghostscript将其转换为PDF。现在我必须添加一个新功能。 我需要用PDF内容替换SVG中的某些元素。不是 仅覆盖,它可以按比例旋转和缩放,以下是一幅图像,例如: 范例 我的问题是有没有办法用ghostscript来做 目前,我只是通过SVG图像元素将其作为光栅化图像插入,但我需要将其作为矢量进行打印 注意:我只需要从插入的PDF中选取第一页 我知道有一个pdftk可以将一个PDF文件覆盖在另一个PDF文件上,但我不能用它缩放和旋转覆盖的PDF文件
我知道有一个pdftk可以将一个PDF文件覆盖在另一个PDF文件上,但我不能用它缩放和旋转覆盖的PDF文件。好的,所以我担心,由于Ghostscript PDF解释器最近为解决安全漏洞而进行了更改,此代码只能在9.26版本之前使用。在未来,PDF itnerpreter将被修改,并且会有更好的方法来实现这一点,但我担心这是一个长期目标。现在,要使用这段代码,您需要坚持使用旧版本 执行此操作的PostScript程序如下所示:
%!PS
%%
%% This code is copied from pdf_main.ps, pdfshowpage_finish
%% sadly that routine always calls showpage, and we want that
%% to be under our control, so we have to duplicate the code
%% here. Not only that but it uses GS extensions which aren't
%% available outside of startup, so some things it simply can't
%% replicate. As a result some of the error handling is less
%% good.
%%
%% I plan to extend the PDF interpreter with two new
%% routines, pdfnoshowpage_finish and then have both
%% that and pdfshowpage_finish call pdfoptionalshowpage_finish
%% which will take a boolean determining whether to actually
%% call the showpage. At that time we'll alter this code.
%%
/draw_page_content { % <pagedict> pdfshowpage_finish -
save /PDFSave exch store
/PDFdictstackcount countdictstack store
/PDFexecstackcount count 2 sub store
(before exec) VMDEBUG
% set up color space substitution (this must be inside the page save)
pdfshowpage_setcspacesub
% Display the actual page contents.
8 dict begin
/BXlevel 0 def
/BMClevel 0 def
/OFFlevels 0 dict def
/BGDefault currentblackgeneration def
/UCRDefault currentundercolorremoval def
%****** DOESN'T HANDLE COLOR TRANSFER YET ******
/TRDefault currenttransfer def
matrix currentmatrix
2 dict
dictbeginpage setmatrix
/DefaultQstate qstate store
count 1 sub /pdfemptycount exch store
% If the page uses any transparency features, show it within
% a transparency group.
dup pageusestransparency dup /PDFusingtransparency exch def {
% Show the page within a PDF 1.4 device filter.
0 .pushpdf14devicefilter {
/DefaultQstate qstate store % device has changed -- reset DefaultQstate
% If the page has a Group, enclose contents in transparency group.
% (Adobe Tech Note 5407, sec 9.2)
dup /Group knownoget {
1 index /CropBox pget {
/CropBox exch
} {
1 index get_media_box pop /MediaBox exch
} ifelse
oforce_elems normrect_elems fix_empty_rect_elems 4 array astore .beginformgroup
showpagecontents
.endtransparencygroup
} {
showpagecontents
} ifelse
} stopped {
% abort the transparency device
.abortpdf14devicefilter
/DefaultQstate qstate store % device has changed -- reset DefaultQstate
stop
} if .poppdf14devicefilter
/DefaultQstate qstate store % device has changed -- reset DefaultQstate
} {
showpagecontents
} ifelse
.free_page_resources
% todo: mixing drawing ops outside the device filter could cause
% problems, for example with the pnga device.
end % scratch dict
% Some PDF files don't have matching q/Q (gsave/grestore) so we need
% to clean up any left over dicts from the dictstack
PDFdictstackcount //false
{ countdictstack 2 index le { exit } if
currentdict /n known not or
end
} loop
pop
count PDFexecstackcount sub { pop } repeat
Repaired % pass Repaired state around the restore
PDFSave restore
currentglobal pdfdict gcheck .setglobal
.setglobal
/Repaired exch def
} def
% And now, draw the page from teh first PDF file
(d:/temp/SO/target.pdf) (r) file runpdfbegin
save
1 pdfgetpage
dup /Page exch store draw_page_content
restore
runpdfend
% and then the page from the secodn PDF file
(d:/temp/SO/source.pdf) (r) file runpdfbegin
save
1 pdfgetpage
dup /Page exch store
% Before drawing the second page, adjust the CTM so
% that the bottom left corner of the page is co-incident
% with the bottom left of the area we want to draw the
% page in
75 575 translate
% adjust the size of the output
0.11 0.11 scale
% and rotate it
-46 rotate
draw_page_content
restore
runpdfend
showpage
将其保存在一个文件中,调用test.ps,并适当调整PostScript程序中的路径。然后使用:gs-sDEVICE=pdfwrite-sOutputFile=out.pdf test.ps运行Ghostscript,您将得到一个类似于我认为您需要的pdf文件。显然,您还需要更改translate/scale/rotate的编号。我在这里发布了一个支持剪切粘贴的PDF文件的解决方案,以防将来有人需要它。我添加了额外的参数来绘制页面内容以支持这一点。我发现dict beginpage setmatrix命令以某种方式覆盖了剪切路径,它应该仅在该命令之后设置。我不是PostScript方面的专家,但至少这个解决方案是可行的。再次感谢肯斯的帮助
%!PS
%%
%% This code is copied from pdf_main.ps, pdfshowpage_finish
%% sadly that routine always calls showpage, and we want that
%% to be under our control, so we have to duplicate the code
%% here. Not only that but it uses GS extensions which aren't
%% available outside of startup, so some things it simply can't
%% replicate. As a result some of the error handling is less
%% good.
%%
%% I plan to extend the PDF interpreter with two new
%% routines, pdfnoshowpage_finish and then have both
%% that and pdfshowpage_finish call pdfoptionalshowpage_finish
%% which will take a boolean determining whether to actually
%% call the showpage. At that time we'll alter this code.
%%
/draw_page_content { % <pagedict> pdfshowpage_finish -
/clipx exch def /clipy exch def /clip_width exch def /clip_height exch def /should_clip exch def
save /PDFSave exch store
/PDFdictstackcount countdictstack store
/PDFexecstackcount count 2 sub store
(before exec) VMDEBUG
% set up color space substitution (this must be inside the page save)
pdfshowpage_setcspacesub
% Display the actual page contents.
8 dict begin
/BXlevel 0 def
/BMClevel 0 def
/OFFlevels 0 dict def
/BGDefault currentblackgeneration def
/UCRDefault currentundercolorremoval def
%****** DOESN'T HANDLE COLOR TRANSFER YET ******
/TRDefault currenttransfer def
matrix currentmatrix
2 dict
dictbeginpage setmatrix
% set clipping here
should_clip 0 gt
{
newpath clipx clipy moveto clipx clipy clip_height add lineto clipx clip_width add clipy clip_height add lineto clipx clip_width add clipy lineto closepath clip
}
{
}
ifelse
/DefaultQstate qstate store
count 1 sub /pdfemptycount exch store
% If the page uses any transparency features, show it within
% a transparency group.
dup pageusestransparency dup /PDFusingtransparency exch def {
% Show the page within a PDF 1.4 device filter.
0 .pushpdf14devicefilter {
/DefaultQstate qstate store % device has changed -- reset DefaultQstate
% If the page has a Group, enclose contents in transparency group.
% (Adobe Tech Note 5407, sec 9.2)
dup /Group knownoget {
1 index /CropBox pget {
/CropBox exch
} {
1 index get_media_box pop /MediaBox exch
} ifelse
oforce_elems normrect_elems fix_empty_rect_elems 4 array astore .beginformgroup
showpagecontents
.endtransparencygroup
} {
showpagecontents
} ifelse
} stopped {
% abort the transparency device
.abortpdf14devicefilter
/DefaultQstate qstate store % device has changed -- reset DefaultQstate
stop
} if .poppdf14devicefilter
/DefaultQstate qstate store % device has changed -- reset DefaultQstate
} {
showpagecontents
} ifelse
.free_page_resources
% todo: mixing drawing ops outside the device filter could cause
% problems, for example with the pnga device.
end % scratch dict
% Some PDF files don't have matching q/Q (gsave/grestore) so we need
% to clean up any left over dicts from the dictstack
PDFdictstackcount //false
{ countdictstack 2 index le { exit } if
currentdict /n known not or
end
} loop
pop
count PDFexecstackcount sub { pop } repeat
Repaired % pass Repaired state around the restore
PDFSave restore
currentglobal pdfdict gcheck .setglobal
.setglobal
/Repaired exch def
} bind def
% And now, draw the page from teh first PDF file
(/opt/files/main.pdf) (r) file runpdfbegin
save
1 pdfgetpage
dup /Page exch store 0 1125 1125 0 0 draw_page_content
restore
runpdfend
gsave
% and then the page from the next PDF file
(/opt/files/placement1.pdf) (r) file runpdfbegin
save
1 pdfgetpage
dup /Page exch store
% Before drawing the second page, adjust the CTM so
% that the bottom left corner of the page is co-incident
% with the bottom left of the area we want to draw the
% page in
120 120 translate
% and rotate it
25 rotate
% adjust the size of the output
0.7 0.7 scale
%let's skew file
[1 0 1.3 1 0 0] concat
% draw file and clip it
1 100 100 0 0 draw_page_content
restore
runpdfend
grestore
showpage
不能使用Ghostscript直接从SVG创建PDF,因为Ghostscript不读取SVG文件。因此,这取决于您实际发送给Ghostscript以创建hte PDF文件的内容。“有可能”您可以在现有内容的基础上绘制PDF内容。但是,您不能使用pdfwrite设备“替换”任何内容。如果页面上有两个项目位于同一位置,则这两个项目都将出现在最终的PDF文件中。你真的需要拿出一个例子和一个Ghostscript命令行来说明你目前正在做什么。Kens,谢谢你的回复。事实上,我意识到我没有正确地解释它。实际上,我首先使用Inkscape将SVG转换为PDF,然后通过Ghostscript进行RGB->CMYK转换等后处理。所以我的问题是如何将一个PDF放置在另一个PDF上,但是上层PDF应该旋转一些角度,并且应该在目标页面上有它的坐标。你不能简单地从命令行来做,你需要编写一个PostScript程序。您需要打开第一个PDF文件,绘制页面内容,然后关闭该PDF文件,更改CTM,打开第二个PDF文件,绘制页面内容,关闭并完成。在Ghostscript的PDF itnerpreter的当前版本中,在启动第二个PDF之前,您还必须关闭第一个PDF的输入,我认为您可能会丢失一些元数据,例如链接、大纲等。我不知道这两者是否对您有问题。我可以尝试解决您的问题,但我需要一些例子文件和角度等工作。谢谢,肯斯!我准备了两份文件。我认为最简单的例子对我来说就足够了,我只是不明白如何迭代对象,转换它们,然后将转换后的对象放到其他画布上……谢谢!我明天试试,然后告诉你。再次感谢你!当然不是。可能是您正在修改其他框参数之一,但我需要查看文件。啊,看起来是的,很抱歉我错过了,因为我不在。让我看看你之前提到的演示我不能给你任何有用的文件。我也不知道有哪种软件会增加透明度,但Cairo图形库生成的PDF文件几乎总是包含透明度,即使实际上并不需要。我认为您可以使用Ghostscript对透明pdfmark的支持来向文件添加透明度,但您必须从pdfmark引用中找到它。处理此问题的简单方法是在转换之前进行gsave保存图形状态,然后在完成转换后恢复图形状态。CTM是图形状态的一部分,因此将遵循gsave/grestore.com,因为自动旋转。这是一种尝试旋转页面的启发式方法,使大部分文本“垂直”并从左向右运行。它是有文档记录的,您可以将其设置为/None来停止它。显然,它只适用于实际文本,而不适用于“看起来像lke”文本的图像或线条。