假设我有两个type 的实例layer1和layer2com.itextpdf.text.pdf.PdfLayer
,其中layer1将用于com.itextpdf.text.pdf.PdfTemplate
包含 type 实例的 type 实例,com.itextpdf.text.Image
而layer2将用于type的3D 注释com.itextpdf.text.pdf.PdfAnnotation
。
问题:设置可见性并激活为layer2保留的区域后,我既不能使其(layer2)不可见,也不能显示 layer1。
基本代码:
private Document document;
private PdfWriter writer;
PdfContentByte upperLayer;
Rectangle box;
private PdfLayer view2D;
private PdfLayer view3D;
public static void main(String[] args) throws DocumentException, MalformedURLException, URISyntaxException, IOException
{
LayersWithDiffDimsDemo generator = new LayersWithDiffDimsDemo();
generator.setupResources();
generator.report();
generator.cleanupResources();
}
public void setupResources() throws DocumentException, IOException
{
document = new Document(PageSize.A4);
FileOutputStream outputStream = new FileOutputStream("layers_with_diff_dimensions.pdf");
writer = PdfWriter.getInstance(document, outputStream);
document.open();
upperLayer = writer.getDirectContent();
box = new Rectangle(26.0f, document.getPageSize().getHeight() - 455.0f, 550, document.getPageSize().getHeight() - 210.0f);
box.setBorder(Rectangle.BOX);
box.setBorderWidth(0.5f);
box.setBorderColor(new BaseColor(0xFF, 0x00, 0x00));
document.add(box);
PdfLayer views = PdfLayer.createTitle("Ansichtsmodi", writer);
view2D = new PdfLayer("2D", writer);
view2D.setOn(true);
view3D = new PdfLayer("3D", writer);
view3D.setOn(false);
views.addChild(view3D);
views.addChild(view2D);
ArrayList<PdfLayer> radio = new ArrayList<>();
radio.add(view3D);
radio.add(view2D);
writer.addOCGRadioGroup(radio);
}
public void report() throws URISyntaxException, MalformedURLException, IOException
{
try
{
// Output the dimension drawing
// teapot.jpg is a screen shot of the 3d model
Image imageIn2D = Image.getInstance("teapot.jpg");
imageIn2D.setAbsolutePosition((document.getPageSize().getWidth() - imageIn2D.getWidth()) / 2, 400.0f);
imageIn2D.setLayer(view2D);
upperLayer.addImage(imageIn2D, true);
try {
// The file name that contains the 3D Model
FileInputStream inputStream = new FileInputStream("teapot.u3d");
PdfStream stream3D = new PdfStream(inputStream, writer);
stream3D.put(PdfName.TYPE, new PdfName("3D"));
stream3D.put(PdfName.SUBTYPE, new PdfName("U3D"));
stream3D.flateCompress();
try {
PdfIndirectObject streamObject = writer.addToBody(stream3D);
stream3D.writeLength();
PdfDictionary dict3D = new PdfDictionary();
dict3D.put(PdfName.TYPE, new PdfName("3DView"));
dict3D.put(new PdfName("XN"), new PdfString("Default"));
dict3D.put(new PdfName("IN"), new PdfString("Unnamed"));
dict3D.put(new PdfName("MS"), PdfName.M);
dict3D.put(new PdfName("C2W"), new PdfArray(new float[]{1, 0, 0, 0, 0, -1, 0, 1, 0, 3, -235, 28}));
dict3D.put(PdfName.CO, new PdfNumber(235));
PdfIndirectObject dictObject = writer.addToBody(dict3D);
PdfAnnotation annot = new PdfAnnotation(writer, box);
annot.setLayer(view3D);
annot.put(PdfName.CONTENTS, new PdfString("3D Model"));
annot.put(PdfName.SUBTYPE, new PdfName("3D"));
annot.put(PdfName.TYPE, PdfName.ANNOT);
annot.put(new PdfName("3DD"), streamObject.getIndirectReference());
annot.put(new PdfName("3DV"), dictObject.getIndirectReference());
PdfAppearance ap = upperLayer.createAppearance(box.getWidth(), box.getHeight());
annot.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, ap);
annot.setPage();
upperLayer.addAnnotation(annot, false);
} catch (IOException ex) {
Logger.getLogger(LayersWithDiffDimsDemo.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(LayersWithDiffDimsDemo.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (DocumentException ex)
{
Logger.getLogger(LayersWithDiffDimsDemo.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void cleanupResources()
{
document.close();
}
感谢您的任何建议!