简体   繁体   English

在Java中将.obj文件转换为.pdf3D

[英]converting an .obj file into .pdf3D in java

I wish that somebody could help me solving this problem. 我希望有人能帮助我解决这个问题。 I want to convert an *.obj file into pdf3D using Java. 我想使用Java将* .obj文件转换为pdf3D。 I have tried using the library itextpdf (at the end you can find the code that I used), but I just managed to convert a u3D file to pdf. 我尝试使用itextpdf库(最后可以找到我使用的代码),但是我只是设法将u3D文件转换为pdf。 Do I have to change something in the code or should I use another library? 我需要更改代码中的某些内容还是应该使用其他库?

@SuppressWarnings("javadoc")
public class Main
{
public static String RESOURCE = "C:\\Users\\bb\\IdeaProjects/Zuccarello.U3D";
public static String RESULT="C:\\Users\\bb\\IdeaProjects/obj.pdf";

public static void main(String[] args) throws DocumentException, IOException {
    new Main().createPdf ( RESULT);
}

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);
    rect.setBorderColor(new BaseColor(0xFF, 0x00, 0x00));
    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();
}
}

3D PDF only supports .prc and .u3d files. 3D PDF仅支持.prc和.u3d文件。 Direct file conversion formats adobe.com So you need to convert one of those first. 直接文件转换格式adobe.com因此,您需要先转换其中一种。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM