4

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.

Shobhit
  • 407
  • 5
  • 15
  • Please tell us what do you meant by read, is it displaying pdf on Screen or reading the PDF data, such as meta data, text, images , etc – Salman Khakwani Oct 02 '13 at 09:18
  • @SalmanKhakwani: I need to do both the job, i.e display the pdf on the screen and read the pdf data such as meta data text, images etc. – Shobhit Oct 11 '13 at 11:15
  • If you're already open for commercial solutions such as muPDF, consider http://pspdfkit.com/android as alternative, featuring a fully pre-built UI for editing annotations. – steipete May 17 '18 at 13:26

2 Answers2

5

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.

GaRRaPeTa
  • 5,459
  • 4
  • 37
  • 61
  • Could you explain why my answer is not working for you and you downvoted it? – GaRRaPeTa Aug 19 '14 at 12:10
  • can you help me with annotation inside password protected pdf? Here is the link : http://stackoverflow.com/questions/28585742/how-to-save-annotation-on-password-protected-pdf-using-mupdf – Manoj Feb 19 '15 at 13:06
  • can you point me how show/hide annotation for particular page? – Manoj Jun 29 '15 at 11:17
  • @SaravanaKumarChinnaraj take a look at MuPdfActivity in your mupdf project. There you can find sample code – Manoj Jul 07 '15 at 09:09
  • @Manoj actually sorry to say this.. i need to make sticky notes annotation in pdf.. – Saravana Kumar Chinnaraj Jul 07 '15 at 09:17
  • @SaravanaKumarChinnaraj you will have to maintain a db and populate data into floating widgets(custom view) manually.. there is no other way in mupdf as far as my knowledge. – Manoj Jul 07 '15 at 09:19
1

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.

323go
  • 14,143
  • 6
  • 33
  • 41
  • Thanks for the quick reply. I can understand your answer, but I am not sure of how to render the PDF to a canvas as background. FYI: I am using an object of PixmapView([check this link](http://stackoverflow.com/a/8587527/2471621)) which has my PDF and this is added to a pdfContainer(RelativeLayout) which displays it. I am also looking to highlight the specific text and so could you also tell me what did you exactly meant by **fitz** here? thanks! – Shobhit Aug 09 '13 at 16:33
  • And yes, I want to save to annotations separately. – Shobhit Aug 09 '13 at 16:48
  • You'll have to extend PixMapView to handle your own drawing in this case. Fitz is muPDF's rendering engine, and there are structs in there such as bb_rect which you'll need to access. – 323go Aug 09 '13 at 16:50
  • I am trying to understand more on Fitz and I would be really thankful to you if you could provide me some more details (links or sample code etc, basically anything) on FItz so that I can get an overview on how am I supposed to use them in my project. Thanks. – Shobhit Oct 18 '13 at 07:40
  • There are a couple of open-source projects on google code, not the least being https://code.google.com/p/mupdf/, and there's a list here: http://www.coderanch.com/t/527716/Android/Mobile/Open-source-PDF-viewer-Android – 323go Oct 18 '13 at 15:25
  • This answer is not right. You CAN draw annotations using MuPDF directly delegating into the library, with no need of drawing them as another layer. See my answer. – GaRRaPeTa Aug 06 '14 at 17:06
  • You missed the point of my answer. If you want to draw annotations in muPDF, you have to add them to the document, just as you wrote in your answer. If you wanted to store them separately, you would have to recreate the document each time, which is less than optimal. – 323go Aug 07 '14 at 01:13