public static void SavePdf(U3DDocument u3dDoc, Stream pdfDocStream) { var pdfDoc = new PdfDocument(); PdfPageBase page = pdfDoc.Pages.Add(PdfPageSize.A4, new PdfMargins(30.0f), PdfPageRotateAngle.RotateAngle0, PdfPageOrientation.Landscape); var rectangle = new System.Drawing.Rectangle(0, 0, (int)(page.Size.Width - 60.0f), (int)(page.Size.Height - 60.0f)); string u3dTempFileName = Path.GetTempFileName(); Save(u3dDoc, File.Create(u3dTempFileName)); Pdf3DAnnotation annotation = new Pdf3DAnnotation(rectangle, u3dTempFileName); annotation.Activation = new Pdf3DActivation(); annotation.Activation.ActivationMode = Pdf3DActivationMode.PageOpen; Pdf3DView View = new Pdf3DView(); View.Background = new Pdf3DBackground(new PdfRGBColor(System.Drawing.Color.White)); View.ExternalName = "Default"; View.InternalName = "Default"; View.ViewNodeName = "Default"; View.RenderMode = new Pdf3DRendermode(Pdf3DRenderStyle.Solid); View.LightingScheme = new Pdf3DLighting(); View.LightingScheme.Style = Pdf3DLightingStyle.Hard; annotation.Views.Add(View); page.AnnotationsWidget.Add(annotation); pdfDoc.SaveToStream(pdfDocStream, FileFormat.PDF); File.Delete(u3dTempFileName); }
public void AddU3DImage(U3DInfo u3DInfo) { try { // page starts from 0 as per spire.pdf int pageNum = u3DInfo.Page - 1; if (pageNum < 0) { pageNum = 0; } if (!(_doc.Pages.Count > pageNum)) { for (int i = 0; i <= pageNum - _doc.Pages.Count; i++) { _doc.Pages.Add(); } } PdfPageBase page = _doc.Pages[pageNum]; string name = System.IO.Path.GetFileNameWithoutExtension(u3DInfo.U3DFile); int y = (int)(u3DInfo.Y - u3DInfo.Height); // adjust the position to map Y to zero by reducing its height - weird ah... Rectangle rt = new Rectangle((int)u3DInfo.X, y, (int)u3DInfo.Width, (int)u3DInfo.Height); Pdf3DAnnotation annotation = new Pdf3DAnnotation(rt, u3DInfo.U3DFile) { Activation = new Pdf3DActivation { ActivationMode = Pdf3DActivationMode.PageOpen } }; Pdf3DView view = new Pdf3DView { Background = new Pdf3DBackground(new PdfRGBColor(Color.White)), ViewNodeName = name, RenderMode = new Pdf3DRendermode(Pdf3DRenderStyle.Solid), InternalName = name, LightingScheme = new Pdf3DLighting { Style = Pdf3DLightingStyle.Headlamp } }; annotation.Views.Add(view); page.AnnotationsWidget.Add(annotation); } catch { // ignored } }
private void button1_Click(object sender, EventArgs e) { //Create a new Pdf document. PdfDocument doc = new PdfDocument(); //Add a new page to it. PdfPageBase page = doc.Pages.Add(); //Draw a rectangle on the page to define the canvas area for the 3D file. Rectangle rt = new Rectangle(0, 80, 200, 200); //Initialize a new object of Pdf3DAnnotation, load the .u3d file as 3D annotation. Pdf3DAnnotation annotation = new Pdf3DAnnotation(rt, @"..\..\..\..\..\..\Data\CreatePdf3DAnnotation.u3d"); annotation.Activation = new Pdf3DActivation(); annotation.Activation.ActivationMode = Pdf3DActivationMode.PageOpen; //Define a 3D view mode. Pdf3DView View = new Pdf3DView(); View.Background = new Pdf3DBackground(new PdfRGBColor(Color.Purple)); View.ViewNodeName = "3DAnnotation"; View.RenderMode = new Pdf3DRendermode(Pdf3DRenderStyle.Solid); View.InternalName = "3DAnnotation"; View.LightingScheme = new Pdf3DLighting(); View.LightingScheme.Style = Pdf3DLightingStyle.Day; //Set the 3D view mode for the annotation. annotation.Views.Add(View); //Add the annotation to Pdf. page.AnnotationsWidget.Add(annotation); String result = "CreatePdf3DAnnotation_out.pdf"; //Save the document doc.SaveToFile(result); //Launch the Pdf file PDFDocumentViewer(result); }
private static void Create3DAnnotations(PdfFixedDocument document, PdfFont font, Stream u3dStream) { PdfBrush blackBrush = new PdfBrush(); PdfPage page = document.Pages.Add(); page.Rotation = 90; page.Graphics.DrawString("3D annotations", font, blackBrush, 50, 50); byte[] u3dContent = new byte[u3dStream.Length]; u3dStream.Read(u3dContent, 0, u3dContent.Length); Pdf3DView view0 = new Pdf3DView(); view0.CameraToWorldMatrix = new double[] { 1, 0, 0, 0, 0, -1, 0, 1, 0, -0.417542, -0.881257, -0.125705 }; view0.CenterOfOrbit = 0.123106; view0.ExternalName = "Default"; view0.InternalName = "Default"; view0.Projection = new Pdf3DProjection(); view0.Projection.FieldOfView = 30; Pdf3DView view1 = new Pdf3DView(); view1.CameraToWorldMatrix = new double[] { -0.999888, 0.014949, 0, 0.014949, 0.999887, 0.00157084, 0.0000234825, 0.00157066, -0.999999, -0.416654, -0.761122, -0.00184508 }; view1.CenterOfOrbit = 0.123106; view1.ExternalName = "Top"; view1.InternalName = "Top"; view1.Projection = new Pdf3DProjection(); view1.Projection.FieldOfView = 14.8096; Pdf3DView view2 = new Pdf3DView(); view2.CameraToWorldMatrix = new double[] { -1.0, -0.0000411671, 0.0000000000509201, -0.00000101387, 0.0246288, 0.999697, -0.0000411546, 0.999697, -0.0246288, -0.417072, -0.881787, -0.121915 }; view2.CenterOfOrbit = 0.123106; view2.ExternalName = "Side"; view2.InternalName = "Side"; view2.Projection = new Pdf3DProjection(); view2.Projection.FieldOfView = 12.3794; Pdf3DView view3 = new Pdf3DView(); view3.CameraToWorldMatrix = new double[] { -0.797002, -0.603977, -0.0000000438577, -0.294384, 0.388467, 0.873173, -0.527376, 0.695921, -0.48741, -0.3518, -0.844506, -0.0675629 }; view3.CenterOfOrbit = 0.123106; view3.ExternalName = "Isometric"; view3.InternalName = "Isometric"; view3.Projection = new Pdf3DProjection(); view3.Projection.FieldOfView = 15.1226; Pdf3DView view4 = new Pdf3DView(); view4.CameraToWorldMatrix = new double[] { 0.00737633, -0.999973, -0.0000000000147744, -0.0656414, -0.000484206, 0.997843, -0.997816, -0.00736042, -0.0656432, -0.293887, -0.757928, -0.119485 }; view4.CenterOfOrbit = 0.123106; view4.ExternalName = "Front"; view4.InternalName = "Front"; view4.Projection = new Pdf3DProjection(); view4.Projection.FieldOfView = 15.1226; Pdf3DView view5 = new Pdf3DView(); view5.CameraToWorldMatrix = new double[] { 0.0151008, 0.999886, 0.0000000000261366, 0.0492408, -0.000743662, 0.998787, 0.998673, -0.0150825, -0.0492464, -0.540019, -0.756862, -0.118884 }; view5.CenterOfOrbit = 0.123106; view5.ExternalName = "Back"; view5.InternalName = "Back"; view5.Projection = new Pdf3DProjection(); view5.Projection.FieldOfView = 12.3794; Pdf3DStream _3dStream = new Pdf3DStream(); _3dStream.Views.Add(view0); _3dStream.Views.Add(view1); _3dStream.Views.Add(view2); _3dStream.Views.Add(view3); _3dStream.Views.Add(view4); _3dStream.Views.Add(view5); _3dStream.Content = u3dContent; _3dStream.DefaultViewIndex = 0; Pdf3DAnnotation _3da = new Pdf3DAnnotation(); _3da.Stream = _3dStream; PdfAnnotationAppearance appearance = new PdfAnnotationAppearance(200, 200); appearance.Graphics.DrawString("Click to activate", font, blackBrush, 50, 50); _3da.NormalAppearance = appearance; page.Annotations.Add(_3da); _3da.VisualRectangle = new PdfVisualRectangle(36, 36, 720, 540); PdfStringAppearanceOptions sao = new PdfStringAppearanceOptions(); sao.Font = font; sao.Brush = blackBrush; PdfStringLayoutOptions slo = new PdfStringLayoutOptions(); slo.Y = 585 + 18 / 2; slo.HorizontalAlign = PdfStringHorizontalAlign.Center; slo.VerticalAlign = PdfStringVerticalAlign.Middle; PdfPen blackPen = new PdfPen(new PdfRgbColor(0, 0, 0), 1); page.Graphics.DrawRectangle(blackPen, 50, 585, 120, 18); slo.X = 50 + 120 / 2; page.Graphics.DrawString("Top", sao, slo); PdfGoTo3DViewAction gotoTopView = new PdfGoTo3DViewAction(); gotoTopView.ViewIndex = 1; gotoTopView.TargetAnnotation = _3da; PdfLinkAnnotation linkGotoTopView = new PdfLinkAnnotation(); page.Annotations.Add(linkGotoTopView); linkGotoTopView.VisualRectangle = new PdfVisualRectangle(50, 585, 120, 18); linkGotoTopView.Action = gotoTopView; page.Graphics.DrawRectangle(blackPen, 190, 585, 120, 18); slo.X = 190 + 120 / 2; page.Graphics.DrawString("Side", sao, slo); PdfGoTo3DViewAction gotoSideView = new PdfGoTo3DViewAction(); gotoSideView.ViewIndex = 2; gotoSideView.TargetAnnotation = _3da; PdfLinkAnnotation linkGotoSideView = new PdfLinkAnnotation(); page.Annotations.Add(linkGotoSideView); linkGotoSideView.VisualRectangle = new PdfVisualRectangle(190, 585, 120, 18); linkGotoSideView.Action = gotoSideView; page.Graphics.DrawRectangle(blackPen, 330, 585, 120, 18); slo.X = 330 + 120 / 2; page.Graphics.DrawString("Isometric", sao, slo); PdfGoTo3DViewAction gotoIsometricView = new PdfGoTo3DViewAction(); gotoIsometricView.ViewIndex = 3; gotoIsometricView.TargetAnnotation = _3da; PdfLinkAnnotation linkGotoIsometricView = new PdfLinkAnnotation(); page.Annotations.Add(linkGotoIsometricView); linkGotoIsometricView.VisualRectangle = new PdfVisualRectangle(330, 585, 120, 18); linkGotoIsometricView.Action = gotoIsometricView; page.Graphics.DrawRectangle(blackPen, 470, 585, 120, 18); slo.X = 470 + 120 / 2; page.Graphics.DrawString("Front", sao, slo); PdfGoTo3DViewAction gotoFrontView = new PdfGoTo3DViewAction(); gotoFrontView.ViewIndex = 4; gotoFrontView.TargetAnnotation = _3da; PdfLinkAnnotation linkGotoFrontView = new PdfLinkAnnotation(); page.Annotations.Add(linkGotoFrontView); linkGotoFrontView.VisualRectangle = new PdfVisualRectangle(470, 585, 120, 18); linkGotoFrontView.Action = gotoFrontView; page.Graphics.DrawRectangle(blackPen, 610, 585, 120, 18); slo.X = 610 + 120 / 2; page.Graphics.DrawString("Back", sao, slo); PdfGoTo3DViewAction gotoBackView = new PdfGoTo3DViewAction(); gotoBackView.ViewIndex = 5; gotoBackView.TargetAnnotation = _3da; PdfLinkAnnotation linkGotoBackView = new PdfLinkAnnotation(); page.Annotations.Add(linkGotoBackView); linkGotoBackView.VisualRectangle = new PdfVisualRectangle(610, 585, 120, 18); linkGotoBackView.Action = gotoBackView; }