PDF叠加不起作用 [英] Pdf overlaying not working

查看:121
本文介绍了PDF叠加不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找解决此问题的方法:我有两个带有图像的横向A3 pdf,并且我想以一种方式将它们覆盖,使得生成的pdf包含两个图像合并为一个图像,就好像其中一个图像是一个图像.水印,但密度相同.想起来就好像在一张A3纸上打印两个不同的pdf文件一样,我想获得完全一样的效果.

I have been looking for a solution for this problem : I have two landscape-oriented A3 pdfs with images and I want to overlay them in a manner that the resulting pdf contains both images merged into one as if one of them was a watermark, but with the same density. Think of it as if about printing two different pdfs on one A3 sheet of paper, I want to get exactly that effect.

换句话说-只是想出一种表达方式-我想覆盖两个pdf,对于上层,使所有白色"区域透明.

In other words - just came up with a way to express it - I would like to overlay two pdfs and for the upper layer, make all the "white" area transparent.

基本上,我只是按照此问题的任何解决方案中的步骤进行操作:

Basically, I just followed steps in any solution from this question:

将一个pdf或ps文件覆盖在另一个

在我的情况下,pdftk无法正常工作.生成的PDF显示了位于顶层的pdf,但是没有看到底层.因此,我开始编程解决方案并下载了pyPdf.

The pdftk didn't work in my case. The resulting PDF displayed the pdf that was on the top layer, but the bottom layer was not seen. So, I proceeded to programming solution and downloaded pyPdf.

该站点的代码正是所需解决方案的实现:

The code from the site was exactly an implementation of the desired solution:

 from pyPdf import PdfFileReader,PdfFileWriter
 output = PdfFileWriter()
 input1 = PdfFileReader(file("b.pdf", "rb"))

 page1 = input1.getPage(0)
 watermark = PdfFileReader(file("a.pdf", "rb"))
 page1.mergePage(watermark.getPage(0))

 output.addPage(page1)

 outputStream = file("c.pdf", "wb")
 output.write(outputStream)
 outputStream.close()

但是,结果与使用pdftk之后的结果相同.

However, the result was the same as after using pdftk.

我做错了什么?也许这不是pdf合并,多重合并,冲压,覆盖等,而是其他?如果是这样,怎么称呼它?

What am I doing wrong? Maybe this is not pdf merging, multimerging, stamping, overlaying etc but something else? If so, how is it called?

推荐答案

White 可能是两种基本情况的结果:要么是没有在其中绘制任何东西,要么是某些东西引起的使用有效的白色绘制在那里.使用这些页面合并方法,可以将第一种类型的PDF赋予背景,而后者则不能.

White in a pdf can be a result of two fundamental situations: Either it is the result of nothing being drawn there or of something being drawn there using an effective color white. PDFs of the first type can be given a background using those page merge methods, PDFs of the latter can't.

示例文件a.pdf的页面内容流开始如下:

The content stream of the page of your sample file a.pdf starts like this:

1 0 0 -1 0 841 cm
0.45 0 0 0.45 0 0 cm
1 0 0 1 0 0 cm
0 0 m 2646 0 l 2646 1870 l 0 1870 l h
q
1 1 1 rg f
Q

前三行更改了操作的坐标系,使其原点位于左上角,坐标值向右和向下递增,并且一个单位为1/160英寸.

The first three lines change the coordinate system for the operations to come to have its origin in the upper left corner, coordinate values increasing right and down, and one unit being 1/160 inch.

第四行绘制一个覆盖整个页面的矩形(实际上甚至更多),第六行用白色填充该矩形. (第五和第七行仅保存和恢复图形状态.)

The fourth line draws a rectangle covering the whole page (actually even slightly more) and the sixth line fills that rectangle with white. (The fifth and seventh line merely save and restore the graphics state.)

因此,通过将此PDF覆盖在另一个页面上,该PDF首先用一个白色矩形覆盖该页面的所有现有内容.

By overlaying this PDF over a page of another one, therefore, this PDF first of all covers all the existing content of that page with a white rectangle.

因此,不能通过简单地将页面内容添加到背景PDF页面的内容中来为您的PDF提供背景.您必须

Thus, your PDF cannot be given a background by simply adding the page content to the content of a background PDF page. You have to

  • 要么先从该内容中删除第4行和第6行(也许 lucidchart 中有一个复选框允许您将这个白色背景矩形打开或关闭)
  • 或使用其他水印过程(例如以另一种方式这样做,使用透明性将您的PDF页面与水印PDF页面重叠).
  • either remove the lines 4 and 6 from that content first (maybe there is some checkbox in lucidchart allowing you to switch this white background rectangle on or of)
  • or use a different watermarking procedure (like doing it the other way around, overlaying your PDF page with the watermark PDF page using transparency).

PS:严格来说,这些内容行已经是错误的:一旦您开始构建路径(在上面的示例中,该路径在0 0 m中发生,即移动到位置0) ,0 ),直到最终使用路径绘制运算符(示例中为f,即 fill ),您才可以使用路径构造(或路径剪切)操作. cf. 此答案以供参考.

PS: Strictly speaking those content lines already are erroneous: As soon as you start constructing a path (which in the sample above happens with 0 0 m, i.e. a move to position 0, 0), you may only use path construction (or path clipping) operations until you finally use a path drawing operator (f, i.e. fill, in your sample). Cf. this answer for a reference.

因此,颜色设置1 1 1 rg(即将填充颜色设置为RGB 100%,100%,100%)和特殊的图形状态操作q(保存图形状态)操作在这里是不允许的.因此,取决于PDF查看器,显示该页面时可能会发生不同的事情,例如填充操作可能会被完全忽略,或者可能只是颜色设置操作而可能会使用当前的填充颜色(黑色?)来代替.不能像Adobe Reader那样依靠所有处理此错误的PDF查看器.

Thus, the color setting 1 1 1 rg (i.e. set fill color to RGB 100%, 100%, 100%) and the special graphic state manipulation q (save graphic state) operations are not allowed here. Depending on the PDF viewer, therefore, different things may happen while displaying that page, e.g. the filling operation might be completely ignored or merely the color setting operation might be and the current fill color (black?) might be used instead. One cannot count on all PDF viewers handling this error like Adobe Reader does.

也许lucidchart已经解决了该问题,并且更新就足够了.否则,您应该要求lucidchart正确开始制作其PDF图表.

Maybe lucidchart have already fixed that issue and an update suffices. Otherwise you should ask lucidchart to start doing their PDF charts right.

这篇关于PDF叠加不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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