Run GenerateTextRun(string Content, WordTable.ColumnStyle Style = WordTable.ColumnStyle.NONE) { RunProperties Props = CurrentRun.Copy() ?? new RunProperties(); if (Style.HasFlag(WordTable.ColumnStyle.BOLD)) { if (!Props.HasChild <Bold>()) { Props.AppendChild(new Bold()); } else { Props.GetFirstChild <Bold>().Val.Value = true; } } if (Style.HasFlag(WordTable.ColumnStyle.ITALIC)) { if (!Props.HasChild <Italic>()) { Props.AppendChild(new Italic()); } else { Props.GetFirstChild <Italic>().Val.Value = true; } } if (Style.HasFlag(WordTable.ColumnStyle.UNDERLINE) && !Props.HasChild <Underline>()) { if (!Props.HasChild <Underline>()) { Props.AppendChild(new Underline()); } else { Props.GetFirstChild <Underline>().Val.Value = UnderlineValues.Single; } } Run NewRun = new Run(); NewRun.AppendChild(Props); string[] Lines = Content.Replace("\r", "").Split(new[] { '\n' }); for (int i = 0; i < Lines.Length; i++) { NewRun.AppendChild(new Text(Lines[i]) { Space = SpaceProcessingModeValues.Preserve }); if (i < Lines.Length - 1) { NewRun.AppendChild(new Break()); } } return(NewRun); }
OpenXmlElement GenerateContent(Body Body, DataType DataType, object Obj, TokenParameters TokenParams) { switch (DataType) { case DataType.None: return(null); case DataType.EmbeddedData: { EmbeddedData ObjData = (EmbeddedData)Obj; switch (ObjData.Type.MediaType) { case "image/jpeg": case "image/jpg": case "image/png": { Image Img = ObjData.ParseImageData(); Utils.GetSizeInEMU(Img, out long W, out long H); const string WidthName = "width"; const string HeightName = "height"; if (TokenParams.Defined(WidthName) && TokenParams.Defined(HeightName)) { W = Utils.MilimeterToEmu(TokenParams.Get <int>(WidthName)); H = Utils.MilimeterToEmu(TokenParams.Get <int>(HeightName)); } else if (TokenParams.Defined(WidthName)) { long NewW = Utils.MilimeterToEmu(TokenParams.Get <int>(WidthName)); Utils.Scale((float)NewW / W, ref W, ref H); } else if (TokenParams.Defined(HeightName)) { long NewH = Utils.MilimeterToEmu(TokenParams.Get <int>(HeightName)); Utils.Scale((float)NewH / H, ref W, ref H); } GenerateImagePart(Img, out string ImageID); return(new Run(new RunProperties(), GenerateDrawing(ImageID, W, H))); } case "text/csv": { string CSVData = ObjData.ParseStringData(); string[] CSVLines = CSVData.Replace("\r", "").Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); // https://www.codeproject.com/Articles/1043875/Create-Word-table-using-OpenXML-and-Csharp-Without Table Tab = new Table(); //TableProperties TblPr = new TableProperties(); //Tab.AppendChild(TblPr); uint TableBorder = 1; BorderValues BorderType = BorderValues.BasicThinLines; TableProperties Props = new TableProperties(); Tab.AppendChild(Props); PageDimensions Dim; GetPageDimensions(Body, out Dim); Props.AppendChild(new TableStyle() { Val = "0" }); Props.AppendChild(new TableWidth() { Width = Dim.FillWidth.ToString(), Type = TableWidthUnitValues.Dxa }); Props.AppendChild(new TableIndentation() { Width = 0, Type = TableWidthUnitValues.Dxa }); TableBorders Borders = new TableBorders(); Borders.AppendChild(new TopBorder() { Val = new EnumValue <BorderValues>(BorderType), Size = TableBorder }); Borders.AppendChild(new BottomBorder() { Val = new EnumValue <BorderValues>(BorderType), Size = TableBorder }); Borders.AppendChild(new LeftBorder() { Val = new EnumValue <BorderValues>(BorderType), Size = TableBorder }); Borders.AppendChild(new RightBorder() { Val = new EnumValue <BorderValues>(BorderType), Size = TableBorder }); Borders.AppendChild(new InsideHorizontalBorder() { Val = new EnumValue <BorderValues>(BorderType), Size = TableBorder }); Borders.AppendChild(new InsideVerticalBorder() { Val = new EnumValue <BorderValues>(BorderType), Size = TableBorder }); Props.AppendChild(Borders); Props.AppendChild(new TableLayout() { Type = TableLayoutValues.Fixed }); foreach (var Line in CSVLines) { string[] Columns = Line.Split(new[] { ';' }); TableCell[] Cells = new TableCell[Columns.Length - 1]; for (int i = 0; i < Columns.Length - 1; i++) { string Column = Columns[i]; if (Column.StartsWith("\"") && Column.EndsWith("\"")) { Column = Column.Substring(1, Column.Length - 2); } Cells[i] = GenerateCell(Column); } Tab.AppendChild(GenerateRow(Cells)); } //Body.ReplaceChild(Tab, Root); return(Tab); } case "text/plain": return(GenerateTextRun(ObjData.ParseStringData())); default: throw new NotImplementedException(); } } case DataType.String: { WordTable.ColumnStyle TokenParamStyle = WordTable.ColumnStyle.NONE; if (TokenParams.Defined("bold")) { TokenParamStyle |= WordTable.ColumnStyle.BOLD; } if (TokenParams.Defined("italic")) { TokenParamStyle |= WordTable.ColumnStyle.ITALIC; } if (TokenParams.Defined("underline")) { TokenParamStyle |= WordTable.ColumnStyle.UNDERLINE; } WordTable.ColumnStyle Style = WordTableContext.GetCurrent()?.GetCurrentColumn()?.ParseStyle() ?? TokenParamStyle; return(GenerateTextRun(Obj.ToString(), Style)); } case DataType.PageBreak: return(new Run(new RunProperties(), new Break() { Type = BreakValues.Page })); default: throw new NotImplementedException(); } }