protected override void Write(OpenXMLRenderer renderer, CodeBlock obj) { renderer.PushParagraphFormat(FormatStyle.CodeBlock); var paragraph = renderer.PushNewParagraph(); if (obj.Inline != null) { // there was a post-processor which has already processed the lines in this code block renderer.WriteChildren(obj.Inline); } else // there was no post-processor - we have to do the writing of the code lines { // original code: renderer.WriteLeafRawLines(obj); // Expand this call directly here in order to be able to include tags var lines = obj.Lines; if (lines.Lines != null) { var slices = lines.Lines; for (var i = 0; i < lines.Count; i++) { var run = renderer.Push(new Run(new DocumentFormat.OpenXml.Wordprocessing.Text() { Text = slices[i].Slice.ToString(), Space = SpaceProcessingModeValues.Preserve })); renderer.PopTo(run); if (i < lines.Count - 1) { run = renderer.Push(new Run(new Break())); renderer.PopTo(run); } } } } renderer.PopTo(paragraph); renderer.PopParagraphFormat(); }
protected override void Write(OpenXMLRenderer renderer, MathInline obj) { var text = obj.Content.Text.Substring(obj.Content.Start, obj.Content.Length); if (string.IsNullOrEmpty(text)) { return; } TexFormula formula = null; try { formula = _formulaParser.Parse(text); } catch (Exception) { return; } var mathRenderer = new MathRendering.OpenXMLWpfMathRenderer(); var mathObj = (DocumentFormat.OpenXml.OpenXmlCompositeElement)mathRenderer.Render(formula.RootAtom); renderer.Push(mathObj); renderer.PopTo(mathObj); }
protected override void Write(OpenXMLRenderer renderer, AutolinkInline obj) { var hyperlink = new Hyperlink(new Run(new DocumentFormat.OpenXml.Wordprocessing.Text(obj.Url))) { DocLocation = obj.Url }; renderer.Push(hyperlink); renderer.PopTo(hyperlink); }
private static void WriteFigureLinkLiteralInline(OpenXMLRenderer renderer, LiteralInline obj) { var text = obj.Content.ToString(); var numberPosition = renderer.FigureLinkList[renderer.CurrentFigureLinkListIndex.Value].Number.Position; var numberLength = renderer.FigureLinkList[renderer.CurrentFigureLinkListIndex.Value].Number.Count; var textBeforeNumber = text.Substring(0, numberPosition - obj.Span.Start); var textAfterNumber = text.Substring(numberPosition + numberLength - obj.Span.Start); var figureCaptionIndex = renderer.FigureLinkList[renderer.CurrentFigureLinkListIndex.Value].CaptionListIndex; var bookmarkId = GetBookmarkId(renderer, figureCaptionIndex); if (!string.IsNullOrEmpty(textBeforeNumber)) { var run = renderer.PushNewRun(); run.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Text() { Space = SpaceProcessingModeValues.Preserve, Text = textBeforeNumber }); renderer.PopTo(run); } { var bookmarkRef = "_REF" + bookmarkId.ToString(System.Globalization.CultureInfo.InvariantCulture); var field = renderer.Push(new SimpleField { Instruction = $" REF {bookmarkRef} \\h " }); var captionNumber = renderer.FigureCaptionIndices[figureCaptionIndex]; // include the number var run = renderer.PushNewRun(); run.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Text() { Space = SpaceProcessingModeValues.Preserve, Text = captionNumber.ToString() }); // renderer.ApplyStyleToRun(StyleDictionary.IdToName[FormatStyle.Link], run); // Note: This would not work - word is not formatting the text as hyperlink if the text is, like here, inside a field renderer.PopTo(field); } if (!string.IsNullOrEmpty(textAfterNumber)) { var run = renderer.PushNewRun(); run.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Text() { Space = SpaceProcessingModeValues.Preserve, Text = textAfterNumber }); renderer.PopTo(run); } }
protected override void Write(OpenXMLRenderer renderer, MathBlock obj) { string text = string.Empty; // obj.Content.Text.Substring(obj.Content.Start, obj.Content.Length); for (int i = 0; i < obj.Lines.Count; ++i) { var l = obj.Lines.Lines[i]; text += l.Slice.Text.Substring(l.Slice.Start, l.Slice.Length); } if (string.IsNullOrEmpty(text)) { return; } TexFormula formula = null; try { formula = _formulaParser.Parse(text); } catch (Exception) { return; } var mathRenderer = new MathRendering.OpenXMLWpfMathRenderer(); var mathObj = (DocumentFormat.OpenXml.OpenXmlCompositeElement)mathRenderer.Render(formula.RootAtom); var wordParagraph = renderer.PushNewParagraph(); // this is a Word paragraph renderer.Push(new Paragraph()); // Attention this is a __Math__ paragraph! renderer.Push(mathObj); renderer.PopTo(wordParagraph); }
protected override void Write(OpenXMLRenderer renderer, LineBreakInline obj) { if (obj.IsHard) { var element = renderer.Peek(); if (element is W.Paragraph) { var run = renderer.Push(new W.Run()); run.AppendChild(new W.Break()); renderer.PopTo(run); } else { var paragraph = renderer.Push(new W.Paragraph()); renderer.PopTo(paragraph); } } else // neither hard nor backslash -> but we have to add a space at least { var element = renderer.Peek(); if (element is W.Paragraph) { var run = renderer.PushNewRun(); run.AppendChild(new W.Text() { Space = SpaceProcessingModeValues.Preserve, Text = " " }); renderer.PopTo(run); } else // this should not happen, because we already are in a paragraph { var paragraph = renderer.Push(new W.Paragraph()); renderer.PopTo(paragraph); } } }
private static void WriteFigureCaptionLiteralInline(OpenXMLRenderer renderer, LiteralInline obj) { // This is probably a figure caption, and maybe the category identifier and the number needs to be replaced by special elements // Split the text in text before the number, and after the number var text = obj.Content.ToString(); var numberPosition = renderer.FigureCaptionList[renderer.CurrentFigureCaptionListIndex.Value].Number.Position; var numberLength = renderer.FigureCaptionList[renderer.CurrentFigureCaptionListIndex.Value].Number.Count; var categoryName = renderer.FigureCaptionList[renderer.CurrentFigureCaptionListIndex.Value].Category.Name; var textBeforeNumber = text.Substring(0, numberPosition - obj.Span.Start); var textAfterNumber = text.Substring(numberPosition + numberLength - obj.Span.Start); if (!string.IsNullOrEmpty(textBeforeNumber)) { var run = renderer.PushNewRun(); run.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Text() { Space = SpaceProcessingModeValues.Preserve, Text = textBeforeNumber }); renderer.PopTo(run); } var para = renderer.Peek(); var bookmarkId = GetBookmarkId(renderer, renderer.CurrentFigureCaptionListIndex.Value); { var bookmarkStart = new BookmarkStart { Id = bookmarkId.ToString(System.Globalization.CultureInfo.InvariantCulture), Name = "_REF" + bookmarkId.ToString(System.Globalization.CultureInfo.InvariantCulture) }; para.AppendChild(bookmarkStart); } { var field = renderer.Push(new SimpleField { Instruction = $" SEQ {categoryName} \\* ARABIC " }); // include the number var captionNumber = renderer.FigureCaptionIndices[renderer.CurrentFigureCaptionListIndex.Value]; var run = renderer.PushNewRun(); run.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Text() { Space = SpaceProcessingModeValues.Preserve, Text = captionNumber.ToString() }); renderer.PopTo(field); } { var bookmarkEnd = new BookmarkEnd() { Id = bookmarkId.ToString(System.Globalization.CultureInfo.InvariantCulture) }; para.AppendChild(bookmarkEnd); } if (!string.IsNullOrEmpty(textAfterNumber)) { var run = renderer.PushNewRun(); run.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Text() { Space = SpaceProcessingModeValues.Preserve, Text = textAfterNumber }); renderer.PopTo(run); } }
/// <inheritdoc/> protected override void Write(OpenXMLRenderer renderer, LinkInline link) { var url = link.GetDynamicUrl != null?link.GetDynamicUrl() ?? link.Url : link.Url; if (link.IsImage) { RenderImage(renderer, link, url); renderer.AddBookmarkIfNeccessary(link); } else // link is not an image { if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute)) { ++_linkIndex; //var nextId = renderer._wordDocument.MainDocumentPart.Parts.Count() + 1; var rId = "lkId" + _linkIndex.ToString(System.Globalization.CultureInfo.InvariantCulture); renderer._wordDocument.MainDocumentPart.AddHyperlinkRelationship(new System.Uri(url, System.UriKind.Absolute), true, rId); var hyperlink = new Hyperlink() { Id = rId }; renderer.Push(hyperlink); renderer.WriteChildren(link); foreach (var run in hyperlink.ChildElements.OfType <Run>()) { renderer.ApplyStyleToRun(StyleDictionary.IdToName[FormatStyle.Link], run); } renderer.PopTo(hyperlink); } else if (!string.IsNullOrEmpty(url) && url.StartsWith("#")) // not a well formed Uri String - then it is probably a fragment reference { if (null != renderer.FigureLinkList) { var idx = renderer.FigureLinkList.FindIndex(x => object.ReferenceEquals(x.Link, link)); if (idx >= 0) { renderer.CurrentFigureLinkListIndex = idx; } } var hyperlink = new Hyperlink() { Anchor = url.Substring(1) }; renderer.Push(hyperlink); renderer.WriteChildren(link); if (!renderer.CurrentFigureLinkListIndex.HasValue || !renderer.DoNotFormatFigureLinksAsHyperlinks) { foreach (var run in hyperlink.ChildElements.OfType <Run>()) { renderer.ApplyStyleToRun(StyleDictionary.IdToName[FormatStyle.Link], run); } } renderer.PopTo(hyperlink); renderer.CurrentFigureLinkListIndex = null; } } }
private void AddImageToBody(OpenXMLRenderer renderer, string relationshipId, ImageRenderToStreamResult streamResult, double?width, double?height) { bool changeAspect = false; long cx; long cy; if (width.HasValue && height.HasValue) { changeAspect = true; cx = (long)(width.Value * 9525); cy = (long)(height.Value * 9525); } else if (width.HasValue) { double aspectYX = streamResult.PixelsY * streamResult.DpiX / (streamResult.PixelsX * streamResult.DpiY); cx = (long)(width.Value * 9525); cy = (long)(width.Value * 9525 * aspectYX); } else if (height.HasValue) { double aspectXY = streamResult.PixelsX * streamResult.DpiY / (streamResult.PixelsY * streamResult.DpiX); cy = (long)(height.Value * 9525); cx = (long)(height.Value * 9525 * aspectXY); } else { cx = (long)(914400 * streamResult.PixelsX / streamResult.DpiX); cy = (long)(914400 * streamResult.PixelsY / streamResult.DpiY); } // limit the image size if set in the renderer. if (renderer.MaxImageWidthIn96thInch.HasValue && renderer.MaxImageHeigthIn96thInch.HasValue) { double cxmax = renderer.MaxImageWidthIn96thInch.Value * 9525; double cymax = renderer.MaxImageHeigthIn96thInch.Value * 9525; var r = Math.Min(cxmax / cx, cymax / cy); if (r < 1) { cx = (long)(r * cx); cy = (long)(r * cy); } } else if (renderer.MaxImageWidthIn96thInch.HasValue) { double cxmax = renderer.MaxImageWidthIn96thInch.Value * 9525; if (cx > cxmax) { cy = (long)(cy * (cxmax / cx)); cx = (long)cxmax; } } else if (renderer.MaxImageHeigthIn96thInch.HasValue) { double cymax = renderer.MaxImageHeigthIn96thInch.Value * 9525; if (cy > cymax) { cx = (long)(cx * (cymax / cy)); cy = (long)cymax; } } ++_figureIndex; var drawing = new DocumentFormat.OpenXml.Wordprocessing.Drawing( new DW.Inline( new DW.Extent() { Cx = cx, Cy = cy }, new DW.EffectExtent() { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L }, new DW.DocProperties() { Id = _figureIndex, Name = "Figure " + _figureIndex.ToString(System.Globalization.CultureInfo.InvariantCulture), Description = streamResult.NameHint }, new DW.NonVisualGraphicFrameDrawingProperties( new A.GraphicFrameLocks() { NoChangeAspect = !changeAspect }), new A.Graphic( new A.GraphicData( new PIC.Picture( new PIC.NonVisualPictureProperties( new PIC.NonVisualDrawingProperties() { Id = 0U, Name = streamResult.NameHint }, new PIC.NonVisualPictureDrawingProperties()), new PIC.BlipFill( new A.Blip( new A.BlipExtensionList( new A.BlipExtension() { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" }) ) { Embed = relationshipId, CompressionState = A.BlipCompressionValues.Print }, new A.Stretch( new A.FillRectangle())), new PIC.ShapeProperties( new A.Transform2D( new A.Offset() { X = 0L, Y = 0L }, new A.Extents() { Cx = cx, Cy = cy }), new A.PresetGeometry( new A.AdjustValueList() ) { Preset = A.ShapeTypeValues.Rectangle })) ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" }) ) { DistanceFromTop = 0U, DistanceFromBottom = 0U, DistanceFromLeft = 0U, DistanceFromRight = 0U }); var run = renderer.Push(new Run()); run.AppendChild(drawing); renderer.PopTo(run); }
protected override void Write(OpenXMLRenderer renderer, md.Table mdTable) { // Create an empty table. var table = renderer.Push(new Table()); // Create a TableProperties object and specify its border information. var tblProp = new TableProperties( new TableBorders( new TopBorder() { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 4 }, new BottomBorder() { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 4 }, new LeftBorder() { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 4 }, new RightBorder() { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 4 }, new InsideHorizontalBorder() { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 4 }, new InsideVerticalBorder() { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 4 } ) ); // Append the TableProperties object to the empty table. table.AppendChild <TableProperties>(tblProp); foreach (md.TableRow mdRow in mdTable) { // Create a row. var wRow = renderer.Push(new TableRow()); for (var i = 0; i < mdRow.Count; i++) { var cellObj = mdRow[i]; var mdCell = (md.TableCell)cellObj; // Create a cell. var wTableCell = renderer.Push(new TableCell()); wTableCell.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Auto })); // Specify the width property of the table cell. renderer.Write(mdCell); // Apply horizontal alignment var horizontalJustification = JustificationValues.Left; if (mdTable.ColumnDefinitions != null) { var columnIndex = mdCell.ColumnIndex < 0 || mdCell.ColumnIndex >= mdTable.ColumnDefinitions.Count ? i : mdCell.ColumnIndex; columnIndex = columnIndex >= mdTable.ColumnDefinitions.Count ? mdTable.ColumnDefinitions.Count - 1 : columnIndex; var alignment = mdTable.ColumnDefinitions[columnIndex].Alignment; if (alignment.HasValue) { switch (alignment) { case md.TableColumnAlign.Center: horizontalJustification = JustificationValues.Center; break; case md.TableColumnAlign.Right: horizontalJustification = JustificationValues.Right; break; case md.TableColumnAlign.Left: horizontalJustification = JustificationValues.Left; break; } } } if (horizontalJustification != JustificationValues.Left) { foreach (var paragraph in wTableCell.ChildElements.OfType <Paragraph>()) { var pp = paragraph.ChildElements.OfType <ParagraphProperties>().FirstOrDefault() ?? paragraph.PrependChild(new ParagraphProperties()); pp.AppendChild(new Justification { Val = horizontalJustification }); } } renderer.PopTo(wTableCell); } renderer.PopTo(wRow); } renderer.PopTo(table); }