问题描述
我正在使用iText7(C#)创建pdf.我需要在现有的pdf文件中添加u3d图片.我可以找到示例( http: //developers.itextpdf.com/examples/itext-action-second-edition/chapter-16#619-pdf3d.java ),但它是Java.谁能帮我在.net C#上给我一个例子?
I am using iText7 (C#) to create the pdf. I need to add a u3d picture to the exising pdf. I can find the example (http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-16#619-pdf3d.java) but it is java one. Can anyone help to give me an example on .net C#?
推荐答案
链接的示例适用于iText5,而不适用于iText7.在iText7中,该示例如下所示:
The Linked example is for iText5, not iText7.In iText7 this example would look like this
public static final String DEST = "./target/test/resources/book/part4/chapter16/Listing_16_16_Pdf3D.pdf";
public static String RESOURCE = "./src/test/resources/img/teapot.u3d";
public static void main(String args[]) throws Exception {
new Listing_16_16_Pdf3D().manipulatePdf(DEST);
}
public void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
Rectangle rect = new Rectangle(100, 400, 400, 400);
PdfStream stream3D = new PdfStream(pdfDoc, new FileInputStream(RESOURCE));
stream3D.put(PdfName.Type, new PdfName("3D"));
stream3D.put(PdfName.Subtype, new PdfName("U3D"));
stream3D.setCompressionLevel(CompressionConstants.DEFAULT_COMPRESSION);
stream3D.flush();
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));
Pdf3DAnnotation annot = new Pdf3DAnnotation(rect, stream3D);
annot.setContents(new PdfString("3D Model"));
annot.setDefaultInitialView(dict3D);
pdfDoc.addNewPage().addAnnotation(annot);
doc.close();
}
或者,如果您希望在C#中使用它(还没有在本地运行它,但是Visual Studio并没有抱怨语法)
Or, if you want it in C# (haven't ran it locally, but visual studio is not complaining about syntax)
public void manipulatePdf(String dest) {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
Rectangle rect = new Rectangle(100, 400, 400, 400);
PdfStream stream3D = new PdfStream(pdfDoc, new FileInputStream(RESOURCE));
stream3D.Put(PdfName.Type, new PdfName("3D"));
stream3D.Put(PdfName.Subtype, new PdfName("U3D"));
stream3D.SetCompressionLevel(CompressionConstants.DEFAULT_COMPRESSION);
stream3D.Flush();
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));
Pdf3DAnnotation annot = new Pdf3DAnnotation(rect, stream3D);
annot.SetContents(new PdfString("3D Model"));
annot.SetDefaultInitialView(dict3D);
pdfDoc.AddNewPage().AddAnnotation(annot);
doc.Close();
}
这篇关于如何使用带有C#的itext7将u3d添加到现有的pdf中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!