Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make annotation like highlighting, strikethrough, underline, draw, add text, etc in android for a pdf viewer?

  • Many applications like RepliGo, Aldiko, Mantano, ezPdf in android market make this type of annotation in their pdf viewer shown in the below image.
  • I tried in many ways to implement this annotation but I failed. I have a pdf viewer for android and separate java code for annotations using iText for drawing lines.
  • My question is can i able to implement iText in android. If it's possible, which package do I have to import?
  • Also in some applications, a canvas method is used for drawing lines. Is it possible to include this canvas method in android instead of using an annotation?. The goal would be to have the same features that annotations have.
  • In the below image (RepliGo PDF Reader) which kind of code did they use for annotations? enter image description here
like image 459
BobDroid Avatar asked Feb 06 '12 07:02

BobDroid


People also ask

How do I annotate a PDF on Android?

Freehand draw directly on the PDF, use the highlight, underline and strikethrough tools, make and reply to comments, add sticky notes, and more. Download the edited PDF file or use the option to share a link that will let others see your annotations online.


1 Answers

Your question seems to be what are methods to allow users to annotate over a PDF file in android/java, so here is one method for you, although it may not be the best solution.

I'd like to point out that it is not actually necessary to edit the actual PDF file just to allow users to add and view annotations. Your application could just store the data for annotations separately, store those annotations for each file, and load them when the file is loaded.

That means it won't create a new PDF file with those annotations placed it, but instead it will just store user-data for each PDF file which is loaded into your app, and display that when the user loads the PDF file again. (So it appears to be annotated).

Example:

  1. Read PDF file text, text-formatting & images into your app
  2. Display a document (like a word-processor)
  3. Allow the user to edit & annotate the document
  4. Save changes & annotation-data in your app (not the PDF file)
  5. When loading the file again, apply changes & annotations previously stored.

Your annotation class might look something like this:

class Annotations implements Serializable {

    public Annotations() {
        annotations = new HashSet<Annotation>();
    }

    public ArrayList<Annotation> getAnnotations() {
        return new ArrayList<Annotation>(annotations);
    }

    public Annotation annotate(int starpos, int endpos) {
        Annotation a = new Annotation(startpos, endpos);
        annotations.add(a);
        return a;
    }

    public void unannotate(Annotation a) {
        annotations.remove(a);
    }

    static enum AnnotationTypes {
        HIGHLIGHT, UNDERLINE;
    }

    class Annotation {
        int startPos, endPos;
        AnnotationTypes type;
        Color color;
        Annotation(int start, int end) {
          startPos = start;
          endPos = end;
        }
        public void update(int start, int end) {
          startPos = start;
          endPos = end;
        }
        public void highlight(int red, int green, int blue) {
            type = AnnotationTypes.HIGHLIGHT;
            color = new Color(red, green, blue);
        }
        public void underline(int red, int green, int blue) {
            type = AnnotationTypes.UNDERLINE;
            color = new Color(red, green, blue);
        }
        // getters
        ...
    }

    private Set<Annotation> annotations;
}

So you're just storing the annotation-display data here, and when you load the file and its respective (serialized) Annotations object, you can use each annotation to affect how you display characters between the startPos and endPos in your document.

Although i've used ints for the two positions startPos and endPos, you could also use two or more variables to refer to array indexes, SQLite database table indexes, char positions for simple text documents; whatever your implementation is you could just change that so that you know where to start annotating and where to end annotating with that AnnotationType.

Also, you could set up property change listeners so that when the annotation properties are changed they fire off methods to update your display/view.

like image 80
Ozzy Avatar answered Oct 07 '22 02:10

Ozzy