Warm tip: This article is reproduced from serverfault.com, please click

其他-在现有的PDF文件中添加文本/注释并在android中查看/渲染输出

(其他 - Adding Text/Annotations into existing PDF file and View/Rendering the output in android)

发布于 2020-12-08 12:28:18

我正在使用pdf编辑器

我已经使用基于iText的OpenPDF核心对pdf文件进行了更改

我正在使用AndroidPdfViewer查看Pdf文件

我的问题是:

  1. 在现有的pdf文件中添加文本,标签或图标之类的新注释。 (已解决

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

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

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

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

任何帮助表示赞赏

更新

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

解决方案1:添加注释

  • 这是我的代码片段,用于将图标注释添加到现有的pdf文件中。

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:显示新更改

  • 这是我的代码段,用于在添加注释后刷新视图,我已将其添加到AndroidPdfViewer核心类中。
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
}

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

Questioner
Hamid Reza
Viewed
0
mkl 2020-12-10 17:56:58

这是我的代码片段,用于将文本添加到pdf文件中,

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

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

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

所以你的观察

但这将替换为所有Pdf文件,而不仅仅是插入其中

不足为奇,但视观众而定可能会有所不同。


要使用OpenPDF(或6之前的任何iText版本或从该版本派生的其他库)实际更改现有文件,你应该使用来阅读现有的PDF PdfReader,在中操纵该阅读器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();

特别要注意在这里使用不同的文件名。关闭stamper和之后,reader你可以删除原始PDF并将其替换为带有标记的版本。

如果不希望再有一个文件,则可以PdfStamperByteArrayOutputStream初始化,然后关闭,stamper然后用reader替换原始文件的内容ByteArrayOutputStream