Not able to redact the StampAnnotation image

Hi,

Our customer has a case like, they are creating a pdf image with stamp type annotation which has an image. They are trying to redact one particular information on that image.
When we apply redact using PdfAnnotationEditor.redactArea method, they annotation is adding behind the image. But we need to redact only the are which user selects.

I need some solution to resolve this issue.

Please find the sample file pdf_image.pdf (63.4 KB)

@nathiya1

Would kindly also share the code snippet that you are using to add the redaction annotation? Is this image also added using Aspose.PDF in the document? Please provide the requested details so that we can further proceed to assist you.

@asad.ali,
Please find the code snippet here.


Rectangle rectangle = new Rectangle(
Math.min(coordinate.startPoint.x, coordinate.endPoint.x),
pageHeight - Math.max(coordinate.startPoint.y, coordinate.endPoint.y),
Math.abs(coordinate.endPoint.x - coordinate.startPoint.x) + (coordinate.startPoint.x + coordinate.endPoint.x),
Math.abs(coordinate.endPoint.y - coordinate.startPoint.y)+ (coordinate.startPoint.y + coordinate.endPoint.y));

Color color = Objects.nonNull(severColor) ? Color.decode(severColor) : Color.decode(annotationDto.color);

PdfAnnotationEditor editor = new PdfAnnotationEditor();
Path tempPath = null;
try (InputStream inputStreamPage = pageService.getPageStream(pageEntity.getDocument().getId(),
pageEntity,false)) {
com.aspose.pdf.Document document = new com.aspose.pdf.Document(inputStreamPage);
editor.bindPdf(document);
editor.redactArea(1, rectangle, color);
tempPath = Files.createTempFile(pageEntity.getDocument().getId().toString(), “.pdf”);
editor.save(tempPath.toFile().toString());


We have created the document and added the image using adobe application. We are using aspose to redact the particular part.

@nathiya1

We are unable to execute this code snippet as it contains some undefined and missing objects. Can you please share code snippet with exact values that you are using to add annotation. We will test the scenario in our environment and address it accordingly.

RedactPDFPage.zip (757 Bytes)
Please find the java class to execute the code.

Please find the input file here,
pdf_image.pdf (63.4 KB)

@nathiya1

Please try to flatten the PDF document before applying redaction annotation. Please check the below code snippet and the output PDF that we generated in our environment.

import com.aspose.pdf.facades.PdfAnnotationEditor;

public class RedactPDFPage {

	public static void main(String[] args) {
		PdfAnnotationEditor editor = new PdfAnnotationEditor();
		String filePathString = "D:\\Files\\pdf_image.pdf";
		Path filePath = Paths.get(filePathString);
		try (InputStream inputStreamPage = Files.newInputStream(filePath)) {
			com.aspose.pdf.Document document = new com.aspose.pdf.Document(inputStreamPage);
			document.flatten();
			com.aspose.pdf.Rectangle asposeRectangle = createAsposeRectangle();
			editor.bindPdf(document);
			Color color = Color.decode("#e67d7d");
			editor.redactArea(1, asposeRectangle, color);
			Path tempPath = Files.createTempFile("redacted", ".pdf");
			editor.save("D:\\Files\\pdf_image_redacted.pdf");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	private static com.aspose.pdf.Rectangle createAsposeRectangle() {
		return new com.aspose.pdf.Rectangle(
				108.0,
				563.0,
				212.0,
				653.0);
	}

}

pdf_image_redacted.pdf (151.3 KB)

@asad.ali,
If we add other type of markups like comment, it will also flatten right. But other type of annotation should be moveable and it should be editable.
Is there any other solution to flatten the stamp type annotation image?
Could you please advise me on this and how we can achieve this?

@nathiya1

Can you please share which Annotation Type are you using to add the image? Can you please share the code snippet?

I mean I am adding other markups using external application like adobe. After added the markups, client is importing into our application to redact the pdf content and other purposes.
In this case, if we flatten the pdf document other markups also will be flatten right? It should be editable after redact operation also.

Please find the sample input here
pdf_image_with markups.pdf (132.8 KB)

@nathiya1

Yes, if you call document.flatten() method, all markups and fields will get flattened. In case you want to keep them editable, you can flatten only single annotation that contains image.

package Aspose;

import java.awt.Color;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;


import com.aspose.pdf.AnnotationType;
import com.aspose.pdf.License;
import com.aspose.pdf.StampAnnotation;
import com.aspose.pdf.facades.PdfAnnotationEditor;

public class RedactPDFPage {

	public static void main(String[] args) throws Exception {
		PdfAnnotationEditor editor = new PdfAnnotationEditor();
		String filePathString = "D:\\Files\\pdf_image_with markups.pdf";
		Path filePath = Paths.get(filePathString);
		try (InputStream inputStreamPage = Files.newInputStream(filePath)) {
			com.aspose.pdf.Document document = new com.aspose.pdf.Document(inputStreamPage);
			for(com.aspose.pdf.Annotation annotation : document.getPages().get_Item(1).getAnnotations())
			{
				if(annotation.getAnnotationType() == AnnotationType.Stamp)
				{
					if(((StampAnnotation)annotation).getSubject() == "Stamp");
					{
						annotation.flatten();
					}
				}
			}
			//document.flatten();
			com.aspose.pdf.Rectangle asposeRectangle = createAsposeRectangle();
			editor.bindPdf(document);
			Color color = Color.decode("#e67d7d");
			editor.redactArea(1, asposeRectangle, color);
			Path tempPath = Files.createTempFile("redacted", ".pdf");
			editor.save("D:\\Files\\pdf_image_redacted.pdf");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static com.aspose.pdf.Rectangle createAsposeRectangle() {
		return new com.aspose.pdf.Rectangle(
				108.0,
				563.0,
				212.0,
				653.0);
	}

}

pdf_image_redacted.pdf (136.6 KB)

@asad.ali Thanks for the solution. This is really great.