添加 Text/Annotations 到现有的 PDF 文件和 View/Rendering android 中的输出

Adding Text/Annotations into existing PDF file and View/Rendering the output in android

我正在开发 pdf 编辑器

我已经使用基于 iText

OpenPDF 核心对 pdf 文件进行了更改

我正在使用 AndroidPdfViewer

查看 Pdf 文件

我的问题是:

  1. 将文本、标签或图标等新注释添加到现有 pdf 文件中。 已解决

  2. 在注释添加到 pdf 文件后立即显示新更改。( 已解决 )

  3. 将用户点击转换为 Pdf 文件坐标以根据用户点击位置添加新注释。

  4. 在添加的注释上获取点击事件并读取添加到该注释中的元数据,例如:读取在图标注释上设置的标签哈希 ID。 ( 已解决)

  5. 从 PDF 文件中删除添加的注释。

感谢任何帮助

更新

============================================= ===========================

解决方案 1:添加注释


public static void addWatermark(Context context, String filePath) throws FileNotFoundException, IOException {

        // get file and FileOutputStream
        if (filePath == null || filePath.isEmpty())
            throw new FileNotFoundException();

        File file = new File(filePath);

        if (!file.exists())
            throw new FileNotFoundException();

        try {

            // inout stream from file
            InputStream inputStream = new FileInputStream(file);

            // we create a reader for a certain document
            PdfReader reader = new PdfReader(inputStream);

            // get page file number count
            int pageNumbers = reader.getNumberOfPages();

            // we create a stamper that will copy the document to a new file
            PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(file));

            // adding content to each page
            int i = 0;
            PdfContentByte under;

            // get watermark icon
            Image img = Image.getInstance(PublicFunction.getByteFromDrawable(context, R.drawable.ic_chat));
            img.setAnnotation(new Annotation("tag", "gd871394bh2c3r", 0, 0, 0, 0));
            img.setAbsolutePosition(230, 190);
            img.scaleAbsolute(50, 50);

            while (i < pageNumbers) {
                i++;
                // watermark under the existing page
                under = stamp.getUnderContent(i);
                under.addImage(img);
            }

            // closing PdfStamper will generate the new PDF file
            stamp.close();

        } catch (Exception de) {
            de.printStackTrace();
        }

    }
}

解决方案 2:显示新更改

public void refresh(int currPage) {

            currentPage = currPage;

            if (!hasSize) {
                waitingDocumentConfigurator = this;
                return;
            }
            PDFView.this.recycle();
            PDFView.this.callbacks.setOnLoadComplete(onLoadCompleteListener);
            PDFView.this.callbacks.setOnError(onErrorListener);
            PDFView.this.callbacks.setOnDraw(onDrawListener);
            PDFView.this.callbacks.setOnDrawAll(onDrawAllListener);
            PDFView.this.callbacks.setOnPageChange(onPageChangeListener);
            PDFView.this.callbacks.setOnPageScroll(onPageScrollListener);
            PDFView.this.callbacks.setOnRender(onRenderListener);
            PDFView.this.callbacks.setOnTap(onTapListener);
            PDFView.this.callbacks.setOnLongPress(onLongPressListener);
            PDFView.this.callbacks.setOnPageError(onPageErrorListener);
            PDFView.this.callbacks.setLinkHandler(linkHandler);

            if (pageNumbers != null) {
                PDFView.this.load(documentSource, password, pageNumbers);
            } else {
                PDFView.this.load(documentSource, password);
            }
        }

解决方案 4:单击 pdf 中的对象

我已经创建了注释并将其设置为添加的图像对象,AndroidPdfViewer 有一个事件处理程序,这里是示例

@Override
public void handleLinkEvent(LinkTapEvent event) {
        // do your stuff here
}

我会在我的问题中添加其他新的解决方案,作为更新部分。

Here is my code snippet for adding text into pdf file,

您的代码不会将文本添加到现有 pdf 文件中。它会创建一个 new PDF,向其中添加文本,然后 appends 这个新的 PDF 到可能已经包含 PDF 的现有文件中。结果是一个文件包含两个 PDF。

连接两个相同类型的文件很少会产生该类型的有效文件。这确实适用于某些文本格式(纯文本、csv 等),但几乎不适用于二进制格式,尤其不适用于 PDF。

因此,您的查看器会显示一个无效的 PDF 文件,因此您的查看器可能只是显示错误并退出。但是 PDF 查看器因试图修复给定的文件而臭名昭著,每个查看器都以自己的方式修复。因此,根据查看器的不同,您还可能只看到原始文件、新文件、两者的组合、空文件或其他一些修复结果。

所以你的观察,

but this will replace with all of the Pdf file, not just inserting into it

这并不奇怪,但可能因观众而异。


要使用 OpenPDF(或 6 之前的任何 iText 版本或从此类版本派生的其他库)实际更改现有文件,您应该使用 PdfReader 读取现有 PDF,操作 reader在 PdfStamper 中,然后关闭压模。

例如:

PdfReader reader = new PdfReader("original.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("original-stamped.pdf"));

PdfContentByte cb = stamper.getOverContent(1);
cb.beginText();
cb.setTextMatrix(100, 400);
cb.showText("Text at position 100,400.");
cb.endText();

stamper.close();
reader.close();

这里要特别注意使用不同的文件名。关闭 stamperreader 后,您可以删除原始 PDF 并将其替换为标记版本。

如果不需要第二个文件,您可以选择用 ByteArrayOutputStream 初始化 PdfStamper,然后关闭 stamperreader 替换原始文件的内容与 ByteArrayOutputStream.

的内容