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}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#include <PDF/PDFNet.h>
7#include <PDF/PDFDoc.h>
8#include <PDF/ElementWriter.h>
9#include <PDF/ElementBuilder.h>
10#include <SDF/Obj.h>
11#include <Filters/MappedFile.h>
12#include <Filters/FilterReader.h>
13#include <Filters/FlateEncode.h>
14#include <iostream>
15#include "../../LicenseKey/CPP/LicenseKey.h"
16
17using namespace pdftron;
18using namespace Filters;
19using namespace SDF;
20using namespace PDF;
21using namespace std;
22
23std::string input_path = "../../TestFiles/";
24
25void Create3DAnnotation(PDFDoc& doc, Obj annots)
26{
27 // ---------------------------------------------------------------------------------
28 // Create a 3D annotation based on U3D content. PDF 1.6 introduces the capability
29 // for collections of three-dimensional objects, such as those used by CAD software,
30 // to be embedded in PDF files.
31 Obj link_3D = doc.CreateIndirectDict();
32 link_3D.PutName("Subtype", "3D");
33
34 // Annotation location on the page
35 Rect link_3D_rect(25, 180, 585, 643);
36 link_3D.PutRect("Rect", link_3D_rect.x1, link_3D_rect.y1,
37 link_3D_rect.x2, link_3D_rect.y2);
38 annots.PushBack(link_3D);
39
40 // The 3DA entry is an activation dictionary (see Table 9.34 in the PDF Reference Manual)
41 // that determines how the state of the annotation and its associated artwork can change.
42 Obj activation_dict_3D = link_3D.PutDict("3DA");
43
44 // Set the annotation so that it is activated as soon as the page containing the
45 // annotation is opened. Other options are: PV (page view) and XA (explicit) activation.
46 activation_dict_3D.PutName("A", "PO");
47
48 // Embed U3D Streams (3D Model/Artwork).
49 {
50 MappedFile u3d_file((input_path + "dice.u3d"));
51 FilterReader u3d_reader(u3d_file);
52
53 // To embed 3D stream without compression, you can omit the second parameter in CreateIndirectStream.
54 Obj u3d_data_dict = doc.CreateIndirectStream(u3d_reader, FlateEncode(Filter()));
55 u3d_data_dict.PutName("Subtype", "U3D");
56 link_3D.Put("3DD", u3d_data_dict);
57 }
58
59 // Set the initial view of the 3D artwork that should be used when the annotation is activated.
60 Obj view3D_dict = link_3D.PutDict("3DV");
61 {
62 view3D_dict.PutString("IN", "Unnamed");
63 view3D_dict.PutString("XN", "Default");
64 view3D_dict.PutName("MS", "M");
65 view3D_dict.PutNumber("CO", 27.5);
66
67 // A 12-element 3D transformation matrix that specifies a position and orientation
68 // of the camera in world coordinates.
69 Obj tr3d = view3D_dict.PutArray("C2W");
70 tr3d.PushBackNumber(1); tr3d.PushBackNumber(0); tr3d.PushBackNumber(0);
71 tr3d.PushBackNumber(0); tr3d.PushBackNumber(0); tr3d.PushBackNumber(-1);
72 tr3d.PushBackNumber(0); tr3d.PushBackNumber(1); tr3d.PushBackNumber(0);
73 tr3d.PushBackNumber(0); tr3d.PushBackNumber(-27.5); tr3d.PushBackNumber(0);
74
75 }
76
77 // Create annotation appearance stream, a thumbnail which is used during printing or
78 // in PDF processors that do not understand 3D data.
79 Obj ap_dict = link_3D.PutDict("AP");
80 {
81 ElementBuilder builder;
82 ElementWriter writer;
83 writer.Begin(doc);
84
85 std::string thumb_pathname(input_path + "dice.jpg");
86 Image image = Image::Create(doc, thumb_pathname.c_str());
87 writer.WritePlacedElement(builder.CreateImage(image, 0.0, 0.0, link_3D_rect.Width(), link_3D_rect.Height()));
88
89 Obj normal_ap_stream = writer.End();
90 normal_ap_stream.PutName("Subtype", "Form");
91 normal_ap_stream.PutRect("BBox", 0, 0, link_3D_rect.Width(), link_3D_rect.Height());
92 ap_dict.Put("N", normal_ap_stream);
93 }
94}
95
96int main(int argc, char *argv[])
97{
98 int ret = 0;
99 PDFNet::Initialize(LicenseKey);
100
101 string output_path = "../../TestFiles/Output/";
102
103 try
104 {
105 PDFDoc doc;
106 Page page = doc.PageCreate();
107 doc.PagePushBack(page);
108 Obj annots = doc.CreateIndirectArray();
109 page.GetSDFObj().Put("Annots", annots);
110
111 Create3DAnnotation(doc, annots);
112 doc.Save((output_path + "dice_u3d.pdf").c_str(), SDFDoc::e_linearized, 0);
113 cout << "Done" << endl;
114 }
115 catch(Common::Exception& e)
116 {
117 cout << e << endl;
118 ret = 1;
119 }
120 catch(...)
121 {
122 cout << "Unknown Exception" << endl;
123 ret = 1;
124 }
125
126 PDFNet::Terminate();
127 return ret;
128}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 . "pdftron"
10)
11
12import "pdftron/Samples/LicenseKey/GO"
13
14// Relative path to the folder containing the test files.
15var inputPath = "../../TestFiles/"
16var outputPath = "../../TestFiles/Output/"
17
18func Create3DAnnotation(doc PDFDoc, annots Obj){
19 // ---------------------------------------------------------------------------------
20 // Create a 3D annotation based on U3D content. PDF 1.6 introduces the capability
21 // for collections of three-dimensional objects, such as those used by CAD software,
22 // to be embedded in PDF files.
23 link3D := doc.CreateIndirectDict()
24 link3D.PutName("Subtype", "3D")
25
26 // Annotation location on the page
27 link3DRect := NewRect(25.0, 180.0, 585.0, 643.0)
28 link3D.PutRect("Rect", link3DRect.GetX1(), link3DRect.GetY1(),
29 link3DRect.GetX2(), link3DRect.GetY2())
30 annots.PushBack(link3D)
31
32 // The 3DA entry is an activation dictionary (see Table 9.34 in the PDF Reference Manual)
33 // that determines how the state of the annotation and its associated artwork can change.
34 activationDict3D := link3D.PutDict("3DA")
35
36 // Set the annotation so that it is activated as soon as the page containing the
37 // annotation is opened. Other options are: PV (page view) and XA (explicit) activation.
38 activationDict3D.PutName("A", "PO")
39
40 // Embed U3D Streams (3D Model/Artwork).
41 u3dFile := NewMappedFile(inputPath + "dice.u3d")
42 u3dReader := NewFilterReader(u3dFile)
43 u3dDataDict := doc.CreateIndirectStream(u3dReader, NewFilter())
44 u3dDataDict.PutName("Subtype", "U3D")
45 link3D.Put("3DD", u3dDataDict)
46
47 // Set the initial view of the 3D artwork that should be used when the annotation is activated.
48 view3DDict := link3D.PutDict("3DV")
49
50 view3DDict.PutString("IN", "Unnamed")
51 view3DDict.PutString("XN", "Default")
52 view3DDict.PutName("MS", "M")
53 view3DDict.PutNumber("CO", 27.5)
54
55 // A 12-element 3D transformation matrix that specifies a position and orientation
56 // of the camera in world coordinates.
57 tr3d := view3DDict.PutArray("C2W")
58 tr3d.PushBackNumber(1.0)
59 tr3d.PushBackNumber(0.0)
60 tr3d.PushBackNumber(0.0)
61 tr3d.PushBackNumber(0.0)
62 tr3d.PushBackNumber(0.0)
63 tr3d.PushBackNumber(-1.0)
64 tr3d.PushBackNumber(0.0)
65 tr3d.PushBackNumber(1.0)
66 tr3d.PushBackNumber(0.0)
67 tr3d.PushBackNumber(0.0)
68 tr3d.PushBackNumber(-27.5)
69 tr3d.PushBackNumber(0.0)
70
71 // Create annotation appearance stream, a thumbnail which is used during printing or
72 // in PDF processors that do not understand 3D data.
73 apDict := link3D.PutDict("AP")
74
75 builder := NewElementBuilder()
76 writer := NewElementWriter()
77 writer.Begin(doc.GetSDFDoc())
78
79 thumbPathname := inputPath + "dice.jpg"
80 image := ImageCreate(doc.GetSDFDoc(), thumbPathname)
81 writer.WritePlacedElement(builder.CreateImage(image, 0.0, 0.0, float64(link3DRect.Width()), float64(link3DRect.Height())))
82
83 normalApStream := writer.End()
84 normalApStream.PutName("Subtype", "Form")
85 normalApStream.PutRect("BBox", 0.0, 0.0, float64(link3DRect.Width()), float64(link3DRect.Height()))
86 apDict.Put("N", normalApStream)
87}
88
89func main(){
90 PDFNetInitialize(PDFTronLicense.Key)
91
92 doc := NewPDFDoc()
93 page := doc.PageCreate()
94 doc.PagePushBack(page)
95 annots := doc.CreateIndirectArray()
96 page.GetSDFObj().Put("Annots", annots)
97
98 Create3DAnnotation(doc, annots)
99 doc.Save(outputPath + "dice_u3d.pdf", uint(SDFDocE_linearized))
100 doc.Close()
101 PDFNetTerminate()
102 fmt.Println("Done.")
103}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.pdf.*;
7import com.pdftron.sdf.Obj;
8import com.pdftron.sdf.SDFDoc;
9import com.pdftron.common.PDFNetException;
10import com.pdftron.filters.*;
11
12
13public class U3DTest {
14
15 static String input_path = "../../TestFiles/";
16
17 static void Create3DAnnotation(PDFDoc doc, Obj annots) throws PDFNetException {
18 // ---------------------------------------------------------------------------------
19 // Create a 3D annotation based on U3D content. PDF 1.6 introduces the capability
20 // for collections of three-dimensional objects, such as those used by CAD software,
21 // to be embedded in PDF files.
22 Obj link_3D = doc.createIndirectDict();
23 link_3D.putName("Subtype", "3D");
24
25 // Annotation location on the page
26 Rect link_3D_rect = new Rect(25, 180, 585, 643);
27 link_3D.putRect("Rect", link_3D_rect.getX1(), link_3D_rect.getY1(),
28 link_3D_rect.getX2(), link_3D_rect.getY2());
29 annots.pushBack(link_3D);
30
31 // The 3DA entry is an activation dictionary (see Table 9.34 in the PDF Reference Manual)
32 // that determines how the state of the annotation and its associated artwork can change.
33 Obj activation_dict_3D = link_3D.putDict("3DA");
34
35 // Set the annotation so that it is activated as soon as the page containing the
36 // annotation is opened. Other options are: PV (page view) and XA (explicit) activation.
37 activation_dict_3D.putName("A", "PO");
38
39 // Embed U3D Streams (3D Model/Artwork).
40 {
41 MappedFile u3d_file = new MappedFile(input_path + "dice.u3d");
42 FilterReader u3d_reader = new FilterReader(u3d_file);
43
44 // To embed 3D stream without compression, you can omit the second parameter in CreateIndirectStream.
45 Obj u3d_data_dict = doc.createIndirectStream(u3d_reader, new FlateEncode(null));
46 u3d_data_dict.putName("Subtype", "U3D");
47 link_3D.put("3DD", u3d_data_dict);
48 }
49
50 // Set the initial view of the 3D artwork that should be used when the annotation is activated.
51 Obj view3D_dict = link_3D.putDict("3DV");
52 {
53 view3D_dict.putString("IN", "Unnamed");
54 view3D_dict.putString("XN", "Default");
55 view3D_dict.putName("MS", "M");
56 view3D_dict.putNumber("CO", 27.5);
57
58 // A 12-element 3D transformation matrix that specifies a position and orientation
59 // of the camera in world coordinates.
60 Obj tr3d = view3D_dict.putArray("C2W");
61 tr3d.pushBackNumber(1);
62 tr3d.pushBackNumber(0);
63 tr3d.pushBackNumber(0);
64 tr3d.pushBackNumber(0);
65 tr3d.pushBackNumber(0);
66 tr3d.pushBackNumber(-1);
67 tr3d.pushBackNumber(0);
68 tr3d.pushBackNumber(1);
69 tr3d.pushBackNumber(0);
70 tr3d.pushBackNumber(0);
71 tr3d.pushBackNumber(-27.5);
72 tr3d.pushBackNumber(0);
73
74 }
75
76 // Create annotation appearance stream, a thumbnail which is used during printing or
77 // in PDF processors that do not understand 3D data.
78 Obj ap_dict = link_3D.putDict("AP");
79 {
80 ElementBuilder builder = new ElementBuilder();
81 ElementWriter writer = new ElementWriter();
82 writer.begin(doc);
83
84 String thumb_pathname = input_path + "dice.jpg";
85 Image image = Image.create(doc, thumb_pathname);
86 writer.writePlacedElement(builder.createImage(image, 0.0, 0.0, link_3D_rect.getWidth(), link_3D_rect.getHeight()));
87
88 Obj normal_ap_stream = writer.end();
89 normal_ap_stream.putName("Subtype", "Form");
90 normal_ap_stream.putRect("BBox", 0, 0, link_3D_rect.getWidth(), link_3D_rect.getHeight());
91 ap_dict.put("N", normal_ap_stream);
92 }
93 }
94
95 public static void main(String[] args) {
96 String output_path = "../../TestFiles/Output/";
97 PDFNet.initialize(PDFTronLicense.Key());
98 try (PDFDoc doc = new PDFDoc()) {
99 Page page = doc.pageCreate();
100 doc.pagePushBack(page);
101 Obj annots = doc.createIndirectArray();
102 page.getSDFObj().put("Annots", annots);
103
104 Create3DAnnotation(doc, annots);
105 doc.save(output_path + "dice_u3d.pdf", SDFDoc.SaveMode.LINEARIZED, null);
106 System.out.println("Done");
107 } catch (Exception e) {
108 e.printStackTrace();
109 }
110
111 PDFNet.terminate();
112 }
113}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6const { PDFNet } = require('@pdftron/pdfnet-node');
7const PDFTronLicense = require('../LicenseKey/LicenseKey');
8
9((exports) => {
10 'use strict';
11
12 exports.runU3DTest = () => {
13 const input_path = '../TestFiles/';
14
15 const create3DAnnotation = async (doc, annots) => {
16 // ---------------------------------------------------------------------------------
17 // Create a 3D annotation based on U3D content. PDF 1.6 introduces the capability
18 // for collections of three-dimensional objects, such as those used by CAD software,
19 // to be embedded in PDF files.
20 const link_3D = await doc.createIndirectDict();
21 link_3D.putName('Subtype', '3D');
22
23 // Annotation location on the page
24 const link_3D_rect = await PDFNet.Rect.init(25, 180, 585, 643);
25 link_3D.putRect('Rect', link_3D_rect.x1, link_3D_rect.y1,
26 link_3D_rect.x2, link_3D_rect.y2);
27 annots.pushBack(link_3D);
28
29 // The 3DA entry is an activation dictionary (see Table 9.34 in the PDF Reference Manual)
30 // that determines how the state of the annotation and its associated artwork can change.
31 const activation_dict_3D = await link_3D.putDict('3DA');
32
33 // Set the annotation so that it is activated as soon as the page containing the
34 // annotation is opened. Other options are: PV (page view) and XA (explicit) activation.
35 activation_dict_3D.putName('A', 'PO');
36
37 // Embed U3D Streams (3D Model/Artwork).
38 const u3d_file = await PDFNet.Filter.createMappedFileFromUString(input_path + 'dice.u3d');
39 const u3d_reader = await PDFNet.FilterReader.create(u3d_file);
40
41 // To embed 3D stream without compression, you can omit the second parameter in CreateIndirectStream.
42 const flateEncode = await PDFNet.Filter.createFlateEncode();
43 const u3d_data_dict = await doc.createIndirectStreamFromFilter(u3d_reader, flateEncode);
44 u3d_data_dict.putName('Subtype', 'U3D');
45 link_3D.put('3DD', u3d_data_dict);
46
47 // Set the initial view of the 3D artwork that should be used when the annotation is activated.
48 const view3D_dict = await link_3D.putDict('3DV');
49 view3D_dict.putString('IN', 'Unnamed');
50 view3D_dict.putString('XN', 'Default');
51 view3D_dict.putName('MS', 'M');
52 view3D_dict.putNumber('CO', 27.5);
53
54 // A 12-element 3D transformation matrix that specifies a position and orientation
55 // of the camera in world coordinates.
56 const tr3d = await view3D_dict.putArray('C2W');
57 tr3d.pushBackNumber(1); tr3d.pushBackNumber(0); tr3d.pushBackNumber(0);
58 tr3d.pushBackNumber(0); tr3d.pushBackNumber(0); tr3d.pushBackNumber(-1);
59 tr3d.pushBackNumber(0); tr3d.pushBackNumber(1); tr3d.pushBackNumber(0);
60 tr3d.pushBackNumber(0); tr3d.pushBackNumber(-27.5); tr3d.pushBackNumber(0);
61
62 // Create annotation appearance stream, a thumbnail which is used during printing or
63 // in PDF processors that do not understand 3D data.
64 const ap_dict = await link_3D.putDict('AP');
65
66 const builder = await PDFNet.ElementBuilder.create();
67 const writer = await PDFNet.ElementWriter.create();
68
69 writer.begin(doc);
70
71 const thumb_pathname = input_path + 'dice.jpg';
72 const image = await PDFNet.Image.createFromFile(doc, thumb_pathname);
73 writer.writePlacedElement(await builder.createImageScaled(image, 0.0, 0.0, await link_3D_rect.width(), await link_3D_rect.height()));
74
75 const normal_ap_stream = await writer.end();
76 normal_ap_stream.putName('Subtype', 'Form');
77 normal_ap_stream.putRect('BBox', 0, 0, await link_3D_rect.width(), await link_3D_rect.height());
78 ap_dict.put('N', normal_ap_stream);
79 }
80
81 const main = async () => {
82 const output_path = '../TestFiles/Output/';
83
84 try {
85 const doc = await PDFNet.PDFDoc.create();
86 const page = await doc.pageCreate();
87 doc.pagePushBack(page);
88 const annots = await doc.createIndirectArray();
89 page.getSDFObj().then(sdf => sdf.put('Annots', annots));
90
91 await create3DAnnotation(doc, annots);
92 doc.save(output_path + 'dice_u3d.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
93 console.log('Done');
94 } catch (err) {
95 console.log(err);
96 }
97 }
98 PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error) {
99 console.log('Error: ' + JSON.stringify(error));
100 }).then(function(){ return PDFNet.shutdown(); });
101 };
102 exports.runU3DTest();
103})(exports);
104// eslint-disable-next-line spaced-comment
105//# sourceURL=U3DTest.js
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14# Relative path to the folder containing the test files.
15input_path = "../../TestFiles/"
16output_path = "../../TestFiles/Output/"
17
18def Create3DAnnotation(doc, annots):
19 # ---------------------------------------------------------------------------------
20 # Create a 3D annotation based on U3D content. PDF 1.6 introduces the capability
21 # for collections of three-dimensional objects, such as those used by CAD software,
22 # to be embedded in PDF files.
23 link_3D = doc.CreateIndirectDict()
24 link_3D.PutName("Subtype", "3D")
25
26 # Annotation location on the page
27 link_3D_rect = Rect(25, 180, 585, 643)
28 link_3D.PutRect("Rect", link_3D_rect.x1, link_3D_rect.y1,
29 link_3D_rect.x2, link_3D_rect.y2)
30 annots.PushBack(link_3D)
31
32 # The 3DA entry is an activation dictionary (see Table 9.34 in the PDF Reference Manual)
33 # that determines how the state of the annotation and its associated artwork can change.
34 activation_dict_3D = link_3D.PutDict("3DA")
35
36 # Set the annotation so that it is activated as soon as the page containing the
37 # annotation is opened. Other options are: PV (page view) and XA (explicit) activation.
38 activation_dict_3D.PutName("A", "PO")
39
40 # Embed U3D Streams (3D Model/Artwork).
41 u3d_file = MappedFile(input_path + "dice.u3d")
42 u3d_reader = FilterReader(u3d_file)
43
44 u3d_data_dict = doc.CreateIndirectStream(u3d_reader, FlateEncode(Filter()))
45 u3d_data_dict.PutName("Subtype", "U3D")
46 link_3D.Put("3DD", u3d_data_dict)
47
48 # Set the initial view of the 3D artwork that should be used when the annotation is activated.
49 view3D_dict = link_3D.PutDict("3DV")
50
51 view3D_dict.PutString("IN", "Unnamed")
52 view3D_dict.PutString("XN", "Default")
53 view3D_dict.PutName("MS", "M")
54 view3D_dict.PutNumber("CO", 27.5)
55
56 # A 12-element 3D transformation matrix that specifies a position and orientation
57 # of the camera in world coordinates.
58 tr3d = view3D_dict.PutArray("C2W")
59 tr3d.PushBackNumber(1)
60 tr3d.PushBackNumber(0)
61 tr3d.PushBackNumber(0)
62 tr3d.PushBackNumber(0)
63 tr3d.PushBackNumber(0)
64 tr3d.PushBackNumber(-1)
65 tr3d.PushBackNumber(0)
66 tr3d.PushBackNumber(1)
67 tr3d.PushBackNumber(0)
68 tr3d.PushBackNumber(0)
69 tr3d.PushBackNumber(-27.5)
70 tr3d.PushBackNumber(0)
71
72 # Create annotation appearance stream, a thumbnail which is used during printing or
73 # in PDF processors that do not understand 3D data.
74 ap_dict = link_3D.PutDict("AP")
75
76 builder = ElementBuilder()
77 writer = ElementWriter()
78 writer.Begin(doc.GetSDFDoc())
79
80 thumb_pathname = input_path + "dice.jpg"
81 image = Image.Create(doc.GetSDFDoc(), thumb_pathname)
82 writer.WritePlacedElement(builder.CreateImage(image, 0.0, 0.0, link_3D_rect.Width(), link_3D_rect.Height()))
83
84 normal_ap_stream = writer.End()
85 normal_ap_stream.PutName("Subtype", "Form")
86 normal_ap_stream.PutRect("BBox", 0, 0, link_3D_rect.Width(), link_3D_rect.Height())
87 ap_dict.Put("N", normal_ap_stream)
88
89def main():
90 PDFNet.Initialize(LicenseKey)
91
92 doc = PDFDoc()
93 page = doc.PageCreate()
94 doc.PagePushBack(page)
95 annots = doc.CreateIndirectArray()
96 page.GetSDFObj().Put("Annots", annots)
97
98 Create3DAnnotation(doc, annots)
99 doc.Save(output_path + "dice_u3d.pdf", SDFDoc.e_linearized)
100 doc.Close()
101 PDFNet.Terminate()
102 print("Done.")
103
104if __name__ == '__main__':
105 main()
1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10// Relative path to the folder containing the test files.
11$input_path = getcwd()."/../../TestFiles/";
12$output_path = $input_path."Output/";
13
14function Create3DAnnotation($doc, $annots)
15{
16 // ---------------------------------------------------------------------------------
17 // Create a 3D annotation based on U3D content. PDF 1.6 introduces the capability
18 // for collections of three-dimensional objects, such as those used by CAD software,
19 // to be embedded in PDF files.
20 $link_3D = $doc->CreateIndirectDict();
21 $link_3D->PutName("Subtype", "3D");
22
23 // Annotation location on the page
24 $link_3D_rect = new Rect(25.0, 180.0, 585.0, 643.0);
25 $link_3D->PutRect("Rect", $link_3D_rect->x1, $link_3D_rect->y1,
26 $link_3D_rect->x2, $link_3D_rect->y2);
27 $annots->PushBack($link_3D);
28
29 // The 3DA entry is an activation dictionary (see Table 9.34 in the PDF Reference Manual)
30 // that determines how the state of the annotation and its associated artwork can change.
31 $activation_dict_3D = $link_3D->PutDict("3DA");
32
33 // Set the annotation so that it is activated as soon as the page containing the
34 // annotation is opened. Other options are: PV (page view) and XA (explicit) activation.
35 $activation_dict_3D->PutName("A", "PO");
36
37 // Embed U3D Streams (3D Model/Artwork).
38 global $input_path;
39 $u3d_file = new MappedFile($input_path."dice.u3d");
40 $u3d_reader = new FilterReader($u3d_file);
41
42 // To embed 3D stream without compression, you can omit the second parameter in CreateIndirectStream.
43 $u3d_data_dict = $doc->CreateIndirectStream($u3d_reader, new FlateEncode(new Filter()));
44 $u3d_data_dict->PutName("Subtype", "U3D");
45 $link_3D->Put("3DD", $u3d_data_dict);
46
47 // Set the initial view of the 3D artwork that should be used when the annotation is activated.
48 $view3D_dict = $link_3D->PutDict("3DV");
49
50 $view3D_dict->PutString("IN", "Unnamed");
51 $view3D_dict->PutString("XN", "Default");
52 $view3D_dict->PutName("MS", "M");
53 $view3D_dict->PutNumber("CO", 27.5);
54
55 // A 12-element 3D transformation matrix that specifies a position and orientation
56 // of the camera in world coordinates.
57 $tr3d = $view3D_dict->PutArray("C2W");
58 $tr3d->PushBackNumber(1); $tr3d->PushBackNumber(0); $tr3d->PushBackNumber(0);
59 $tr3d->PushBackNumber(0); $tr3d->PushBackNumber(0); $tr3d->PushBackNumber(-1);
60 $tr3d->PushBackNumber(0); $tr3d->PushBackNumber(1); $tr3d->PushBackNumber(0);
61 $tr3d->PushBackNumber(0); $tr3d->PushBackNumber(-27.5); $tr3d->PushBackNumber(0);
62
63 // Create annotation appearance stream, a thumbnail which is used during printing or
64 // in PDF processors that do not understand 3D data.
65 $ap_dict = $link_3D->PutDict("AP");
66
67 $builder = new ElementBuilder();
68 $writer = new ElementWriter();
69 $writer->Begin($doc->GetSDFDoc());
70
71 $thumb_pathname = $input_path."dice.jpg";
72 $image = Image::Create($doc->GetSDFDoc(), $thumb_pathname);
73 $writer->WritePlacedElement($builder->CreateImage($image, 0.0, 0.0, $link_3D_rect->Width(), $link_3D_rect->Height()));
74
75 $normal_ap_stream = $writer->End();
76 $normal_ap_stream->PutName("Subtype", "Form");
77 $normal_ap_stream->PutRect("BBox", 0, 0, $link_3D_rect->Width(), $link_3D_rect->Height());
78 $ap_dict->Put("N", $normal_ap_stream);
79}
80
81// ---------------------------------------------------------------------------------
82
83 PDFNet::Initialize($LicenseKey);
84 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
85
86 $doc = new PDFDoc();
87 $page = $doc->PageCreate();
88 $doc->PagePushBack($page);
89 $annots = $doc->CreateIndirectArray();
90 $page->GetSDFObj()->Put("Annots", $annots);
91
92 Create3DAnnotation($doc, $annots);
93 $doc->Save($output_path."dice_u3d.pdf", SDFDoc::e_linearized);
94 PDFNet::Terminate();
95 echo "Done.\n";
96?>
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12# Relative path to the folder containing the test files.
13$input_path = "../../TestFiles/"
14$output_path = "../../TestFiles/Output/"
15
16def Create3DAnnotation(doc, annots)
17 # ---------------------------------------------------------------------------------
18 # Create a 3D annotation based on U3D content. PDF 1.6 introduces the capability
19 # for collections of three-dimensional objects, such as those used by CAD software,
20 # to be embedded in PDF files.
21 link_3D = doc.CreateIndirectDict
22 link_3D.PutName("Subtype", "3D")
23
24 # Annotation location on the page
25 link_3D_rect = Rect.new(25, 180, 585, 643)
26 link_3D.PutRect("Rect", link_3D_rect.x1, link_3D_rect.y1,
27 link_3D_rect.x2, link_3D_rect.y2)
28 annots.PushBack(link_3D)
29
30 # The 3DA entry is an activation dictionary (see Table 9.34 in the PDF Reference Manual)
31 # that determines how the state of the annotation and its associated artwork can change.
32 activation_dict_3D = link_3D.PutDict("3DA")
33
34 # Set the annotation so that it is activated as soon as the page containing the
35 # annotation is opened. Other options are: PV (page view) and XA (explicit) activation.
36 activation_dict_3D.PutName("A", "PO")
37
38 # Embed U3D Streams (3D Model/Artwork).
39 u3d_file = MappedFile.new($input_path + "dice.u3d")
40 u3d_reader = FilterReader.new(u3d_file)
41
42 u3d_data_dict = doc.CreateIndirectStream(u3d_reader, FlateEncode.new(Filter.new))
43 u3d_data_dict.PutName("Subtype", "U3D")
44 link_3D.Put("3DD", u3d_data_dict)
45
46 # Set the initial view of the 3D artwork that should be used when the annotation is activated.
47 view3D_dict = link_3D.PutDict("3DV")
48
49 view3D_dict.PutString("IN", "Unnamed")
50 view3D_dict.PutString("XN", "Default")
51 view3D_dict.PutName("MS", "M")
52 view3D_dict.PutNumber("CO", 27.5)
53
54 # A 12-element 3D transformation matrix that specifies a position and orientation
55 # of the camera in world coordinates.
56 tr3d = view3D_dict.PutArray("C2W")
57 tr3d.PushBackNumber(1)
58 tr3d.PushBackNumber(0)
59 tr3d.PushBackNumber(0)
60 tr3d.PushBackNumber(0)
61 tr3d.PushBackNumber(0)
62 tr3d.PushBackNumber(-1)
63 tr3d.PushBackNumber(0)
64 tr3d.PushBackNumber(1)
65 tr3d.PushBackNumber(0)
66 tr3d.PushBackNumber(0)
67 tr3d.PushBackNumber(-27.5)
68 tr3d.PushBackNumber(0)
69
70 # Create annotation appearance stream, a thumbnail which is used during printing or
71 # in PDF processors that do not understand 3D data.
72 ap_dict = link_3D.PutDict("AP")
73
74 builder = ElementBuilder.new
75 writer = ElementWriter.new
76 writer.Begin(doc.GetSDFDoc)
77
78 thumb_pathname = $input_path + "dice.jpg"
79 image = Image.Create(doc.GetSDFDoc, thumb_pathname)
80 writer.WritePlacedElement(builder.CreateImage(image, 0.0, 0.0, link_3D_rect.Width, link_3D_rect.Height))
81
82 normal_ap_stream = writer.End
83 normal_ap_stream.PutName("Subtype", "Form")
84 normal_ap_stream.PutRect("BBox", 0, 0, link_3D_rect.Width, link_3D_rect.Height)
85 ap_dict.Put("N", normal_ap_stream)
86end
87
88 PDFNet.Initialize(PDFTronLicense.Key)
89
90 doc = PDFDoc.new
91 page = doc.PageCreate
92 doc.PagePushBack(page)
93 annots = doc.CreateIndirectArray
94 page.GetSDFObj.Put("Annots", annots)
95
96 Create3DAnnotation(doc, annots)
97 doc.Save($output_path + "dice_u3d.pdf", SDFDoc::E_linearized)
98 doc.Close
99 PDFNet.Terminate
100 puts "Done."
1'
2' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3'
4Imports System
5Imports pdftron
6Imports pdftron.Common
7Imports pdftron.Filters
8Imports pdftron.SDF
9Imports PDFTRON.PDF
10Module U3DTestVB
11 Dim pdfNetLoader As PDFNetLoader
12 Sub New()
13 pdfNetLoader = pdftron.PDFNetLoader.Instance()
14 End Sub
15
16 Sub Main()
17 PDFNet.Initialize(PDFTronLicense.Key)
18 Const output_path As String = "../../../../TestFiles/Output/"
19
20 Try
21 Using doc As PDFDoc = New PDFDoc
22 Dim page As Page = doc.PageCreate
23 doc.PagePushBack(page)
24 Dim annots As Obj = doc.CreateIndirectArray
25 page.GetSDFObj.Put("Annots", annots)
26 U3DTest.Create3DAnnotation(doc, annots)
27 doc.Save(output_path + "dice_u3d.pdf", SDFDoc.SaveOptions.e_linearized)
28 Console.WriteLine("Done. Result saved in dice_u3d.pdf")
29 End Using
30 Catch e As PDFNetException
31 Console.WriteLine(e.Message)
32 End Try
33 PDFNet.Terminate()
34 End Sub
35
36 Class U3DTest
37 Shared Sub Create3DAnnotation(ByVal doc As PDFDoc, ByVal annots As Obj)
38 ' ---------------------------------------------------------------------------------
39 ' Create a 3D annotation based on U3D content. PDF 1.6 introduces the capability
40 ' for collections of three-dimensional objects, such as those used by CAD software,
41 ' to be embedded in PDF files.
42
43 Const input_path As String = "../../../../TestFiles/"
44 Dim link_3D As Obj = doc.CreateIndirectDict
45 link_3D.PutName("Subtype", "3D")
46
47 ' Annotation location on the page
48 Dim bbox As Rect = New Rect(25, 180, 585, 643)
49 link_3D.PutRect("Rect", bbox.x1, bbox.y1, bbox.x2, bbox.y2)
50 annots.PushBack(link_3D)
51
52 ' The 3DA entry is an activation dictionary (see Table 9.34 in the PDF Reference Manual)
53 ' that determines how the state of the annotation and its associated artwork can change.
54 Dim activation_dict_3D As Obj = link_3D.PutDict("3DA")
55
56 ' Set the annotation so that it is activated as soon as the page containing the
57 ' annotation is opened. Other options are: PV (page view) and XA (explicit) activation.
58
59 ' Embed U3D Streams (3D Model/Artwork).
60 activation_dict_3D.PutName("A", "PO")
61 Dim u3d_file As MappedFile = New MappedFile(input_path + "dice.u3d")
62 Dim u3d_reader As FilterReader = New FilterReader(u3d_file)
63 Dim u3d_data_dict As Obj = doc.CreateIndirectStream(u3d_reader)
64 u3d_data_dict.PutName("Subtype", "U3D")
65 link_3D.Put("3DD", u3d_data_dict)
66
67 ' Set the initial view of the 3D artwork that should be used when the annotation is activated.
68 Dim view3D_dict As Obj = link_3D.PutDict("3DV")
69 view3D_dict.PutString("IN", "Unnamed")
70 view3D_dict.PutString("XN", "Default")
71 view3D_dict.PutName("MS", "M")
72 view3D_dict.PutNumber("CO", 27.5)
73
74 ' A 12-element 3D transformation matrix that specifies a position and orientation
75 ' of the camera in world coordinates.
76 Dim tr3d As Obj = view3D_dict.PutArray("C2W")
77 tr3d.PushBackNumber(1)
78 tr3d.PushBackNumber(0)
79 tr3d.PushBackNumber(0)
80 tr3d.PushBackNumber(0)
81 tr3d.PushBackNumber(0)
82 tr3d.PushBackNumber(-1)
83 tr3d.PushBackNumber(0)
84 tr3d.PushBackNumber(1)
85 tr3d.PushBackNumber(0)
86 tr3d.PushBackNumber(0)
87 tr3d.PushBackNumber(-27.5)
88 tr3d.PushBackNumber(0)
89
90 ' Create annotation appearance stream, a thumbnail which is used during printing or
91 ' in PDF processors that do not understand 3D data.
92 Dim ap_dict As Obj = link_3D.PutDict("AP")
93
94 Using builder As ElementBuilder = New ElementBuilder
95 Using writer As ElementWriter = New ElementWriter
96 writer.Begin(doc.GetSDFDoc)
97 writer.WritePlacedElement(builder.CreateImage(Image.Create(doc.GetSDFDoc, input_path + "dice.jpg"), 0, 0, bbox.Width, bbox.Height))
98 Dim normal_ap_stream As Obj = writer.End
99 normal_ap_stream.PutName("Subtype", "Form")
100 normal_ap_stream.PutRect("BBox", 0, 0, bbox.Width, bbox.Height)
101 ap_dict.Put("N", normal_ap_stream)
102 End Using
103 End Using
104 End Sub
105 End Class
106
107End Module
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales