コード例 #1
0
        internal A.Paragraph ToParagraph()
        {
            var para = new A.Paragraph();

            para.ParagraphProperties = new A.ParagraphProperties();
            para.ParagraphProperties.Append(new A.DefaultRunProperties());

            A.Run run;

            if (istrReal.Text != null)
            {
                run = new A.Run();
                run.RunProperties = new A.RunProperties();
                run.Text          = new A.Text(istrReal.Text.Text);
                para.Append(run);
            }

            Run xrun;

            RunFont xrunRunFont;
            Bold    xrunBold;
            Italic  xrunItalic;
            Strike  xrunStrike;

            DocumentFormat.OpenXml.Spreadsheet.Color xrunColor;
            FontSize              xrunFontSize;
            Underline             xrunUnderline;
            VerticalTextAlignment xrunVertical;
            FontScheme            xrunScheme;

            string                     sFont;
            bool?                      bBold;
            bool?                      bItalic;
            bool?                      bStrike;
            double?                    fFontSize;
            UnderlineValues?           vUnderline;
            VerticalAlignmentRunValues?vVertical;
            bool                       bHasColor;
            var clrRun = new SLColorTransform(new List <Color>());
            FontSchemeValues?vScheme;

            using (var oxr = OpenXmlReader.Create(istrReal))
            {
                while (oxr.Read())
                {
                    if (oxr.ElementType == typeof(Run))
                    {
                        run = new A.Run();
                        run.RunProperties = new A.RunProperties();

                        sFont      = string.Empty;
                        bBold      = null;
                        bItalic    = null;
                        bStrike    = null;
                        fFontSize  = null;
                        vUnderline = null;
                        vVertical  = null;
                        bHasColor  = false;
                        vScheme    = null;

                        xrun = (Run)oxr.LoadCurrentElement();
                        if (xrun.RunProperties != null)
                        {
                            using (var oxrProps = OpenXmlReader.Create(xrun.RunProperties))
                            {
                                while (oxrProps.Read())
                                {
                                    if (oxrProps.ElementType == typeof(RunFont))
                                    {
                                        xrunRunFont = (RunFont)oxrProps.LoadCurrentElement();
                                        if (xrunRunFont.Val != null)
                                        {
                                            sFont = xrunRunFont.Val.Value;
                                        }
                                    }
                                    else if (oxrProps.ElementType == typeof(Bold))
                                    {
                                        xrunBold = (Bold)oxrProps.LoadCurrentElement();
                                        if (xrunBold.Val != null)
                                        {
                                            bBold = xrunBold.Val.Value;
                                        }
                                    }
                                    else if (oxrProps.ElementType == typeof(Italic))
                                    {
                                        xrunItalic = (Italic)oxrProps.LoadCurrentElement();
                                        if (xrunItalic.Val != null)
                                        {
                                            bItalic = xrunItalic.Val.Value;
                                        }
                                    }
                                    else if (oxrProps.ElementType == typeof(Strike))
                                    {
                                        xrunStrike = (Strike)oxrProps.LoadCurrentElement();
                                        if (xrunStrike.Val != null)
                                        {
                                            bStrike = xrunStrike.Val.Value;
                                        }
                                    }
                                    else if (oxrProps.ElementType == typeof(DocumentFormat.OpenXml.Spreadsheet.Color))
                                    {
                                        xrunColor =
                                            (DocumentFormat.OpenXml.Spreadsheet.Color)oxrProps.LoadCurrentElement();
                                        if (xrunColor.Rgb != null)
                                        {
                                            bHasColor = true;
                                            clrRun    = new SLColorTransform(new List <Color>());
                                            clrRun.SetColor(SLTool.ToColor(xrunColor.Rgb.Value), 0);
                                        }
                                        else if (xrunColor.Theme != null)
                                        {
                                            bHasColor = true;
                                            clrRun    = new SLColorTransform(new List <Color>());
                                            if (xrunColor.Tint != null)
                                            {
                                                clrRun.SetColor((SLThemeColorIndexValues)xrunColor.Theme.Value,
                                                                xrunColor.Tint.Value, 0);
                                            }
                                            else
                                            {
                                                clrRun.SetColor((SLThemeColorIndexValues)xrunColor.Theme.Value, 0, 0);
                                            }
                                        }
                                    }
                                    else if (oxrProps.ElementType == typeof(FontSize))
                                    {
                                        xrunFontSize = (FontSize)oxrProps.LoadCurrentElement();
                                        if (xrunFontSize.Val != null)
                                        {
                                            fFontSize = xrunFontSize.Val.Value;
                                        }
                                    }
                                    else if (oxrProps.ElementType == typeof(Underline))
                                    {
                                        xrunUnderline = (Underline)oxrProps.LoadCurrentElement();
                                        if (xrunUnderline.Val != null)
                                        {
                                            vUnderline = xrunUnderline.Val.Value;
                                        }
                                    }
                                    else if (oxrProps.ElementType == typeof(VerticalTextAlignment))
                                    {
                                        xrunVertical = (VerticalTextAlignment)oxrProps.LoadCurrentElement();
                                        if (xrunVertical.Val != null)
                                        {
                                            vVertical = xrunVertical.Val.Value;
                                        }
                                    }
                                    else if (oxrProps.ElementType == typeof(FontScheme))
                                    {
                                        xrunScheme = (FontScheme)oxrProps.LoadCurrentElement();
                                        if (xrunScheme.Val != null)
                                        {
                                            vScheme = xrunScheme.Val.Value;
                                        }
                                    }
                                }
                            }
                        }

                        if (vScheme != null)
                        {
                            if (vScheme.Value == FontSchemeValues.Major)
                            {
                                sFont = "+mj-lt";
                            }
                            else if (vScheme.Value == FontSchemeValues.Minor)
                            {
                                sFont = "+mn-lt";
                            }
                        }

                        if (bHasColor)
                        {
                            if (clrRun.IsRgbColorModelHex)
                            {
                                run.RunProperties.Append(new A.SolidFill
                                {
                                    RgbColorModelHex = clrRun.ToRgbColorModelHex()
                                });
                            }
                            else
                            {
                                run.RunProperties.Append(new A.SolidFill
                                {
                                    SchemeColor = clrRun.ToSchemeColor()
                                });
                            }
                        }

                        if (sFont.Length > 0)
                        {
                            run.RunProperties.Append(new A.LatinFont {
                                Typeface = sFont
                            });
                        }

                        if (fFontSize != null)
                        {
                            run.RunProperties.FontSize = (int)(fFontSize.Value * 100);
                        }

                        if (bBold != null)
                        {
                            run.RunProperties.Bold = bBold.Value;
                        }

                        if (bItalic != null)
                        {
                            run.RunProperties.Italic = bItalic.Value;
                        }

                        if (vUnderline != null)
                        {
                            if ((vUnderline.Value == UnderlineValues.Single) ||
                                (vUnderline.Value == UnderlineValues.SingleAccounting))
                            {
                                run.RunProperties.Underline = A.TextUnderlineValues.Single;
                            }
                            else if ((vUnderline.Value == UnderlineValues.Double) ||
                                     (vUnderline.Value == UnderlineValues.DoubleAccounting))
                            {
                                run.RunProperties.Underline = A.TextUnderlineValues.Double;
                            }
                        }

                        if (bStrike != null)
                        {
                            run.RunProperties.Strike = bStrike.Value
                                ? A.TextStrikeValues.SingleStrike
                                : A.TextStrikeValues.NoStrike;
                        }

                        if (vVertical != null)
                        {
                            if (vVertical.Value == VerticalAlignmentRunValues.Superscript)
                            {
                                run.RunProperties.Baseline = 30000;
                            }
                            else if (vVertical.Value == VerticalAlignmentRunValues.Subscript)
                            {
                                run.RunProperties.Baseline = -25000;
                            }
                            else
                            {
                                run.RunProperties.Baseline = 0;
                            }
                        }
                        else
                        {
                            run.RunProperties.Baseline = 0;
                        }

                        run.Text = new A.Text(xrun.Text.Text);
                        para.Append(run);
                    }
                }
            }

            return(para);
        }
コード例 #2
0
        // SLFont takes on extra duties so you don't have to learn more classes. Just like SLRstType.
        internal A.Paragraph ToParagraph()
        {
            var para = new A.Paragraph();

            para.ParagraphProperties = new A.ParagraphProperties();

            var defrunprops = new A.DefaultRunProperties();

            var sFont = string.Empty;

            if ((FontName != null) && (FontName.Length > 0))
            {
                sFont = FontName;
            }

            if (HasFontScheme)
            {
                if (FontScheme == FontSchemeValues.Major)
                {
                    sFont = "+mj-lt";
                }
                else if (FontScheme == FontSchemeValues.Minor)
                {
                    sFont = "+mn-lt";
                }
            }

            if (HasFontColor)
            {
                var clr = new SLColorTransform(new List <Color>());
                if ((clrFontColor.Rgb != null) && (clrFontColor.Rgb.Length > 0))
                {
                    clr.SetColor(SLTool.ToColor(clrFontColor.Rgb), 0);

                    defrunprops.Append(new A.SolidFill
                    {
                        RgbColorModelHex = clr.ToRgbColorModelHex()
                    });
                }
                else if (clrFontColor.Theme != null)
                {
                    // potential casting error? If the SLFont class was set properly, there shouldn't be errors...
                    var themeindex = (SLThemeColorIndexValues)clrFontColor.Theme.Value;
                    if (clrFontColor.Tint != null)
                    {
                        clr.SetColor(themeindex, clrFontColor.Tint.Value, 0);
                    }
                    else
                    {
                        clr.SetColor(themeindex, 0, 0);
                    }

                    defrunprops.Append(new A.SolidFill
                    {
                        SchemeColor = clr.ToSchemeColor()
                    });
                }
            }

            if (sFont.Length > 0)
            {
                defrunprops.Append(new A.LatinFont {
                    Typeface = sFont
                });
            }

            if (FontSize != null)
            {
                defrunprops.FontSize = (int)(FontSize.Value * 100);
            }

            if (Bold != null)
            {
                defrunprops.Bold = Bold.Value;
            }

            if (Italic != null)
            {
                defrunprops.Italic = Italic.Value;
            }

            if (HasUnderline)
            {
                if ((Underline == UnderlineValues.Single) || (Underline == UnderlineValues.SingleAccounting))
                {
                    defrunprops.Underline = A.TextUnderlineValues.Single;
                }
                else if ((Underline == UnderlineValues.Double) || (Underline == UnderlineValues.DoubleAccounting))
                {
                    defrunprops.Underline = A.TextUnderlineValues.Double;
                }
            }

            if (Strike != null)
            {
                defrunprops.Strike = Strike.Value ? A.TextStrikeValues.SingleStrike : A.TextStrikeValues.NoStrike;
            }

            if (HasVerticalAlignment)
            {
                if (VerticalAlignment == VerticalAlignmentRunValues.Superscript)
                {
                    defrunprops.Baseline = 30000;
                }
                else if (VerticalAlignment == VerticalAlignmentRunValues.Subscript)
                {
                    defrunprops.Baseline = -25000;
                }
                else
                {
                    defrunprops.Baseline = 0;
                }
            }

            para.ParagraphProperties.Append(defrunprops);

            return(para);
        }