Annotation3D.cs
//
// This code is part of Document Solutions for PDF .NET demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Pdf.Annotations;
using DsPdfWeb.Demos.Common;

namespace DsPdfWeb.Demos.Basics
{
    // This demo shows how to embed an interactive 3D model in a PDF using Annotation3D.
    public class Annotation3D
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            var page = doc.NewPage();

            var rc = Util.AddNote(
                "The rectangle below is a Model3DAnnotation containing a 3D model. " +
                "It activates automatically when this page becomes visible, but can only be viewed interactively " +
                "in a PDF reader with native 3D support, such as Adobe Acrobat/Reader.", page);

            // Load the 3D model (U3D format):
            var modelPath = Path.Combine("Resources", "3DModels", "plane64x64_base.u3d");
            using var modelStream = File.OpenRead(modelPath);

            // Add a 3D annotation showing the model, activated when the page becomes visible:
            var m3d = new Model3DAnnotation
            {
                Stream = new Model3DStream(modelStream, Model3DStreamFormat.U3D),
                ActivationCondition = Model3DActivationCondition.PageBecomesVisible,
                DeactivationCondition = Model3DDeactivationCondition.PageBecomesInvisible,
                Page = page,
                Rect = new RectangleF(rc.X, rc.Bottom + 18, 72 * 5, 72 * 4)
            };

            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}