3D注释和PdfLayer

时间:2016-04-24 18:47:51

标签: itext layer pdf-annotations

假设我有两个类型为com.itextpdf.text.pdf.PdfLayer layer1 layer2 layer1 将用于实例包含类型com.itextpdf.text.pdf.PdfTemplate layer2 的实例的类型com.itextpdf.text.Image将用于com.itextpdf.text.pdf.PdfAnnotation类型的 3D注释

问题:设置可见性并激活为 layer2 保留的区域后,我既不能使它( layer2 )不可见也不能显示层1。

基本代码:

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();
}

感谢您的任何建议!

1 个答案:

答案 0 :(得分:1)

假设您的查看器是Acrobat或Reader,只要3D注释处于活动状态,它就会可见。 annot与图层(可见或其他)相关联的事实不会改变它。

捍卫我的断言......

http://practicalpdf.com/downloads/BrakeAssembly.pdf打开文件。

该文件有一个3D Annotation占用整个页面,并设置为在单击时激活。首次打开文件时,它将被停用。 3D annot也位于名为“3DAnnot”的图层上,文件初始视图设置为在Reader中打开,并显示图层面板。 PDF规范支持将注释与图层相关联,但是如果没有插件或通过库,就无法在Acrobat中创建注释。

请注意,首次打开文件时,3D注释未处于活动状态,您可以切换图层可见性。请注意,注释海报可见性遵循图层可见性。现在点击它来激活annot。现在请注意,已激活 3D Annot不受图层可见性的影响。

这是因为图层可见性仅适用于PDF页面内容。 3D annot的海报(外观)是该词典中唯一的页面内容部分。 3D查看器运行时只是投射到与annot相同的矩形中。在激活时,即使您在保存3D注释的图层之上添加图层,3D也将始终显示在页面内容上。