gpt4 book ai didi

java - 在java中将.obj文件转换为.pdf3D

转载 作者:行者123 更新时间:2023-12-02 02:03:59 29 4
gpt4 key购买 nike

我希望有人能帮我解决这个问题。我想使用 Java 将 *.obj 文件转换为 pdf3D。我尝试过使用库 itextpdf (最后你可以找到我使用的代码),但我只是设法将 u3D 文件转换为 pdf。我是否必须更改代码中的某些内容或者应该使用其他库?

@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 仅支持 .prc 和 .u3d 文件。 Direct file conversion formats adobe.com因此,您需要先转换其中一个。

关于java - 在java中将.obj文件转换为.pdf3D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51097204/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com