问题描述
我正在尝试使用 PDPageContentStream 创建水印.这就是我现在拥有的
I am trying to create a watermark with using PDPageContentStream. This is what I have right now
PDPageContentStream contentStream = new PDPageContentStream(doc,page, true,true);
contentStream.beginText();
contentStream.setFont(font,40);
contentStream.setTextRotation(Math.PI/4,page.getMediaBox().getWidth()/4,page.getMediaBox().getHeight()/4);
contentStream.setNonStrokingColor(210,210,210); //light grey
contentStream.drawString(_text);
contentStream.endText();
contentStream.close();
它会创建一个 45 度角的浅灰色文本.但是 - 当然 - 它覆盖了它下面的实际页面内容,并且不可能看到某些内容.
What happens is it creates a 45 degree angled text with the light grey color. But -of course- it overlays the actual page content beneath it and it is not possible to see some of the content.
是否可以先创建 contentStream 然后附加页面内容?我找到了 this 示例.它使用 PDExtendedGraphicsState 和 PDResources.我是 pdfbox 的新手,几乎没有图形经验.这些是我需要的,什么是 pdfbox 中的资源?
Is it possible to create the contentStream first and then append the page content?I found this example. It uses PDExtendedGraphicsState and PDResources. I am new to pdfbox and almost have no graphics experience.Are these what I need and What is a resource in pdfbox?
提前致谢.
附言我知道我可以使用带有 jpeg 的覆盖实用程序.但我现在正试图用 PDPageContentStream 找出这个问题.
p.s. I am aware that I can use overlay utility with a jpeg. But I am trying to figure out this problem with PDPageContentStream for now.
推荐答案
如果您使用 PDFBox 2.0+,那么现在稍微容易一些:
If you use PDFBox 2.0+, then its slightly easier now:
PDExtendedGraphicsState extendedGraphicsState = new PDExtendedGraphicsState();
extendedGraphicsState.setNonStrokingAlphaConstant((float) alpha);
contents.saveGraphicsState();
contents.setGraphicsStateParameters(extendedGraphicsState);
// do your stuff
contents.restoreGraphicsState();
这篇关于如何使用 PDPageContentStream 作为内容流的底层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!