PDF3D/PDFGenerator/Program.cs

122 lines
4.8 KiB
C#
Raw Normal View History

2023-04-24 16:20:24 +08:00
using iText.IO.Image;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Annot;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Element;
2023-05-11 17:06:50 +08:00
using Newtonsoft.Json;
2023-05-12 15:37:43 +08:00
using Org.BouncyCastle.Utilities.IO;
2023-04-24 16:20:24 +08:00
using System;
2023-05-11 17:06:50 +08:00
using System.Collections.Generic;
2023-04-24 16:20:24 +08:00
using System.Diagnostics;
using System.IO;
2023-05-11 17:06:50 +08:00
using System.Linq;
2023-04-24 16:20:24 +08:00
using System.Reflection;
public class Program
{
public static string IDTFTool = Directory.GetParent(Assembly.GetExecutingAssembly().Location) + @"\\IDTFTool\\IDTFConverter.exe";
static void Main(string[] args)
{
2023-05-11 17:06:50 +08:00
GeneratePDF(args[0], args[1], args[2]);
2023-04-24 16:20:24 +08:00
}
2023-05-11 15:06:02 +08:00
public static void GenerateU3D(string idtf, string u3d)
2023-04-24 16:20:24 +08:00
{
System.Diagnostics.Process exep = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = IDTFTool;
2023-05-11 15:06:02 +08:00
startInfo.Arguments = $"-input \"{idtf}\" -output \"{u3d}\"";
2023-04-24 16:20:24 +08:00
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
exep.StartInfo = startInfo;
exep.Start();
exep.WaitForExit();
}
2023-05-11 17:06:50 +08:00
public static void GeneratePDF(string idtf, string view, string pdf)
2023-04-24 16:20:24 +08:00
{
2023-05-12 17:29:37 +08:00
var u3d = System.IO.Path.GetTempPath() + System.IO.Path.GetRandomFileName();
2023-05-11 15:06:02 +08:00
GenerateU3D(idtf, u3d);
2023-04-24 16:20:24 +08:00
var writer = new PdfWriter(pdf);
PdfDocument pdfDoc = new PdfDocument(writer);
var size = PageSize.A4;
Document doc = new Document(pdfDoc);
2023-05-11 17:06:50 +08:00
var views = Generate3DView(view);
2023-05-11 15:06:02 +08:00
PdfStream stream3D = new PdfStream(pdfDoc, new FileStream(u3d, FileMode.Open));
stream3D.Put(PdfName.Type, new PdfName("3D"));
stream3D.Put(PdfName.Subtype, new PdfName("U3D"));
2023-05-11 17:06:50 +08:00
stream3D.Put(new PdfName("VA"), new PdfArray(views.ToArray()));
2023-05-11 15:06:02 +08:00
stream3D.SetCompressionLevel(CompressionConstants.DEFAULT_COMPRESSION);
stream3D.Flush();
2023-05-11 17:06:50 +08:00
2023-05-12 15:37:43 +08:00
Rectangle rect = new Rectangle(50, 50, size.GetWidth() - 100, size.GetHeight() - 100);
2023-04-24 16:20:24 +08:00
Pdf3DAnnotation annot = new Pdf3DAnnotation(rect, stream3D);
2023-05-11 17:06:50 +08:00
annot.SetContents(new PdfString("3D Model"));
2023-05-12 15:37:43 +08:00
annot.Put(PdfName._3DV, PdfName.L);//default view
2023-04-24 16:20:24 +08:00
var page = pdfDoc.AddNewPage(size).SetRotation(90);
page.AddAnnotation(annot);
2023-05-12 15:37:43 +08:00
//logo
2023-04-24 16:20:24 +08:00
ImageData imageData = ImageDataFactory.Create(System.IO.Directory.GetParent(Assembly.GetExecutingAssembly().Location) + "\\logo.png");
Image image = new Image(imageData);
image.SetHeight(25);
image.SetMarginLeft(-20);
image.SetMarginTop(-20);
2023-05-11 17:06:50 +08:00
image.SetRotationAngle(Math.PI / 2);
2023-04-24 16:20:24 +08:00
doc.Add(image);
// 关闭文档
pdfDoc.Close();
doc.Close();
}
2023-05-11 17:06:50 +08:00
private static List<PdfDictionary> Generate3DView(string view)
{
List<PdfDictionary> list = new List<PdfDictionary>();
string viewStr = File.ReadAllText(view);
var views = JsonConvert.DeserializeObject<List<IDTF.Net.View>>(viewStr);
foreach (var item in views)
{
if (item.Parents.Any())
{
PdfDictionary dict3D = new PdfDictionary();
dict3D.Put(PdfName.Type, new PdfName("3DView"));
dict3D.Put(new PdfName("XN"), new PdfString(item.Name));
2023-05-12 15:37:43 +08:00
dict3D.Put(new PdfName("IN"), new PdfString(item.Name));
dict3D.Put(new PdfName("RM"), RenderMode());
dict3D.Put(new PdfName("LS"), RenderLight());
2023-05-11 17:06:50 +08:00
dict3D.Put(new PdfName("MS"), PdfName.M);
2023-05-12 15:37:43 +08:00
dict3D.Put(new PdfName("C2W"),
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.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 }));
dict3D.Flush();
2023-05-11 17:06:50 +08:00
list.Add(dict3D);
}
}
return list;
}
2023-05-12 15:37:43 +08:00
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;
}
2023-04-24 16:20:24 +08:00
}