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 } }
public void FillU3DImageInAcroForm <T>(T detail) { IDictionary <String, PdfFormField> fields = _form.GetFormFields(); Type type = detail.GetType(); PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo property in properties) { PdfFormField toSet; PdfActionAttribute required = property.GetCustomAttribute <PdfActionAttribute>(); if (fields.TryGetValue(property.Name, out toSet)) { var value = property.GetValue(detail, null)?.ToString(); PdfFormField field = fields[property.Name]; if (!string.IsNullOrWhiteSpace(value)) { var widgets = field.GetWidgets(); if (widgets == null || widgets.Count == 0) { throw new ArgumentNullException($"no widgets to the field"); } PdfArray position = widgets.First().GetRectangle(); PdfPage page = field.GetWidgets().First().GetPage(); if (page == null) { throw new ArgumentNullException( $"field widget annotation is not associated with any page"); } int pageNum = _pdfDoc.GetPageNumber(page); float width = (float)(position.GetAsNumber(2).GetValue() - position.GetAsNumber(0).GetValue()); float height = (float)(position.GetAsNumber(3).GetValue() - position.GetAsNumber(1).GetValue()); var u3DInfo = new U3DInfo { Height = height, Width = width, X = (float)position.GetAsNumber(0).GetValue(), Y = (float)position.GetAsNumber(1).GetValue(), Page = pageNum, U3DFile = value }; _u3DInfos.Add(u3DInfo); _form.RemoveField(field.GetFieldName().ToString()); } else { if (required != null) { if (required.Action.Equals(ActionFlag.Required)) { throw new ArgumentNullException($"No {property.Name} image found"); } if (required.Action.Equals(ActionFlag.Optional)) { _form.RemoveField(field.GetFieldName().ToString()); continue; } if (required.Action.Equals(ActionFlag.NotAvailable)) { toSet.SetValue("N/A").SetFontSizeAutoScale().SetReadOnly(true); } } } } } // close the target pdf first: _pdfDoc?.Close(); using (var helper = new U3DImageHelper(TargetFile)) { foreach (var u3DInfo in _u3DInfos) { helper.AddU3DImage(u3DInfo); } } // reopen the target pdf: _temporaryFile = Path.Combine(Path.GetDirectoryName(TargetFile), $"temp_{DateTime.Now.Ticks}.pdf"); File.Move(TargetFile, _temporaryFile); Init(TargetFile, _temporaryFile); }