U3D

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

1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using System.Threading.Tasks;
8using Windows.Foundation;
9
10using pdftron.Filters;
11using pdftron.PDF;
12using pdftron.SDF;
13
14using PDFNetUniversalSamples.ViewModels;
15
16namespace PDFNetSamples
17{
18 public sealed class U3DTest : Sample
19 {
20 public U3DTest() :
21 base("U3D", "This example illustrates how to embed U3D content (3 dimensional models) in PDF.")
22 {
23 }
24
25 public override IAsyncAction RunAsync()
26 {
27 return Task.Run(new System.Action(async () => {
28 WriteLine("--------------------------------");
29 WriteLine("Starting U3D Test...");
30 WriteLine("--------------------------------\n");
31 try
32 {
33 PDFDoc doc = new PDFDoc();
34 pdftron.PDF.Page page = doc.PageCreate();
35 doc.PagePushBack(page);
36 Obj annots = doc.CreateIndirectArray();
37 page.GetSDFObj().Put("Annots", annots);
38
39 Create3DAnnotation(doc, annots);
40 String output_file_path = Path.Combine(OutputPath, "dice_u3d.pdf");
41 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
42 WriteLine("Done. Results saved in " + output_file_path);
43 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
44 }
45 catch (Exception e)
46 {
47 WriteLine(GetExceptionMessage(e));
48 }
49
50 WriteLine("\n--------------------------------");
51 WriteLine("Done U3D Test.");
52 WriteLine("--------------------------------\n");
53 })).AsAsyncAction();
54 }
55
56 void Create3DAnnotation(PDFDoc doc, Obj annots)
57 {
58 // ---------------------------------------------------------------------------------
59 // Create a 3D annotation based on U3D content. PDF 1.6 introduces the capability
60 // for collections of three-dimensional objects, such as those used by CAD software,
61 // to be embedded in PDF files.
62 Obj link_3D = doc.CreateIndirectDict();
63 link_3D.PutName("Subtype", "3D");
64
65 // Annotation location on the page
66 pdftron.PDF.Rect bbox = new pdftron.PDF.Rect(25, 180, 585, 643);
67 link_3D.PutRect("Rect", bbox.x1, bbox.y1, bbox.x2, bbox.y2);
68 annots.PushBack(link_3D);
69
70 // The 3DA entry is an activation dictionary (see Table 9.34 in the PDF Reference Manual)
71 // that determines how the state of the annotation and its associated artwork can change.
72 Obj activation_dict_3D = link_3D.PutDict("3DA");
73
74 // Set the annotation so that it is activated as soon as the page containing the
75 // annotation is opened. Other options are: PV (page view) and XA (explicit) activation.
76 activation_dict_3D.PutName("A", "PO");
77
78 // Embed U3D Streams (3D Model/Artwork).
79 MappedFile u3d_file = new MappedFile(Path.Combine(InputPath, "dice.u3d"));
80 FilterReader u3d_reader = new FilterReader(u3d_file);
81
82 Obj u3d_data_dict = doc.CreateIndirectStream(u3d_reader);
83 u3d_data_dict.PutName("Subtype", "U3D");
84 link_3D.Put("3DD", u3d_data_dict);
85
86 // Set the initial view of the 3D artwork that should be used when the annotation is activated.
87 Obj view3D_dict = link_3D.PutDict("3DV");
88 view3D_dict.PutString("IN", "Unnamed");
89 view3D_dict.PutString("XN", "Default");
90 view3D_dict.PutName("MS", "M");
91 view3D_dict.PutNumber("CO", 27.5);
92
93 // A 12-element 3D transformation matrix that specifies a position and orientation
94 // of the camera in world coordinates.
95 Obj tr3d = view3D_dict.PutArray("C2W");
96 tr3d.PushBackNumber(1); tr3d.PushBackNumber(0); tr3d.PushBackNumber(0);
97 tr3d.PushBackNumber(0); tr3d.PushBackNumber(0); tr3d.PushBackNumber(-1);
98 tr3d.PushBackNumber(0); tr3d.PushBackNumber(1); tr3d.PushBackNumber(0);
99 tr3d.PushBackNumber(0); tr3d.PushBackNumber(-27.5); tr3d.PushBackNumber(0);
100
101 // Create annotation appearance stream, a thumbnail which is used during printing or
102 // in PDF processors that do not understand 3D data.
103 Obj ap_dict = link_3D.PutDict("AP");
104 ElementBuilder builder = new ElementBuilder();
105 ElementWriter writer = new ElementWriter();
106 writer.Begin(doc.GetSDFDoc());
107
108 writer.WritePlacedElement(builder.CreateImage(
109 pdftron.PDF.Image.Create(doc.GetSDFDoc(), Path.Combine(InputPath, "dice.jpg")),
110 0, 0, bbox.Width(), bbox.Height()));
111
112 Obj normal_ap_stream = writer.End();
113 normal_ap_stream.PutName("Subtype", "Form");
114 normal_ap_stream.PutRect("BBox", 0, 0, bbox.Width(), bbox.Height());
115 ap_dict.Put("N", normal_ap_stream);
116
117 }
118 }
119}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales