[英]Adding Text/Annotations into existing PDF file and View/Rendering the output in android
我正在使用pdf 编辑器。
我已经使用基于 iText 的OpenPDF核心对 pdf 文件进行了更改
我正在使用AndroidPdfViewer查看 Pdf 文件
我的问题是:
将文本或标签或图标等新注释添加到现有 pdf 文件中。 (已解决)
在将注释添加到 pdf 文件后立即显示新更改。 (已解决)
将用户点击转换为 Pdf 文件坐标,以根据用户点击位置添加新注释。
在添加的注释上获取点击事件并读取添加到该注释中的元数据,例如:读取在图标注释上设置的标签 hash 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();
}
}
}
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);
}
}
我已经创建注释并将其设置为添加图像 object, AndroidPdfViewer
有一个事件处理程序,这里是示例
@Override
public void handleLinkEvent(LinkTapEvent event) {
// do your stuff here
}
我将在我的问题中添加其他新的解决方案,作为更新部分。
这是我在 pdf 文件中添加文本的代码片段,
您的代码不会将文本添加到现有的pdf 文件中。 它创建一个新的 PDF,向其中添加文本,并将这个新的 PDF附加到可能已经包含 PDF 的现有文件中。 结果是一个包含两个 PDF 的文件。
连接相同类型的两个文件很少会产生该类型的有效文件。 这确实适用于某些文本格式(纯文本,csv,...),但几乎不适用于二进制格式,特别是不适用于 PDF。
因此,您的查看器会显示一个作为 PDF 无效的文件,因此您的查看器可能只是显示错误并退出。 但是 PDF 查看器因试图修复给定的文件而臭名昭著,每个查看器都以自己的方式。 因此,根据查看器的不同,您也可以仅看到原始文件、仅新文件、两者的组合、空文件或其他修复结果。
所以你的观察,
但这将替换为所有 Pdf 文件,而不仅仅是插入其中
这并不奇怪,但可能因观众而异。
要使用 OpenPDF(或 6 之前的任何 iText 版本或从此类版本派生的其他库)实际更改现有文件,您应该使用 PdfReader 阅读现有的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 并替换为盖章版本。
如果不希望有第二个文件,您也可以使用ByteArrayOutputStream
初始化PdfStamper
并在关闭stamper
和reader
后将原始文件的内容替换为ByteArrayOutputStream
的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.