问题描述
使用PDFBox 2.0.20将图像添加到OCG层时遇到麻烦。我曾经以PDFbox - how can i add content to a layer?中的mkl答案为基础建立我的代码,当时我可能已经宣誓它可以正常工作,但是现在,如果我查看生成的PDF并打开或关闭图层,则其余部分都不会切换图像小组。
更令人困惑的是,如果我在Adobe Acrobat中的“内容”下查看,则每次调用beginMarkedContent()
都会看到新的容器。每个容器的字典都有正确的图层名称,就像PDFBox是在复制图层而不是附加到图层一样,尽管在图层选项卡下没有新图层出现。
public void add_image(
String imageFilename,Integer adX,Integer adY,Integer rotation,Integer adWidth,Integer adHeight,String stickId,Integer fontSize,String placement)
throws Exception,IOException {
// get the OCG layer
PDDocumentCatalog catalog = document.getDocumentCatalog();
PDOptionalContentProperties ocprops = catalog.getOCProperties();
if (ocprops == null) {
ocprops = new PDOptionalContentProperties();
catalog.setOCProperties(ocprops);
}
PDOptionalContentGroup ocgLayer = null;
if (ocprops.hasGroup(ocgLayerName)) {
System.out.println("Using layer named: " + ocgLayerName);
ocgLayer = ocprops.getGroup(ocgLayerName);
System.out.println(ocgLayer);
} else {
throw new Exception("OCG named '" + ocgLayerName +
"' does not exist in template '" + templateFilename + "'");
}
PDResources resources = page.getResources();
if (resources == null) {
resources = new PDResources();
page.setResources(resources);
}
PDImageXObject image = getPdImage(imageFilename);
contentStream = new PDPageContentStream(
document,page,AppendMode.APPEND,false,true);
// We can finally add the image to the layer
contentStream.beginMarkedContent(COSName.OC,ocgLayer);
if (rotation != 180) {
contentStream.drawImage(image,adX,adY,adWidth,adHeight);
} else {
Matrix imageMatrix = Matrix.getRotateInstance(Math.toRadians(rotation),adHeight);
contentStream.transform(imageMatrix);
Integer imageTx = - adX;
Integer imageTy = - adY;
contentStream.drawImage(image,imageTx,imageTy,adHeight);
imageMatrix = Matrix.getRotateInstance(Math.toRadians(180),adHeight);
contentStream.transform(imageMatrix);
}
contentStream.endMarkedContent();
contentStream.close();
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)