Пример #1
0
        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();
            }
        }