コード例 #1
0
        public virtual Paragraph InsertParagraphAfterSelf(string text, bool trackChanges, Formatting formatting)
        {
            XElement newParagraph = new XElement
                                    (
                XName.Get("p", DocX.w.NamespaceName), new XElement(XName.Get("pPr", DocX.w.NamespaceName)), HelperFunctions.FormatInput(text, formatting.Xml)
                                    );

            if (trackChanges)
            {
                newParagraph = Paragraph.CreateEdit(EditType.ins, DateTime.Now, newParagraph);
            }

            Xml.AddAfterSelf(newParagraph);
            XElement newlyInserted = Xml.ElementsAfterSelf().First();

            Paragraph p = new Paragraph(Document, newlyInserted, -1);

            return(p);
        }
コード例 #2
0
        public virtual Paragraph InsertParagraph(string text, bool trackChanges, Formatting formatting)
        {
            var newParagraph = new XElement
                               (
                XName.Get("p", DocX.w.NamespaceName), new XElement(XName.Get("pPr", DocX.w.NamespaceName)), HelperFunctions.FormatInput(text, formatting.Xml)
                               );

            if (trackChanges)
            {
                newParagraph = HelperFunctions.CreateEdit(EditType.ins, DateTime.Now, newParagraph);
            }

            this.Xml.Add(newParagraph);

            var newParagraphAdded = new Paragraph(this.Document, newParagraph, 0);
            var cell = this as Cell;

            if (cell != null)
            {
                newParagraphAdded.PackagePart = cell.PackagePart;
            }
            else
            {
                var docx = this as DocX;
                if (docx != null)
                {
                    newParagraphAdded.PackagePart = this.Document.PackagePart;
                }
                else
                {
                    var footer = this as Footer;
                    if (footer != null)
                    {
                        newParagraphAdded.PackagePart = footer.PackagePart;
                    }
                    else
                    {
                        var header = this as Header;
                        if (header != null)
                        {
                            newParagraphAdded.PackagePart = header.PackagePart;
                        }
                        else
                        {
                            newParagraphAdded.PackagePart = this.Document.PackagePart;
                        }
                    }
                }
            }

            this.SetParentContainer(newParagraphAdded);

            return(newParagraphAdded);
        }
コード例 #3
0
        public static Formatting Parse(XElement rPr, Formatting formatting = null)
        {
            if (formatting == null)
            {
                formatting = new Formatting();
            }

            if (rPr == null)
            {
                return(formatting);
            }

            // Build up the Formatting object.
            foreach (XElement option in rPr.Elements())
            {
                switch (option.Name.LocalName)
                {
                case "lang":
                    formatting.Language = new CultureInfo(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName), null) ?? option.GetAttribute(XName.Get("eastAsia", DocX.w.NamespaceName), null) ?? option.GetAttribute(XName.Get("bidi", DocX.w.NamespaceName)));
                    break;

                case "spacing":
                    formatting.Spacing = Double.Parse(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName))) / 20.0;
                    break;

                case "position":
                    formatting.Position = Int32.Parse(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName))) / 2;
                    break;

                case "kern":
                    formatting.Kerning = Int32.Parse(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName))) / 2;
                    break;

                case "w":
                    formatting.PercentageScale = Int32.Parse(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName)));
                    break;

                case "sz":
                    formatting.Size = Double.Parse(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName))) / 2;
                    break;

                case "rFonts":
                    var fontName = option.GetAttribute(XName.Get("ascii", DocX.w.NamespaceName), null)
                                   ?? option.GetAttribute(XName.Get("hAnsi", DocX.w.NamespaceName), null)
                                   ?? option.GetAttribute(XName.Get("cs", DocX.w.NamespaceName), null)
                                   ?? option.GetAttribute(XName.Get("hint", DocX.w.NamespaceName), null)
                                   ?? option.GetAttribute(XName.Get("eastAsia", DocX.w.NamespaceName), null);

                    formatting.FontFamily = (fontName != null)
                                    ? new Font(fontName)
                                    : (formatting.FontFamily == null) ?
                                            new Font("Calibri") : formatting.FontFamily;
                    break;

                case "color":
                    try
                    {
                        var color = option.GetAttribute(XName.Get("val", DocX.w.NamespaceName));
                        formatting.FontColor = (color == "auto") ? Color.Black : ColorTranslator.FromHtml(string.Format("#{0}", color));
                    }
                    catch (Exception)
                    {
                        // ignore
                    }
                    break;

                case "vanish":
                    formatting._hidden = true;
                    break;

                case "b":
                    formatting.Bold = option.GetAttribute(XName.Get("val", DocX.w.NamespaceName)) != "0";
                    break;

                case "i":
                    formatting.Italic = true;
                    break;

                case "highlight":
                    switch (option.GetAttribute(XName.Get("val", DocX.w.NamespaceName)))
                    {
                    case "yellow":
                        formatting.Highlight = NET.Highlight.yellow;
                        break;

                    case "green":
                        formatting.Highlight = NET.Highlight.green;
                        break;

                    case "cyan":
                        formatting.Highlight = NET.Highlight.cyan;
                        break;

                    case "magenta":
                        formatting.Highlight = NET.Highlight.magenta;
                        break;

                    case "blue":
                        formatting.Highlight = NET.Highlight.blue;
                        break;

                    case "red":
                        formatting.Highlight = NET.Highlight.red;
                        break;

                    case "darkBlue":
                        formatting.Highlight = NET.Highlight.darkBlue;
                        break;

                    case "darkCyan":
                        formatting.Highlight = NET.Highlight.darkCyan;
                        break;

                    case "darkGreen":
                        formatting.Highlight = NET.Highlight.darkGreen;
                        break;

                    case "darkMagenta":
                        formatting.Highlight = NET.Highlight.darkMagenta;
                        break;

                    case "darkRed":
                        formatting.Highlight = NET.Highlight.darkRed;
                        break;

                    case "darkYellow":
                        formatting.Highlight = NET.Highlight.darkYellow;
                        break;

                    case "darkGray":
                        formatting.Highlight = NET.Highlight.darkGray;
                        break;

                    case "lightGray":
                        formatting.Highlight = NET.Highlight.lightGray;
                        break;

                    case "black":
                        formatting.Highlight = NET.Highlight.black;
                        break;
                    }
                    break;

                case "strike":
                    formatting.StrikeThrough = NET.StrikeThrough.strike;
                    break;

                case "dstrike":
                    formatting.StrikeThrough = NET.StrikeThrough.doubleStrike;
                    break;

                case "u":
                    formatting.UnderlineStyle = HelperFunctions.GetUnderlineStyle(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName)));
                    try
                    {
                        var color = option.GetAttribute(XName.Get("color", DocX.w.NamespaceName));
                        if (!string.IsNullOrEmpty(color))
                        {
                            formatting.UnderlineColor = System.Drawing.ColorTranslator.FromHtml(string.Format("#{0}", color));
                        }
                    }
                    catch (Exception)
                    {
                        // ignore
                    }
                    break;

                case "vertAlign": //script
                    var script = option.GetAttribute(XName.Get("val", DocX.w.NamespaceName), null);
                    formatting.Script = (Script)Enum.Parse(typeof(Script), script);
                    break;

                case "caps":
                    formatting.CapsStyle = NET.CapsStyle.caps;
                    break;

                case "smallCaps":
                    formatting.CapsStyle = NET.CapsStyle.smallCaps;
                    break;

                case "shd":
                    var fill = option.GetAttribute(XName.Get("fill", DocX.w.NamespaceName));
                    if (!string.IsNullOrEmpty(fill))
                    {
                        formatting.Shading = System.Drawing.ColorTranslator.FromHtml(string.Format("#{0}", fill));
                    }
                    break;

                case "bdr":
                    var borderSize  = BorderSize.one;
                    var borderColor = Color.Black;
                    var borderSpace = 0;
                    var borderStyle = BorderStyle.Tcbs_single;

                    var bdrColor = option.Attribute(XName.Get("color", DocX.w.NamespaceName));
                    if ((bdrColor != null) && (bdrColor.Value != "auto"))
                    {
                        borderColor = System.Drawing.ColorTranslator.FromHtml(string.Format("#{0}", bdrColor.Value));
                    }
                    var size = option.Attribute(XName.Get("sz", DocX.w.NamespaceName));
                    if (size != null)
                    {
                        var sizeValue = System.Convert.ToSingle(size.Value);
                        if (sizeValue == 2)
                        {
                            borderSize = BorderSize.one;
                        }
                        else if (sizeValue == 4)
                        {
                            borderSize = BorderSize.two;
                        }
                        else if (sizeValue == 6)
                        {
                            borderSize = BorderSize.three;
                        }
                        else if (sizeValue == 8)
                        {
                            borderSize = BorderSize.four;
                        }
                        else if (sizeValue == 12)
                        {
                            borderSize = BorderSize.five;
                        }
                        else if (sizeValue == 18)
                        {
                            borderSize = BorderSize.six;
                        }
                        else if (sizeValue == 24)
                        {
                            borderSize = BorderSize.seven;
                        }
                        else if (sizeValue == 36)
                        {
                            borderSize = BorderSize.eight;
                        }
                        else if (sizeValue == 48)
                        {
                            borderSize = BorderSize.nine;
                        }
                        else
                        {
                            borderSize = BorderSize.one;
                        }
                    }
                    var space = option.Attribute(XName.Get("space", DocX.w.NamespaceName));
                    if (space != null)
                    {
                        borderSpace = System.Convert.ToInt32(space.Value);
                    }
                    var bdrStyle = option.Attribute(XName.Get("val", DocX.w.NamespaceName));
                    if (bdrStyle != null)
                    {
                        borderStyle = (BorderStyle)Enum.Parse(typeof(BorderStyle), "Tcbs_" + bdrStyle.Value);
                    }

                    formatting.Border = new Border(borderStyle, borderSize, borderSpace, borderColor);
                    break;

                case "rStyle":
                    var style = option.GetAttribute(XName.Get("val", DocX.w.NamespaceName), null);
                    formatting.StyleName = style;
                    break;

                default:
                    break;
                }
            }

            return(formatting);
        }