How to copy text using MuPDF?

2.3k views Asked by At

I'm using MuPDF lib in android platform. And i want to extract text from pdf file. I've got an example from Github, that shows pdf files normally.

So, is it possible to extract text from pdf using MuPDF and copy the texts ???

1

There are 1 answers

0
Ganesh Kanna On BEST ANSWER

Yes you can extract the text from PDF.

If you got the library sample you can see the following code in MuPDFPageView.java

@Override
protected TextWord[][] getText() {
    return mCore.textLines(mPageNumber);
}

This will extract the text from PDF by page number.

See the usage of below method you will get idea about how selection and copy text works

@TargetApi(11)
public boolean copySelection() {
    final StringBuilder text = new StringBuilder();

    processSelectedText(new TextProcessor() {
        StringBuilder line;

        public void onStartLine() {
            line = new StringBuilder();
        }

        public void onWord(TextWord word) {
            if (line.length() > 0)
                line.append(' ');
            line.append(word.w);
        }

        public void onEndLine() {
            if (text.length() > 0)
                text.append('\n');
            text.append(line);
        }
    });

    if (text.length() == 0)
        return false;

    int currentApiVersion = Build.VERSION.SDK_INT;
    if (currentApiVersion >= Build.VERSION_CODES.HONEYCOMB) {
        android.content.ClipboardManager cm = (android.content.ClipboardManager)mContext.getSystemService(Context.CLIPBOARD_SERVICE);

        cm.setPrimaryClip(ClipData.newPlainText("MuPDF", text));
    } else {
        android.text.ClipboardManager cm = (android.text.ClipboardManager)mContext.getSystemService(Context.CLIPBOARD_SERVICE);
        cm.setText(text);
    }

    deselectText();

    return true;
}