1

假设我有两个type 的实例layer1layer2com.itextpdf.text.pdf.PdfLayer,其中layer1将用于com.itextpdf.text.pdf.PdfTemplate包含 type 实例的 type 实例,com.itextpdf.text.Imagelayer2将用于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();
}

感谢您的任何建议!

4

1 回答 1

1

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

捍卫我的主张...

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

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

请注意,当您第一次打开文件时,3D 注释未激活,您可以切换图层可见性。请注意,注释海报可见性遵循图层可见性。现在通过单击激活注释。现在请注意,激活的3D Annot 不受图层可见性的影响。

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

于 2016-04-26T20:28:57.533 回答