- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用 pdfbox 2.0.8 将多个图像添加到 pdf 中,但目前只会添加一个。我有两个不同的图像,应该附加到两个不同的 acrofields,但只会添加我的列表中的最后一个。
这是我的测试函数:
@Test
public void attachBulkImageToField(){
List<ImageData> data = new ArrayList<>();
data.add(new ImageData(signatureAusstellerField,signatureAussteller.toPath()));
data.add(new ImageData(signatureDienstleisterField, signatureDienstleister.toPath()));
ImageToFieldDrawer imgDrawer = new ImageToFieldDrawer(pdf);
assertTrue(imgDrawer.drawImageToField(data, Paths.get("d:\\imageBulk.pdf")));
}
public boolean drawImageToField(List<ImageData> data, final Path outPath) {
try {
for (ImageData element : data) {
addImageForField(element.getImagePath(), getAcroFieldWithName(element.getFieldName()));
}
savePdf(outPath);
return true;
} catch (IOException e) {
e.printStackTrace();
} catch (PDFSizeException e) {
e.printStackTrace();
}
return false;
}
private void savePdf(Path outPath) throws IOException {
pdDocument.save(outPath.toFile());
pdDocument.close();
}
private void addImageForField(Path signature, AcroField targetField) throws IOException {
PDPage page = pdDocument.getPage(targetField.getPageNr() - 1);
DrawImage image = new DrawImage(Files.readAllBytes(signature), 0, 0);
PDImageXObject pdImage = PDImageXObject.createFromFile(signature.toAbsolutePath().toString(), pdDocument);
try(PDPageContentStream contentStream = new PDPageContentStream(pdDocument, page)){
contentStream.drawImage(pdImage, targetField.getX(), targetField.getY(), targetField.getWidth(), targetField.getHeight());
}
}
public class ImageData {
private String fieldName;
private Path imagePath;
public ImageData(String fieldName, Path imagePath) {
this.fieldName = fieldName;
this.imagePath = imagePath;
}
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
public Path getImagePath() {
return imagePath;
}
public void setImagePath(Path imagePath) {
this.imagePath = imagePath;
}
}
最佳答案
您使用以下方法为目标页面创建内容流
PDPageContentStream contentStream = new PDPageContentStream(pdDocument, page)
这个构造函数被记录为
/**
* Create a new PDPage content stream. This constructor overwrites all existing content streams
* of this page.
*
* @param document The document the page is part of.
* @param sourcePage The page to write the contents to.
* @throws IOException If there is an error writing to the page contents.
*/
public PDPageContentStream(PDDocument document, PDPage sourcePage) throws IOException
因此,使用此构造函数,您将覆盖该页面的所有现有内容流!特别是,您会覆盖任何先前添加的用于绘制另一图像的指令...
您应该使用不同的构造函数,例如
/**
* Create a new PDPage content stream. If the appendContent parameter is set to
* {@link AppendMode#APPEND}, you may want to use
* {@link #PDPageContentStream(PDDocument, PDPage, PDPageContentStream.AppendMode, boolean, boolean)}
* instead, with the fifth parameter set to true.
*
* @param document The document the page is part of.
* @param sourcePage The page to write the contents to.
* @param appendContent Indicates whether content will be overwritten, appended or prepended.
* @param compress Tell if the content stream should compress the page contents.
* @throws IOException If there is an error writing to the page contents.
*/
public PDPageContentStream(PDDocument document, PDPage sourcePage, AppendMode appendContent,
boolean compress) throws IOException
使用AppendMode.APPEND
或AppendMode.PREPEND
,具体取决于新内容是否应绘制在先前绘制的内容之上或之下。
关于java - PdfBox 将多个图像添加到 pdf 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47934390/
在 apache PDFBox 网站上,http://pdfbox.apache.org/2.0/migration.html ,PDF 渲染示例。 ImageIOUtil.writeImage(bi
我们正在使用 PDFBox 从 PDF 中提取文本。 某些 PDF 的文本无法正确提取。 下图显示了 PDF 中的一部分作为图像: 文本提取后,我们得到以下文本: 3, 8 5 EU R 1 Nett
任何人都可以帮我了解如何使用 Apache PDFBOX 将文本替换为图像吗? 最佳答案 import java.io.File; import java.io.IOException;
我正在升级我的项目中的库并将 pdfbox 从 0.6.7 升级到 1.6.0 版,但找不到 LucenePDFDocument 类。 Apache 页面上的文档/教程中仍然提到了该类。有任何想法吗?
在我的应用程序中,我使用 Apache PDFBox 来呈现 PDF 文件并进行静音打印。 PDFBox 可以很好地渲染 PFD,但我遇到了问题 缩放 来到图片。 在这里我想在打印 PDF 之前设置页
我正在使用使用 pdfbox 2.0.3 的 Apache Tika 1.14。 我用它来提取文件的文本内容。在生产模式下处理许多文件时,我会记录许多如下语句: WARN o.a.p.pdmodel
有人能给我一个关于如何使用 Apache PDFBox 将 PDF 文件转换为不同图像的示例吗(PDF 的每一页一个图像)? 最佳答案 1.8.* 版本的解决方案: PDDocument docume
我正在使用 PDF BOX 获取 PDF 文本的颜色信息。我可以使用以下代码获得输出。但是我的疑问是 StrokingColor 代表什么,Non stroking color 代表什么。基于此,我将
我终于成功地让 PDFBox 打印我的 unicodes。但现在,我想了解我提出的解决方案。下面的代码可以运行并将 ≥ 打印到页面上。 有两件事不起作用: 改变PDType0Font.load(doc
我正在使用 PDF BOX 获取 PDF 文本的颜色信息。我可以使用以下代码获得输出。但是我的疑问是 StrokingColor 代表什么,Non stroking color 代表什么。基于此,我将
我的项目要求在 Apache pdfbox api 的帮助下生成发票。截至目前,我可以在生成的 pdf 中插入图像、文本,但在生成表格时发现困难。我什至找不到单个示例模板。如果有人有请提供链接。 注意
我如何使用 PDFBox 2.0.0 获取字段位置?在 Pdfbox 1.8.11 中,我是这样工作的: String formTemplate = "Template.pdf
我在我们的一个项目中使用了 CMS Magnolia。在日志文件中有很多错误,例如: 字体 ArialMT 中使用的 OpenType 布局表未在 PDFBox 中实现 这对 PDF 有什么影响?可以
我一直在使用 PDFBox 1.8 来处理 pdf。现在我计划转向 PDFBox 2.0-RC-2。我在迁移时遇到一些问题。 在 PDFBox 1.8 中,我曾经使用以下方法从 PDPage 获取 t
我正在使用 PDFBox 1.7.0(由于生产服务器中的版本较旧,我无法选择该版本)。我正在尝试将图像添加到已有 Logo 的现有 PDF 中。当我添加新图像时,旧图像就像被替换一样消失了。 // U
我正在使用 Apache PDFBox 库 在可填写的 PDF 表格 (AcroFrom) 中填写信息。完成信息填写后,我需要编写一个新的PDF文件(不可编辑格式)。 我尝试了 setReadOnly
在对 PDDocument 进行数字签名后,我已关闭 Apache PDFBox 中的 PDDocument。我收到警告:当我关闭我的实例时,You did not close PDF Documen
我在 wildfly 模块中使用 Apache Tika(tika-app 1.17)。当我开始提取 PDF 时,它总是抛出错误: java.lang.NoClassDefFoundError: Co
我尝试使用 pdfbox 并使用 groovy 进行编译,但我不知道 这是我使用的代码 groovyc main.groovy 但不起作用,请帮助我 this.class.classLoader.ro
我在 wildfly 模块中使用 Apache Tika(tika-app 1.17)。当我开始提取 PDF 时,它总是抛出错误: java.lang.NoClassDefFoundError: Co
我是一名优秀的程序员,十分优秀!