如何在android中为pdf查看器创建突出显示、删除、下划线、绘制、添加文本等注释?

时间:2022-05-19 21:08:34
  • Many applications like RepliGo, Aldiko, Mantano, ezPdf in android market make this type of annotation in their pdf viewer shown in the below image.
  • 许多应用程序,如RepliGo, Aldiko, Mantano, ezPdf在android market,在他们的pdf查看器中,在下图中显示这类注释。
  • 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.
  • 我尝试了很多方法来实现这个注释,但是失败了。我有一个用于android的pdf查看器和用于注释的独立java代码,使用iText绘制线条。
  • My question is can i able to implement iText in android. If it's possible, which package do I have to import?
  • 我的问题是能否在android上实现iText。如果可能的话,我需要导入哪个包?
  • 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.
  • 在某些应用程序中,还使用画布方法绘制线条。是否可以在android中包含这个canvas方法而不是使用注释?目标是拥有与注释相同的特性。
  • In the below image (RepliGo PDF Reader) which kind of code did they use for annotations? 如何在android中为pdf查看器创建突出显示、删除、下划线、绘制、添加文本等注释?
  • 在下面的图片(RepliGo PDF Reader)中,他们在注释中使用了哪种代码?

2 个解决方案

#1


6  

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.

您的问题似乎是,有哪些方法允许用户在android/java中注释PDF文件,所以这里有一个方法适合您,尽管它可能不是最好的解决方案。

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.

我想指出的是,实际上并不需要编辑实际的PDF文件来允许用户添加和查看注释。您的应用程序可以单独存储注释的数据,为每个文件存储这些注释,并在加载文件时加载它们。

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).

这意味着它不会创建一个带有这些注释的新PDF文件,而是将为加载到应用程序的每个PDF文件存储用户数据,并在用户再次加载PDF文件时显示出来。(这似乎是有注解的)。

Example:

例子:

  1. Read PDF file text, text-formatting & images into your app
  2. 阅读PDF文件文本,文本格式和图片到你的应用。
  3. Display a document (like a word-processor)
  4. 显示文档(如文字处理程序)
  5. Allow the user to edit & annotate the document
  6. 允许用户编辑和注释文档。
  7. Save changes & annotation-data in your app (not the PDF file)
  8. 在应用程序中保存更改和注释数据(不是PDF文件)
  9. When loading the file again, apply changes & annotations previously stored.
  10. 再次加载文件时,应用先前存储的更改和注释。

Your annotation class might look something like this:

您的annotation类可能是这样的:

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.

因此,您只是在这里存储注释显示数据,当您加载文件及其各自的(序列化)注释对象时,您可以使用每个注释来影响如何在您的文档中显示startPos和endPos之间的字符。

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.

虽然我已经使用了两个位置startPos和endPos的ints,但您也可以使用两个或多个变量来引用数组索引、SQLite数据库表索引、用于简单文本文档的char位置;无论你的实现是什么,你都可以改变它,这样你就知道从哪里开始注释,从哪里结束注释。

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.

此外,还可以设置属性更改侦听器,以便当注释属性更改时,它们触发方法更新显示/视图。

#2


0  

Pdf-annotation is an open source and good point to start with https://code.google.com/p/pdf-annotation/

Pdf-annotation是一个开放源码的好点,可以从https://code.google.com/p/pdf-annotation/开始。

#1


6  

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.

您的问题似乎是,有哪些方法允许用户在android/java中注释PDF文件,所以这里有一个方法适合您,尽管它可能不是最好的解决方案。

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.

我想指出的是,实际上并不需要编辑实际的PDF文件来允许用户添加和查看注释。您的应用程序可以单独存储注释的数据,为每个文件存储这些注释,并在加载文件时加载它们。

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).

这意味着它不会创建一个带有这些注释的新PDF文件,而是将为加载到应用程序的每个PDF文件存储用户数据,并在用户再次加载PDF文件时显示出来。(这似乎是有注解的)。

Example:

例子:

  1. Read PDF file text, text-formatting & images into your app
  2. 阅读PDF文件文本,文本格式和图片到你的应用。
  3. Display a document (like a word-processor)
  4. 显示文档(如文字处理程序)
  5. Allow the user to edit & annotate the document
  6. 允许用户编辑和注释文档。
  7. Save changes & annotation-data in your app (not the PDF file)
  8. 在应用程序中保存更改和注释数据(不是PDF文件)
  9. When loading the file again, apply changes & annotations previously stored.
  10. 再次加载文件时,应用先前存储的更改和注释。

Your annotation class might look something like this:

您的annotation类可能是这样的:

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.

因此,您只是在这里存储注释显示数据,当您加载文件及其各自的(序列化)注释对象时,您可以使用每个注释来影响如何在您的文档中显示startPos和endPos之间的字符。

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.

虽然我已经使用了两个位置startPos和endPos的ints,但您也可以使用两个或多个变量来引用数组索引、SQLite数据库表索引、用于简单文本文档的char位置;无论你的实现是什么,你都可以改变它,这样你就知道从哪里开始注释,从哪里结束注释。

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.

此外,还可以设置属性更改侦听器,以便当注释属性更改时,它们触发方法更新显示/视图。

#2


0  

Pdf-annotation is an open source and good point to start with https://code.google.com/p/pdf-annotation/

Pdf-annotation是一个开放源码的好点,可以从https://code.google.com/p/pdf-annotation/开始。