Ham*_*eza 5 pdf android itext androidpdfviewer openpdf
我正在使用pdf 编辑器。
我使用基于 iText 的OpenPDF核心对 pdf 文件进行了更改
我正在使用AndroidPdfViewer查看 Pdf 文件
我的问题是:
将新的注释(如文本或标签或图标)添加到现有的 pdf 文件中。 (已解决)
在注释添加到 pdf 文件后立即显示新更改。(已解决)
将用户点击转换为 Pdf 文件坐标以根据用户点击位置添加新注释。
在添加的注释上获取点击事件并读取添加到该注释中的元数据,例如:读取在图标注释上设置的标签哈希 ID。(已解决)
从 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了注释并将其设置为添加的图像对象,AndroidPdfViewer有一个事件处理程序,这是示例
@Override
public void handleLinkEvent(LinkTapEvent event) {
// do your stuff here
}
Run Code Online (Sandbox Code Playgroud)
我将在我的问题中添加其他新解决方案,作为更新部分。
这是我的代码片段,用于将文本添加到 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();
Run Code Online (Sandbox Code Playgroud)
特别注意此处使用不同的文件名。关闭后,stamper您reader可以删除原始 PDF 并将其替换为盖章版本。
如果不需要第二个文件,您也可以PdfStamper使用 a进行初始化,并ByteArrayOutputStream在关闭后将原始文件的内容替换为.stamperreaderByteArrayOutputStream