How to remove highlights from a page?

On my Android app, I store highlight quads in a database, and draw them as pages are opened. However, if I go on a page back and forth, same highlights are drawn over the existing ones, making them darker and darker. Also the pages with highlights open slower then the ones with no highlights.

How can I make sure that one highlight is drawn only once. I was thinking of deleting any existing highlights on a page as it is opened and then draw them from the database if there are any. But I couldn’t find a way to delete the highlights. Is there a way to do it in my PFDView?

mPdfDoc = new PDFDoc(fis);

mPDFView.setDoc(mPdfDoc);

And I open page as

mPDFView.setCurrentPage(pageNumber);

Thanks

To delete annotations already on a document, see the following sample.
https://www.pdftron.com/pdfnet/samplecode/AnnotationTest.java.html

Alternatively, using the sample code above, when you load each annotation from your database, you could check for an existing highlight annotation at that location, and replace the two with a single annotation that is a union of the two, or remove the intersections from your database highlight.

Thanks for your reply. the rects have very precise double values. If I load one from the database and also check what exists at the current location, will these double values match perfectly every time?

Also, in a different scenario, I want to be able to delete these highlights by clicking on them, which pops up a delete icon. But when clicking this delete icon, how do I get some sort of highlight id by which connect them with their id in the database? Or this will also need comparison by the rects, in which case again, will the values match perfectly for all the four corners?

Zeeshan A Zakaria

On Wednesday, March 4, 2015 at 7:09:42 PM UTC-5, Ryan wrote:

To delete annotations already on a document, see the following sample.
https://www.pdftron.com/pdfnet/samplecode/AnnotationTest.java.html

Alternatively, using the sample code above, when you load each annotation from your database, you could check for an existing highlight annotation at that location, and replace the two with a single annotation that is a union of the two, or remove the intersections from your database highlight.

Not exactly, but if you reduce the precision to say 2-4 decimal places, then they should match. But really, I would avoid any logic that relies on matching vertices/coordinates.

You can assign a unique ID to any annotation, by calling Annot.SetUniqueID.

This value must be unique for the entire document. This id is part of the spec, and goes into the NM entry.

I agree that matching coordinates is not the best approach. But I was not aware of SetUniqueID either, which is exactly what I wanted to do.

However, I am setting highlights using code in TextSelect.java class, under protected void onQuickMenuClicked(int menu_id, String menu_title). Where in this method do I use Annot.SetUniqueID so it is assigned to the set of selected/drawn rects? And I am also not sure how the document remember that this unique ID belonged to which highlight? I am not saving the PDF in anyway. Every time I open the PDF and change pages, it relies on my database to draw highlights.

Zeeshan

On Monday, March 9, 2015 at 2:42:33 PM UTC-4, Ryan wrote:

Not exactly, but if you reduce the precision to say 2-4 decimal places, then they should match. But really, I would avoid any logic that relies on matching vertices/coordinates.

You can assign a unique ID to any annotation, by calling Annot.SetUniqueID.

This value must be unique for the entire document. This id is part of the spec, and goes into the NM entry.

I couldn’t find SetUniqueId in the PDFTron library. I am using Java. Is this for C++?

Zeeshan

On Monday, March 9, 2015 at 2:42:33 PM UTC-4, Ryan wrote:

Not exactly, but if you reduce the precision to say 2-4 decimal places, then they should match. But really, I would avoid any logic that relies on matching vertices/coordinates.

You can assign a unique ID to any annotation, by calling Annot.SetUniqueID.

This value must be unique for the entire document. This id is part of the spec, and goes into the NM entry.

Correction: Android Studio’s search couldn’t find it probably because it points to compile code, but it does exist in Annot class.

Question: Is there any example of assigning and retrieving unique ids to annotations of type highlight? Seeing what happens in the TextSelect class, do I assign it like:

TextMarkup tm = Highlight.create(doc, bbox);
tm.setUniqueID(highlightCategoryId);

Is this the right approach?

Zeeshan

On Tuesday, March 10, 2015 at 1:41:30 PM UTC-4, Zeeshan Zakaria wrote:

I couldn’t find SetUniqueId in the PDFTron library. I am using Java. Is this for C++?


Zeeshan

On Monday, March 9, 2015 at 2:42:33 PM UTC-4, Ryan wrote:

Not exactly, but if you reduce the precision to say 2-4 decimal places, then they should match. But really, I would avoid any logic that relies on matching vertices/coordinates.

You can assign a unique ID to any annotation, by calling Annot.SetUniqueID.

This value must be unique for the entire document. This id is part of the spec, and goes into the NM entry.

I managed to do it successfully and would post my code here if it could help others:

  1. I created a public static Annot mAnnot1 in Pan.java

  2. In my class where I am duplicating code from TextSelect class’s onQuickMenuClicked, I did the following to insert my own annotation ids into the ids which are being drawn on the screen:

    tm.refreshAppearance();
    Page page = mPDFView.getDoc().getPage(pg);
    page.annotPushBack™;
    Pan.mAnnot1 = tm;
    Pan.mAnnot1.setUniqueID(annotationId); // annotationId is my own generated UUID, which I use to uniquely identify annotations in my database

  3. Now to remove the highlights from my database, once I selected them by doing single tap on them followed by clicking my own delete button, now I could retrieve the right unique ID for any of the selected highlight on the page:

// Remove highlight from the database

try {

String annotationId = Pan.mAnnot1.getUniqueID().getAsPDFText();

Log.d(TAG, "removing annotationId: " + annotationId);

removeAnnotationByID(annotationId); // My method which calls the database to delete the annotation highlight by id

} catch (Exception e) {

e.printStackTrace();

}

And then to remove this highlight from the screen, I use the PDFTron’s sample code, which I personally don’t agree because it captures annotations by x and y coordinates and not by some id as one would expect:

getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
try {
Page page = mPdfDoc.getPage(mCurrentPage);
page.annotRemove(Pan.mAnnot1);
mPDFView.update(Pan.mAnnot1, mCurrentPage);
mPDFView.clearSelection();
} catch (Exception e) {
e.printStackTrace();
}
}
});

Zeeshan A Zakaria

On Tuesday, March 10, 2015 at 2:53:55 PM UTC-4, Zeeshan Zakaria wrote:

Correction: Android Studio’s search couldn’t find it probably because it points to compile code, but it does exist in Annot class.

Question: Is there any example of assigning and retrieving unique ids to annotations of type highlight? Seeing what happens in the TextSelect class, do I assign it like:

TextMarkup tm = Highlight.create(doc, bbox);
tm.setUniqueID(highlightCategoryId);

Is this the right approach?


Zeeshan

On Tuesday, March 10, 2015 at 1:41:30 PM UTC-4, Zeeshan Zakaria wrote:

I couldn’t find SetUniqueId in the PDFTron library. I am using Java. Is this for C++?


Zeeshan

On Monday, March 9, 2015 at 2:42:33 PM UTC-4, Ryan wrote:

Not exactly, but if you reduce the precision to say 2-4 decimal places, then they should match. But really, I would avoid any logic that relies on matching vertices/coordinates.

You can assign a unique ID to any annotation, by calling Annot.SetUniqueID.

This value must be unique for the entire document. This id is part of the spec, and goes into the NM entry.