java,Itextpdf:更改3D形状在pdf导出页面中的位置

时间:2018-07-16 09:15:22

标签: java 3d itext

我使用Itextpdf库将3D形状导出为* .pdf。形状显示正确,但是问题在于它们没有出现在页面中间。我试图缩放页面的坐标,但这没有用。谁能帮我解决这个问题。我认为它与相机参数(PdfArray)有关,但是我没有找到有关参数的说明。 这是我使用的代码

public void createPdf(String filename) throws IOException, DocumentException {
    // step 1
    Document document = new Document();

    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
    // step 3
    document.open();
    // step 4
    Rectangle rect = new Rectangle(100, 400, 500, 800);
    rect.setBorder(Rectangle.BOX);
    rect.setBorderWidth(0.5f);
    document.add(rect);

    PdfStream stream3D = new PdfStream(new FileInputStream(RESOURCE), writer);
    stream3D.put(PdfName.TYPE, new PdfName("3D"));
    stream3D.put(PdfName.SUBTYPE, new PdfName("U3D"));
    stream3D.flateCompress();
    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, rect);
    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 = writer.getDirectContent().createAppearance(rect.getWidth(), rect.getHeight());
    annot.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, ap);
    annot.setPage();

    writer.addAnnotation(annot);
    // step 5
    document.close();
}

0 个答案:

没有答案