U3D

Sample C# code for using Apryse SDK to embed U3D content (3 dimensional models) in PDF files. Learn more about our Server SDK.

1//
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3//
4
5using System;
6using pdftron;
7using pdftron.Common;
8using pdftron.Filters;
9using pdftron.SDF;
10using pdftron.PDF;
11
12namespace U3DTestCS
13{
14 /// <summary>
15 /// This example illustrates how to embed U3D content in PDF.
16 /// Some parameters used to center 3D model are assumed.
17 /// </summary>
18 class Class1
19 {
20 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
21 static Class1() {}
22
23 // Relative path to the folder containing test files.
24 const string input_path = "../../../../TestFiles/";
25 const string output_path = "../../../../TestFiles/Output/";
26
27 static void Main(string[] args)
28 {
29 PDFNet.Initialize(PDFTronLicense.Key);
30
31 try
32 {
33 using (PDFDoc doc = new PDFDoc())
34 {
35 Page page = doc.PageCreate();
36 doc.PagePushBack(page);
37 Obj annots = doc.CreateIndirectArray();
38 page.GetSDFObj().Put("Annots", annots);
39
40 Create3DAnnotation(doc, annots);
41 doc.Save(output_path + "dice_u3d.pdf", SDFDoc.SaveOptions.e_linearized);
42 }
43 Console.WriteLine("Done");
44 }
45 catch (PDFNetException e)
46 {
47 Console.WriteLine(e.Message);
48 }
49 PDFNet.Terminate();
50 }
51
52 static void Create3DAnnotation(PDFDoc doc, Obj annots)
53 {
54 // ---------------------------------------------------------------------------------
55 // Create a 3D annotation based on U3D content. PDF 1.6 introduces the capability
56 // for collections of three-dimensional objects, such as those used by CAD software,
57 // to be embedded in PDF files.
58 Obj link_3D = doc.CreateIndirectDict();
59 link_3D.PutName("Subtype", "3D");
60
61 // Annotation location on the page
62 Rect bbox = new Rect(25, 180, 585, 643);
63 link_3D.PutRect("Rect", bbox.x1, bbox.y1, bbox.x2, bbox.y2);
64 annots.PushBack(link_3D);
65
66 // The 3DA entry is an activation dictionary (see Table 9.34 in the PDF Reference Manual)
67 // that determines how the state of the annotation and its associated artwork can change.
68 Obj activation_dict_3D = link_3D.PutDict("3DA");
69
70 // Set the annotation so that it is activated as soon as the page containing the
71 // annotation is opened. Other options are: PV (page view) and XA (explicit) activation.
72 activation_dict_3D.PutName("A", "PO");
73
74 // Embed U3D Streams (3D Model/Artwork).
75 MappedFile u3d_file = new MappedFile(input_path + "dice.u3d");
76 FilterReader u3d_reader = new FilterReader(u3d_file);
77
78 Obj u3d_data_dict = doc.CreateIndirectStream(u3d_reader);
79 u3d_data_dict.PutName("Subtype", "U3D");
80 link_3D.Put("3DD", u3d_data_dict);
81
82 // Set the initial view of the 3D artwork that should be used when the annotation is activated.
83 Obj view3D_dict = link_3D.PutDict("3DV");
84 view3D_dict.PutString("IN", "Unnamed");
85 view3D_dict.PutString("XN", "Default");
86 view3D_dict.PutName("MS", "M");
87 view3D_dict.PutNumber("CO", 27.5);
88
89 // A 12-element 3D transformation matrix that specifies a position and orientation
90 // of the camera in world coordinates.
91 Obj tr3d = view3D_dict.PutArray("C2W");
92 tr3d.PushBackNumber(1); tr3d.PushBackNumber(0); tr3d.PushBackNumber(0);
93 tr3d.PushBackNumber(0); tr3d.PushBackNumber(0); tr3d.PushBackNumber(-1);
94 tr3d.PushBackNumber(0); tr3d.PushBackNumber(1); tr3d.PushBackNumber(0);
95 tr3d.PushBackNumber(0); tr3d.PushBackNumber(-27.5); tr3d.PushBackNumber(0);
96
97 // Create annotation appearance stream, a thumbnail which is used during printing or
98 // in PDF processors that do not understand 3D data.
99 Obj ap_dict = link_3D.PutDict("AP");
100 ElementBuilder builder = new ElementBuilder();
101 ElementWriter writer = new ElementWriter();
102 writer.Begin(doc);
103
104 writer.WritePlacedElement(builder.CreateImage(
105 Image.Create(doc, input_path + "dice.jpg"),
106 0, 0, bbox.Width(), bbox.Height()));
107
108 Obj normal_ap_stream = writer.End();
109 normal_ap_stream.PutName("Subtype", "Form");
110 normal_ap_stream.PutRect("BBox", 0, 0, bbox.Width(), bbox.Height());
111 ap_dict.Put("N", normal_ap_stream);
112 }
113 }
114}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales