add default rendermode & light

This commit is contained in:
Zhuangkh 2023-05-12 15:37:43 +08:00
parent c07a80fe2f
commit 3dcbe4a0eb

View File

@ -6,6 +6,7 @@ using iText.Kernel.Pdf.Canvas;
using iText.Layout; using iText.Layout;
using iText.Layout.Element; using iText.Layout.Element;
using Newtonsoft.Json; using Newtonsoft.Json;
using Org.BouncyCastle.Utilities.IO;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
@ -36,19 +37,15 @@ public class Program
public static void GeneratePDF(string idtf, string view, string pdf) public static void GeneratePDF(string idtf, string view, string pdf)
{ {
var u3d = System.IO.Path.GetTempFileName(); var u3d = System.IO.Path.GetRandomFileName();
GenerateU3D(idtf, u3d); GenerateU3D(idtf, u3d);
var writer = new PdfWriter(pdf); var writer = new PdfWriter(pdf);
PdfDocument pdfDoc = new PdfDocument(writer); PdfDocument pdfDoc = new PdfDocument(writer);
var size = PageSize.A4; var size = PageSize.A4;
Document doc = new Document(pdfDoc); Document doc = new Document(pdfDoc);
Rectangle rect = new Rectangle(50, 50, size.GetWidth() - 100, size.GetHeight() - 100);
var views = Generate3DView(view); var views = Generate3DView(view);
PdfStream stream3D = new PdfStream(pdfDoc, new FileStream(u3d, FileMode.Open)); PdfStream stream3D = new PdfStream(pdfDoc, new FileStream(u3d, FileMode.Open));
stream3D.Put(PdfName.Type, new PdfName("3D")); stream3D.Put(PdfName.Type, new PdfName("3D"));
stream3D.Put(PdfName.Subtype, new PdfName("U3D")); stream3D.Put(PdfName.Subtype, new PdfName("U3D"));
@ -56,15 +53,14 @@ public class Program
stream3D.SetCompressionLevel(CompressionConstants.DEFAULT_COMPRESSION); stream3D.SetCompressionLevel(CompressionConstants.DEFAULT_COMPRESSION);
stream3D.Flush(); stream3D.Flush();
Rectangle rect = new Rectangle(50, 50, size.GetWidth() - 100, size.GetHeight() - 100);
Pdf3DAnnotation annot = new Pdf3DAnnotation(rect, stream3D); Pdf3DAnnotation annot = new Pdf3DAnnotation(rect, stream3D);
annot.SetContents(new PdfString("3D Model")); annot.SetContents(new PdfString("3D Model"));
annot.SetDefaultInitialView(views.First()); annot.Put(PdfName._3DV, PdfName.L);//default view
var page = pdfDoc.AddNewPage(size).SetRotation(90); var page = pdfDoc.AddNewPage(size).SetRotation(90);
page.AddAnnotation(annot); page.AddAnnotation(annot);
// PdfCanvas canvas = new PdfCanvas(page);
//logo
ImageData imageData = ImageDataFactory.Create(System.IO.Directory.GetParent(Assembly.GetExecutingAssembly().Location) + "\\logo.png"); ImageData imageData = ImageDataFactory.Create(System.IO.Directory.GetParent(Assembly.GetExecutingAssembly().Location) + "\\logo.png");
Image image = new Image(imageData); Image image = new Image(imageData);
image.SetHeight(25); image.SetHeight(25);
@ -91,24 +87,40 @@ public class Program
PdfDictionary dict3D = new PdfDictionary(); PdfDictionary dict3D = new PdfDictionary();
dict3D.Put(PdfName.Type, new PdfName("3DView")); dict3D.Put(PdfName.Type, new PdfName("3DView"));
dict3D.Put(new PdfName("XN"), new PdfString(item.Name)); dict3D.Put(new PdfName("XN"), new PdfString(item.Name));
dict3D.Put(new PdfName("IN"), new PdfString(item.Name));
//dict3D.Put(new PdfName("IN"), new PdfString("Unnamed")); dict3D.Put(new PdfName("RM"), RenderMode());
dict3D.Put(new PdfName("LS"), RenderLight());
dict3D.Put(new PdfName("MS"), PdfName.M); dict3D.Put(new PdfName("MS"), PdfName.M);
//dict3D.Put(new PdfName("C2W"), dict3D.Put(new PdfName("C2W"),
// new PdfArray(new double[] { item.Parents[0].Transform.c0r0, item.Parents[0].Transform.c0r1, item.Parents[0].Transform.c0r2, new PdfArray(new double[] { item.Parents[0].Transform.c0r0, item.Parents[0].Transform.c0r1, item.Parents[0].Transform.c0r2,
// item.Parents[0].Transform.c1r0, item.Parents[0].Transform.c1r1, item.Parents[0].Transform.c1r2, item.Parents[0].Transform.c1r0, item.Parents[0].Transform.c1r1, item.Parents[0].Transform.c1r2,
// item.Parents[0].Transform.c2r0, item.Parents[0].Transform.c2r1, item.Parents[0].Transform.c2r2, item.Parents[0].Transform.c2r0, item.Parents[0].Transform.c2r1, item.Parents[0].Transform.c2r2,
// item.Parents[0].Transform.c3r0, item.Parents[0].Transform.c3r1, item.Parents[0].Transform.c3r2 })); item.Parents[0].Transform.c3r0, item.Parents[0].Transform.c3r1, item.Parents[0].Transform.c3r2 }));
dict3D.Put(new PdfName("C2W"), dict3D.Put(new PdfName("C2W"),
new PdfArray(new double[] { item.Parents[0].Transform.c0r0, item.Parents[0].Transform.c1r0, item.Parents[0].Transform.c2r0, new PdfArray(new double[] { item.Parents[0].Transform.c0r0, item.Parents[0].Transform.c1r0, item.Parents[0].Transform.c2r0,
item.Parents[0].Transform.c0r1, item.Parents[0].Transform.c1r1, item.Parents[0].Transform.c2r1, item.Parents[0].Transform.c0r1, item.Parents[0].Transform.c1r1, item.Parents[0].Transform.c2r1,
item.Parents[0].Transform.c0r2, item.Parents[0].Transform.c1r2, item.Parents[0].Transform.c2r2, item.Parents[0].Transform.c0r2, item.Parents[0].Transform.c1r2, item.Parents[0].Transform.c2r2,
item.Parents[0].Transform.c3r0, item.Parents[0].Transform.c3r1, item.Parents[0].Transform.c3r2 })); item.Parents[0].Transform.c3r0, item.Parents[0].Transform.c3r1, item.Parents[0].Transform.c3r2 }));
//dict3D.Put(PdfName.CO, new PdfNumber(235)); dict3D.Flush();
list.Add(dict3D); list.Add(dict3D);
} }
} }
return list; return list;
} }
private static PdfDictionary RenderMode()
{
PdfDictionary dict3D = new PdfDictionary();
dict3D.Put(PdfName.Type, new PdfName("3DRenderMode"));
dict3D.Put(PdfName.Subtype, new PdfName("SolidOutline"));
return dict3D;
}
private static PdfDictionary RenderLight()
{
PdfDictionary dict3D = new PdfDictionary();
dict3D.Put(PdfName.Type, new PdfName("3DLightingScheme"));
dict3D.Put(PdfName.Subtype, new PdfName("Day"));
return dict3D;
}
} }