Question

I am developing an android application which should help users to annotate the PDF file. I am using MuPDF to parse the PDF on android device. I am able to read the PDF but unable to figure out a way which will help me implement annotations features for the PDF. I couldn't find any sample core nor could I find any manuals that explains what I am looking for. Anyone have any sample code, any other material or any links which will give me an idea on how can I support simple annotations (say draw a rectangle or write freely) on PDF files.

Any kind of help in this regard is much appreciated.

Thanks in advance.

Was it helpful?

Solution 2

Do you want to store the annotations in the PDF file or separately?

The former is not possible with muPDF.

However, if you intend to store the annotations separately, then you could conceivably render the PDF to a canvas as background and then draw over it. You'll still have to write the equivalent of a drawing app. There's "FingerPaint" in the Android samples, and also Android Paint in open source.

If you want to highlight specific text in the PDF, things will get quite complex. You'd have to extract the character dimensions from fitz and manage a cursor of sorts in your touch handlers. This isn't impossible, but far from trivial.

OTHER TIPS

You CAN draw annotations by completely delegating into the MuPDF library.

The sample app provided my them includes code for this.

The method in the native layer of MuPDF supporting this is addMarkupAnnotationInternal(int page, PointF[] quadPoints, int type)

The passed PointF[] defines the rectangle that you want to annotate. This is likely to come from a the text the user has selected.

If you are using the java wrapper that they provide, com.artifex.mupdfdemo.MuPDFCore.java, they provide the method called:

MuPDFCore#addMarkupAnnotation(int page, PointF[] quadPoints, Annotation.Type type)

To see how it is actually done and how the MuPDFCore#addMarkupAnnotation receives the quadpoints from the selection of the user, try the sample app while reading the code.

Basically, for adding an annotation you need to:

1- provide a list of points (quads) defining the box and the page of the highlighted area. 2- delegate into the MuPDF library by using the aforementioned method to add the annotation to the document and to render it.

For the first step, you will likely want to let the user select the area of the document to highlight. If so, you can let the user select by starting selection mode with:

MuPDFReaderView#setMode(MuPDFReaderView.Mode.Selecting);

and when the user finishes, get the selection with:

MuPDFView#selectText();

Doing all of this is not straightforward and you need to understand the API, so, I really recommend you to read the sample code.

In regards as where do you save the annotations, that is another story. You can save them in the PDF file (MuPDF lets you doing this) or you can store them anywhere else, which will require more code and will dissociate the annotations from the PDF file.

So, you can do it entirely with MuPDF, there is no need to draw the annotations as another layer to the Canvas/View using the Android API.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top