gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:46:40 26 4
gpt4 key购买 nike

我正在处理 pdf编辑器 .
我已使用 OpenPDF 对 pdf 文件进行了更改基于 iText 的核心
我正在查看带有 AndroidPdfViewer 的 Pdf 文件
我的 问题 是:

  • 将文本或标签或图标等新注释添加到现有 pdf 文件中。 ( 已解决 )
  • 在将注释添加到 pdf 文件后立即显示新更改。( 已解决 )
  • 将用户点击转换为 Pdf 文件坐标,以根据用户点击的位置添加新的注释。
  • 在添加的注释上获取点击事件并读取添加到该注释中的元数据,例如:读取在图标注释上设置的标签哈希 id。 ( 已解决 )
  • 从 PDF 文件中删除添加的注释。

  • 任何帮助表示赞赏
    更新
    ==================================================== =======================
    解决方案 1:添加注释
  • 这是我的代码 fragment ,用于将图标注释添加到现有的 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:显示新更改
  • 这是我添加注释后刷新 View 的代码 fragment ,我已将其添加到 AndroidPdfViewer核心类(class)。
  • 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 文件中。它创建一个新的 PDF,向其中添加文本,并将这个新的 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 , 在 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 并将其替换为加盖版本。
    如果不希望有第二个文件,您也可以初始化 PdfStamperByteArrayOutputStream并在关闭 stamper 后和 reader将原始文件的内容替换为 ByteArrayOutputStream 的内容.

    关于android - 在现有 PDF 文件中添加文本/注释并在 android 中查看/渲染输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65198940/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com