如何使用 Itext 将图像文件添加到现有的 PDF 中,并为其声明一个唯一的名称?
How to use Itext to add an image file into an existing PDF, and declare a unique name for it?
为了下次阅读PDF时能找到添加的图片文件并替换为另一个图片文件,我想使用Itext将图片文件添加到现有的PDF中,并为其声明一个唯一的名称.
我的代码:
final PdfName key = new PdfName("MY_SIGN_KEY");
final PdfName val = new PdfName("MY_SIGN_VAL");
Image signImage=Image.getInstance(signPngFile.getAbsolutePath());
signImage.setAlignment(1);
signImage.scaleAbsolute(newWidth, newHeight);
signImage.setAbsolutePosition(200,200);
PdfContentByte over = stamper.getOverContent(1);
PdfImage stream = new PdfImage(signImage, "", null);
stream.put(key,val);// a unique name for it.(设置唯一标识符)
//PdfIndirectObject ref=over.getPdfWriter().addToBody(stream);
//signImage.setDirectReference(ref.getIndirectReference());
over.addImage(signImage);
我试过你的代码,它对我有用。请参阅 AddImageWithID 示例:
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
Image image = Image.getInstance(IMG);
PdfImage stream = new PdfImage(image, "", null);
stream.put(new PdfName("ITXT_SpecialId"), new PdfName("123456789"));
PdfIndirectObject ref = stamper.getWriter().addToBody(stream);
image.setDirectReference(ref.getIndirectReference());
image.setAbsolutePosition(36, 400);
PdfContentByte over = stamper.getOverContent(1);
over.addImage(image);
stamper.close();
reader.close();
}
在这个例子中,我将一个名为 hello.pdf and I add an image named bruno.jpg with the file hello_with_image_id.pdf 的文件作为结果。
图像看起来不黑:
添加ID:
你能试试我分享的代码,看看问题是否仍然存在。
我能想到您得到黑色图像的一个原因:在我们的代码中,我们假设添加了单个图像。对于 JPEG,情况总是如此。但是,对于 PNG 或 GIF,添加一个源图像可能会导致添加两个图像。严格来说,PDF 不支持透明图像(取决于您如何理解透明图像的概念)。每当您添加带有透明部分的单个源图像时,将向 PDF 添加两幅图像:一幅不透明图像和一幅图像遮罩。不透明图像和图像蒙版的组合产生了被感知为透明图像的东西。也许这就是你的情况。
为了下次阅读PDF时能找到添加的图片文件并替换为另一个图片文件,我想使用Itext将图片文件添加到现有的PDF中,并为其声明一个唯一的名称. 我的代码:
final PdfName key = new PdfName("MY_SIGN_KEY");
final PdfName val = new PdfName("MY_SIGN_VAL");
Image signImage=Image.getInstance(signPngFile.getAbsolutePath());
signImage.setAlignment(1);
signImage.scaleAbsolute(newWidth, newHeight);
signImage.setAbsolutePosition(200,200);
PdfContentByte over = stamper.getOverContent(1);
PdfImage stream = new PdfImage(signImage, "", null);
stream.put(key,val);// a unique name for it.(设置唯一标识符)
//PdfIndirectObject ref=over.getPdfWriter().addToBody(stream);
//signImage.setDirectReference(ref.getIndirectReference());
over.addImage(signImage);
我试过你的代码,它对我有用。请参阅 AddImageWithID 示例:
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
Image image = Image.getInstance(IMG);
PdfImage stream = new PdfImage(image, "", null);
stream.put(new PdfName("ITXT_SpecialId"), new PdfName("123456789"));
PdfIndirectObject ref = stamper.getWriter().addToBody(stream);
image.setDirectReference(ref.getIndirectReference());
image.setAbsolutePosition(36, 400);
PdfContentByte over = stamper.getOverContent(1);
over.addImage(image);
stamper.close();
reader.close();
}
在这个例子中,我将一个名为 hello.pdf and I add an image named bruno.jpg with the file hello_with_image_id.pdf 的文件作为结果。
图像看起来不黑:
添加ID:
你能试试我分享的代码,看看问题是否仍然存在。
我能想到您得到黑色图像的一个原因:在我们的代码中,我们假设添加了单个图像。对于 JPEG,情况总是如此。但是,对于 PNG 或 GIF,添加一个源图像可能会导致添加两个图像。严格来说,PDF 不支持透明图像(取决于您如何理解透明图像的概念)。每当您添加带有透明部分的单个源图像时,将向 PDF 添加两幅图像:一幅不透明图像和一幅图像遮罩。不透明图像和图像蒙版的组合产生了被感知为透明图像的东西。也许这就是你的情况。