Universal 3D (U3D) is a compressed file format for 3D computer graphic data. 3D modules in U3D format can be inserted into PDF documents and interactively visualized by Acrobat Reader. This article presents how to embed a pre-created U3D file into a PDF document using Spire.PDF in C#, VB.NET.
Main Steps:
Step 1: Initialize a new object of PdfDocuemnt, and add a blank page to the PDF document.
PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add();
Step 2: Draw a rectangle on the page to define the canvas area for the 3D file.
Rectangle rt = new Rectangle(0, 80, 200, 200);
Step 3: Initialize a new object of Pdf3DAnnotation, load the .u3d file as 3D annotation.
Pdf3DAnnotation annotation = new Pdf3DAnnotation(rt, "teapot.u3d"); annotation.Activation = new Pdf3DActivation(); annotation.Activation.ActivationMode = Pdf3DActivationMode.PageOpen;
Step 4: Define a 3D view mode.
Pdf3DView View= new Pdf3DView(); View.Background = new Pdf3DBackground(new PdfRGBColor(Color.Purple )); View.ViewNodeName = "test"; View.RenderMode = new Pdf3DRendermode(Pdf3DRenderStyle.Solid); View.InternalName = "test"; View.LightingScheme = new Pdf3DLighting(); View.LightingScheme.Style = Pdf3DLightingStyle.Day;
Step 5: Set the 3D view mode for the annotation.
annotation.Views.Add(View);
Step 6: Add the annotation to PDF.
page.AnnotationsWidget.Add(annotation);
Step 7: Save the file.
doc.SaveToFile("Create3DPdf.pdf", FileFormat.PDF);
Output:
Full Code:
[C#]
using Spire.Pdf; using Spire.Pdf.Annotations; using Spire.Pdf.Graphics; using System.Drawing; namespace Embed3DInteractiveGraphics { class Program { static void Main(string[] args) { PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add(); Rectangle rt = new Rectangle(0, 80, 200, 200); Pdf3DAnnotation annotation = new Pdf3DAnnotation(rt, "teapot.u3d"); annotation.Activation = new Pdf3DActivation(); annotation.Activation.ActivationMode = Pdf3DActivationMode.PageOpen; Pdf3DView View= new Pdf3DView(); View.Background = new Pdf3DBackground(new PdfRGBColor(Color.Purple)); View.ViewNodeName = "test"; View.RenderMode = new Pdf3DRendermode(Pdf3DRenderStyle.Solid); View.InternalName = "test"; View.LightingScheme = new Pdf3DLighting(); View.LightingScheme.Style = Pdf3DLightingStyle.Day; annotation.Views.Add(View); page.AnnotationsWidget.Add(annotation); doc.SaveToFile("Create3DPdf.pdf", FileFormat.PDF); } } }
[VB.NET]
Imports Spire.Pdf Imports Spire.Pdf.Annotations Imports Spire.Pdf.Graphics Imports System.Drawing Namespace Embed3DInteractiveGraphics Class Program Private Shared Sub Main(args As String()) Dim doc As New PdfDocument() Dim page As PdfPageBase = doc.Pages.Add() Dim rt As New Rectangle(0, 80, 200, 200) Dim annotation As New Pdf3DAnnotation(rt, "teapot.u3d") annotation.Activation = New Pdf3DActivation() annotation.Activation.ActivationMode = Pdf3DActivationMode.PageOpen Dim View As New Pdf3DView() View.Background = New Pdf3DBackground(New PdfRGBColor(Color.Purple)) View.ViewNodeName = "test" View.RenderMode = New Pdf3DRendermode(Pdf3DRenderStyle.Solid) View.InternalName = "test" View.LightingScheme = New Pdf3DLighting() View.LightingScheme.Style = Pdf3DLightingStyle.Day annotation.Views.Add(View) page.AnnotationsWidget.Add(annotation) doc.SaveToFile("Create3DPdf.pdf", FileFormat.PDF) End Sub End Class End Namespace