如何使用 JPod 在 PDF 中嵌入图像?
How to embed an image in a PDF using JPod?
我想使用 JPod 库将图像添加到生成的 pdf。
JPod DrawInLineImagePlain 示例(位于 https://github.com/born2snipe/learning-jpod/blob/master/not-in-repo/examples/de/intarsys/pdf/example/content/DrawInlineImagePlain.java)建议像这样添加图像(假设先前定义了 CSCreator 创建者):
// image bytes
private byte[] data = new byte[] { (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170 };
creator.transform(400, 0, 0, 400, 100, 100);
creator.perform("BI");
creator.perform("/W 16 /H 16 /BPC 1 /CS /G ID");
COSString stringData = COSString.create(data);
creator.perform("EI", stringData);
creator.close();
但是,我不能这样做,因为 JPod 不再支持 perform()。
看来 inlineImage() 应该可以嵌入图像。 JPod 文档这样描述 inlineImage():
Stroke an inlined image. PDF graphics operators "BI", "ID", "EI".
这让我假设 inlineImage() 可以对 DrawInLineImagePlain 示例中的所有图形运算符进行编码,并且还可以对宽度、高度、每个组件的位数、颜色 space 和使用 PDImage 参数中的信息的图像数据。
所以我尝试像这样平行示例(再次假设先前定义了 CSCreator 创建者):
// image bytes
byte[] data = new byte[] { (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170 };
PDImage imageData = (PDImage) PDImage.META.createNew();
imageData.setBytes(data);
imageData.setHeight(16);
imageData.setWidth(16);
imageData.setBitsPerComponent(1);
imageData.setColorSpace(PDColorSpace.getNamed(PDColorSpace.CN_CS_DeviceGray));
creator.inlineImage(imageData);
creator.close();
不幸的是,这会生成一个空白的 PDF。我需要更改什么才能使图像显示在 PDF 中?
我猜文档或页面设置没有问题,因为我可以用此图像嵌入替换文本或线条图,并且文本或线条在 PDF 中显示得很好。
不幸的是,inlineImage
没有在 CSCreator 中实现。使用 addOperand
直接向内容流添加内容。您的图片代码为:
content.addOperation(new CSOperation(CSOperators.CSO_BI));
CSOperation operationID = new CSOperation(CSOperators.CSO_ID);
operationID.addOperand(PDImage.DK_W);
operationID.addOperand(COSInteger.create(16));
operationID.addOperand(PDImage.DK_H);
operationID.addOperand(COSInteger.create(16));
operationID.addOperand(PDImage.DK_BPC);
operationID.addOperand(COSInteger.create(1));
operationID.addOperand(PDImage.DK_CS);
operationID.addOperand(PDColorSpace.CN_CS_G);
content.addOperation(operationID);
CSOperation operationEI = new CSOperation(CSOperators.CSO_EI);
operationEI.addOperand(COSString.create(data));
content.addOperation(operationEI);
或者使用 doImage
将常规图像添加到您的文档
我想使用 JPod 库将图像添加到生成的 pdf。
JPod DrawInLineImagePlain 示例(位于 https://github.com/born2snipe/learning-jpod/blob/master/not-in-repo/examples/de/intarsys/pdf/example/content/DrawInlineImagePlain.java)建议像这样添加图像(假设先前定义了 CSCreator 创建者):
// image bytes
private byte[] data = new byte[] { (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170 };
creator.transform(400, 0, 0, 400, 100, 100);
creator.perform("BI");
creator.perform("/W 16 /H 16 /BPC 1 /CS /G ID");
COSString stringData = COSString.create(data);
creator.perform("EI", stringData);
creator.close();
但是,我不能这样做,因为 JPod 不再支持 perform()。
看来 inlineImage() 应该可以嵌入图像。 JPod 文档这样描述 inlineImage():
Stroke an inlined image. PDF graphics operators "BI", "ID", "EI".
这让我假设 inlineImage() 可以对 DrawInLineImagePlain 示例中的所有图形运算符进行编码,并且还可以对宽度、高度、每个组件的位数、颜色 space 和使用 PDImage 参数中的信息的图像数据。
所以我尝试像这样平行示例(再次假设先前定义了 CSCreator 创建者):
// image bytes
byte[] data = new byte[] { (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170, (byte) 170,
(byte) 170, (byte) 170, (byte) 170, (byte) 170 };
PDImage imageData = (PDImage) PDImage.META.createNew();
imageData.setBytes(data);
imageData.setHeight(16);
imageData.setWidth(16);
imageData.setBitsPerComponent(1);
imageData.setColorSpace(PDColorSpace.getNamed(PDColorSpace.CN_CS_DeviceGray));
creator.inlineImage(imageData);
creator.close();
不幸的是,这会生成一个空白的 PDF。我需要更改什么才能使图像显示在 PDF 中?
我猜文档或页面设置没有问题,因为我可以用此图像嵌入替换文本或线条图,并且文本或线条在 PDF 中显示得很好。
inlineImage
没有在 CSCreator 中实现。使用 addOperand
直接向内容流添加内容。您的图片代码为:
content.addOperation(new CSOperation(CSOperators.CSO_BI));
CSOperation operationID = new CSOperation(CSOperators.CSO_ID);
operationID.addOperand(PDImage.DK_W);
operationID.addOperand(COSInteger.create(16));
operationID.addOperand(PDImage.DK_H);
operationID.addOperand(COSInteger.create(16));
operationID.addOperand(PDImage.DK_BPC);
operationID.addOperand(COSInteger.create(1));
operationID.addOperand(PDImage.DK_CS);
operationID.addOperand(PDColorSpace.CN_CS_G);
content.addOperation(operationID);
CSOperation operationEI = new CSOperation(CSOperators.CSO_EI);
operationEI.addOperand(COSString.create(data));
content.addOperation(operationEI);
或者使用 doImage