假設我有兩個實例 layer1的和二層com.itextpdf.text.pdf.PdfLayer
型,與層1將用於com.itextpdf.text.pdf.PdfTemplate
類型的實例,其中包含com.itextpdf.text.Image
類型和二層的實例將用於類型爲com.itextpdf.text.pdf.PdfAnnotation
的3D註釋。3D註釋和PdfLayer
問題:設置可見性和激活用於二層保留的區域後,我既不能使它(二層)unvisible也不顯示層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();
}
謝謝你的任何建議!
請分享您的代碼 –
嗨Alexey,謝謝您的貢獻。我在這個問題背後插入了基本代碼。 –