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

Bob*_*oid 69 java pdf android pdf-generation pdf-annotations

  • 许多应用程序,如Android市场上的RepliGo,Aldiko,Mantano,ezPdf,在其pdf查看器中进行了这种类型的注释,如下图所示.
  • 我试过很多方法来实现这个注释,但我失败了.我有一个用于android的pdf查看器和用于使用iText绘制线条的注释的单独java代码.
  • 我的问题是我能否在android中实现iText.如果可能,我必须导入哪个包?
  • 此外,在某些应用程序中,canvas方法用于绘制线条.是否可以在android中包含此canvas方法而不是使用注释?目标是拥有与注释相同的功能.
  • 在下面的图像(RepliGo PDF Reader)中,他们使用哪种代码进行注释? 在此输入图像描述

Ozz*_*zzy 6

你的问题似乎是允许用户在android/java中注释PDF文件的方法,所以这里有一种方法,虽然它可能不是最好的解决方案.

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

这意味着它不会创建带有这些注释的新PDF文件,而是只存储加载到应用程序中的每个PDF文件的用户数据,并在用户再次加载PDF文件时显示.(所以它似乎是注释的).

例:

  1. 将PDF文件文本,文本格式和图像读入您的应用程序
  2. 显示文档(如文字处理程序)
  3. 允许用户编辑和注释文档
  4. 在应用中保存更改和注释数据(不是PDF文件)
  5. 再次加载文件时,应用先前存储的更改和注释.

您的注释类可能如下所示:

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;
}
Run Code Online (Sandbox Code Playgroud)

因此,您只需在此处存储注释显示数据,并在加载文件及其各自(序列化的)注释对象时,可以使用每个注释来影响在文档中startPosendPos文档之间显示字符的方式.

虽然我用intS表示两个位置startPosendPos,您也可以使用两个或多个变量来引用数组索引,SQLite数据库表索引,简单的文本文件的字符位置; 无论您的实现是什么,您都可以更改它,以便您知道从哪里开始注释以及在何处结束使用该AnnotationType进行注释.

此外,您可以设置属性更改侦听器,以便在更改注释属性时触发更新显示/视图的方法.