Highlight Annotation is deletable

Hi,
I use the HighlightAnnotation Class for highlighting some text on a specific position defined by a Rectangle.
I works so far but I found out, that I can delete the highlightAnnotation on the produced pdf.
Is it possible to have some read only highlight or just to burn the highlight into the pdf, so that a user using the pdf is not able to delete the highlightAnnotation?

best regards,

Igor

@igor.berchtold

You can use Annotation.Flatten() method after adding annotation to the PDF file so that they would not be able to select or delete. In case you still face any issue, please share your sample PDF with code snippet. We will test the scenario in our environment and address it accordingly.

Fantastic, this works, thank you for the hint.

best regards,
Igor

1 Like

Hi @asad.ali
I still struggle with HighlightAnnotation. I do have this code for calling createHighlightAnnotation with some values

pdfPage.getParagraphs().add(createHighlightAnnotation(annotationen, pdfPage, hight));

private HighlightAnnotation createHighlightAnnotation(Map<String, String> annotationen, Page pdfPage, float hight) {

HighlightAnnotation highlightAnnotation = new HighlightAnnotation(pdfPage, new Rectangle(llx,lly,urx,ury));
java.awt.Color brushColor =
java.awt.Color.decode(annotationUtil.convertP8colorToRGBHex(annotationen.get(F_BRUSHCOLOR)));
highlightAnnotation.setColor(Color.fromRgb(brushColor));
highlightAnnotation.flatten();
return highlightAnnotation;
}

When I open my saved file with AcrobatReader, I cannot see the Highlight. I can see in the comment that there is a Annotation. There is on the left side were the actual annotation should start a thin yellow bare.

What do I wrong?annotated_6ecccf67-374a-4f98-a19a-1de91c493f39.pdf (168.2 KB)

@igor.berchtold

It seems like you shared an output PDF document. Would you please share the respective source PDF file with us along with sample code snippet which we can run without any issue to test the scenario. We will look into the scenario and address it accordingly.

I send you the full code to reproduce and some sample PDF Files.
AnnotationHiglightExample.java.zip (451.3 KB)

Within the ZIP file there is AnnotationHiglightExample.java, I used the following dependency for the aspose library:

    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-pdf</artifactId>
        <version>21.1</version>
        <scope>compile</scope>
    </dependency>

Please adjust the input and output path to your location of the files.
Original_Grid.pdf is the source and Original_Grid_output.pdf is the produced file. Opening this file you can see the highlight, you can not select it but you can see it in the comment, you can delete it but the highlight still is there.

Now the case with testAnnotationen.pdf you can see teh produced file as testAnnotationen_output.pdf. Here you can not see the Highlight annotation. You can see the highlight Annotation in the comment and you can select it. So one can observe, that the highlight is behind the image. Also here I can delete it but I can not see anymore the highligth.

I hope you can reproduce the case and give me some hint how to solve it.

best regards,

Igor

@igor.berchtold

We were able to notice the issue in our environment. We also tried to set the z-index for highlight annotation use setZIndex() method but it did not give much success. Therefore, we have logged an issue as PDFJAVA-40174 in our issue tracking system. We will further look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.

@igor.berchtold

We have investigated the earlier logged ticket and found that this is not a bug but expected behavior. Highlight object is placed behind the text and objects. But if it is required to use similar object that is placed on front layer, you can use TextStamp instead:

// comment this line in code
// pdfPage.getParagraphs().add(createHighlightAnnotation(pdfPage, hight));
// use the following method
createTextStamp(pdfPage, hight);

Method createTextStamp()

private static void createTextStamp(Page pdfPage, float hight) {
        String F_LEFT = "2.293888888888889";
        String F_TOP = "1.136111111111111";
        String F_HEIGHT = "0.5468055555555555";
        String F_WIDTH = "0.5147222222222222";

        TextStamp textStamp = new TextStamp(" ");
        textStamp.setBackground(Boolean.FALSE);


        java.awt.Color brushColor =  java.awt.Color.decode(convertP8colorToRGBHex("65535"));
        textStamp.getTextState().setBackgroundColor(Color.fromRgb(brushColor));

        textStamp.setOpacity(0.5);//this line can be commented to check that the textStamp is really on top.
        textStamp.setHorizontalAlignment(HorizontalAlignment.None);
        textStamp.setXIndent(Double.parseDouble(F_LEFT) * 72);
        textStamp.setYIndent(hight - Double.parseDouble(F_TOP) * 72 - Double.parseDouble(F_HEIGHT) * 72);
        textStamp.setHeight(Double.parseDouble(F_HEIGHT) * 72);
        textStamp.setWidth(Double.parseDouble(F_WIDTH) * 72);
        pdfPage.addStamp(textStamp);
    }