问题
I want to add a transparent png image to an existing pdf to blur some part of the text. I don't understand how to apply the transparency
I have tried several code examples found in the documentation, but none worked
// Read the pdf input
PdfReader pdfReader = new PdfReader(value);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PdfWriter pdfWriter = new PdfWriter(outputStream);
PdfDocument pdfDoc = new PdfDocument(pdfReader, pdfWriter);
Document document = new Document(pdfDoc);
// Creating an ImageData object
ImageData data = ImageDataFactory.create(fileName);
data.setTransparency(new int[] {0xF0, 0xFF });
for (int x = 1; x < 800; ) {
for (int y = 1; y < 1000; ) {
Image image = new Image(data);
image.setFixedPosition(x , y);
document.add(image);
y = y + y1 + 40;
}
x = x + x1 + 40;
}
// The content has now been modified, return it as a stream
document.close();
When I use the setTransparency method, then nothing is shown on the screen as if it was fully transparent. If I comment out the setTransparency method, then the blur image is added, but not transparency at all making it ineffective.
I am attaching a screen shot of the two output pdfs. The first one is when setTransparency method is called. The second one is when the setTransparency method is commented out.
When setTransparency is called:
When the method setTransparency is commented out
I am expecting to be able to setTransparency and see these blurred images on top on the pdf. I am using this image to blur the text:
回答1:
Your "transparent image" (54-544263_face-blur-png-face-blur-overlay-png-transparent.png) is not transparent at all! It might be a screenshot of how a transparent image looks in an image editor that supports transparent images and underlays it with a checkerboard pattern to visualize transparency.
After investigating the source of that example image a bit:
You appear to have downloaded a preview image from pngix.com. These preview images do not contain the actual transparency but instead illustrate the effect of that transparency. To get an image with actual transparency from pngix.com with its current UI, you have to look very hard and find the "free download" button on the page (for me it was sandwiched between "sponsored searches"), followed by the "Download PNG" button in the popup; this allows you to download an the image with actual transparency.
That being said, you can easily create an image of such circular fade into transparency yourself using free tools, e.g. gimp. Images from pngix.com are for personal use only, so theoretically you may have to deal with legal issues if you use them professionally without extra license.
And even better, you actually don't need bitmap images for that at all, you can also use native PDF mechanisms which use vector graphics instructions, i.e. no need to introduce large images for results looking good in hi res! See e.g. the example screenshot at the bottom of this answer, that image was created using the test method testComplex from the TestTransparency test class.
来源:https://stackoverflow.com/questions/57723093/itext7-image-transparency