/// <summary>
        /// Converts a paragraphes style to css.
        /// </summary>
        /// <param name="s">
        /// The style.
        /// </param>
        /// <returns>
        /// A css string.
        /// </returns>
        private static string ParagraphStyleToCss(ParagraphStyle s)
        {
            var css = new StringBuilder();

            if (s.FontFamily != null)
            {
                css.Append(string.Format("font-family:{0};", s.FontFamily));
            }

            css.Append(string.Format("font-size:{0}pt;", s.FontSize));
            if (s.Bold)
            {
                css.Append(string.Format("font-weight:bold;"));
            }

            return(css.ToString());
        }
        /// <summary>
        /// Converts a paragraphes style to css.
        /// </summary>
        /// <param name="s">
        /// The style.
        /// </param>
        /// <returns>
        /// A css string.
        /// </returns>
        private static string ParagraphStyleToCss(ParagraphStyle s)
        {
            var css = new StringBuilder();
            if (s.FontFamily != null)
            {
                css.Append(string.Format("font-family:{0};", s.FontFamily));
            }

            css.Append(string.Format("font-size:{0}pt;", s.FontSize));
            if (s.Bold)
            {
                css.Append(string.Format("font-weight:bold;"));
            }

            return css.ToString();
        }
        /// <summary>
        /// The create style.
        /// </summary>
        /// <param name="ps">
        /// The ps.
        /// </param>
        /// <param name="styleID">
        /// The style id.
        /// </param>
        /// <param name="styleName">
        /// The style name.
        /// </param>
        /// <param name="basedOnStyleID">
        /// The based on style id.
        /// </param>
        /// <param name="nextStyleID">
        /// The next style id.
        /// </param>
        /// <param name="isDefault">
        /// The is default.
        /// </param>
        /// <param name="isCustomStyle">
        /// The is custom style.
        /// </param>
        /// <returns>
        /// </returns>
        private static Style CreateStyle(
            ParagraphStyle ps,
            string styleID,
            string styleName,
            string basedOnStyleID,
            string nextStyleID,
            bool isDefault = false,
            bool isCustomStyle = true)
        {
            // todo: add font to FontTable?
            var rPr = new StyleRunProperties();

            // http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.color.aspx
            var color = new Color { Val = ps.TextColor.ToString().Trim('#').Substring(2) };
            rPr.Append(color);

            // http://msdn.microsoft.com/en-us/library/cc850848.aspx
            rPr.Append(new RunFonts { Ascii = ps.FontFamily, HighAnsi = ps.FontFamily });
            rPr.Append(new FontSize { Val = new StringValue((ps.FontSize * 2).ToString(CultureInfo.InvariantCulture)) });
            rPr.Append(
                new FontSizeComplexScript
                    {
                       Val = new StringValue((ps.FontSize * 2).ToString(CultureInfo.InvariantCulture))
                    });

            if (ps.Bold)
            {
                rPr.Append(new Bold());
            }

            if (ps.Italic)
            {
                rPr.Append(new Italic());
            }

            var pPr = new StyleParagraphProperties();
            var spacingBetweenLines2 = new SpacingBetweenLines
                {
                    After = string.Format(CultureInfo.InvariantCulture, "{0}", ps.SpacingAfter * 20),
                    Before = string.Format(CultureInfo.InvariantCulture, "{0}", ps.SpacingBefore * 20),
                    Line = string.Format(CultureInfo.InvariantCulture, "{0}", ps.LineSpacing * 240),
                    LineRule = LineSpacingRuleValues.Auto
                };
            var indentation = new Indentation
                {
                    Left = string.Format(CultureInfo.InvariantCulture, "{0}", ps.LeftIndentation * 20),
                    Right = string.Format(CultureInfo.InvariantCulture, "{0}", ps.RightIndentation * 20)
                };
            var contextualSpacing1 = new ContextualSpacing();

            pPr.Append(spacingBetweenLines2);
            pPr.Append(contextualSpacing1);
            pPr.Append(indentation);

            // StyleRunProperties styleRunProperties7 = new StyleRunProperties();
            // RunFonts runFonts8 = new RunFonts() { Ascii = "Verdana", HighAnsi = "Verdana" };
            // Color color7 = new Color() { Val = "000000" };

            // styleRunProperties7.Append(runFonts8);
            // styleRunProperties7.Append(color7);

            // http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.style.aspx
            var style = new Style
                {
                    Default = new OnOffValue(isDefault),
                    CustomStyle = new OnOffValue(isCustomStyle),
                    StyleId = styleID,
                    Type = StyleValues.Paragraph
                };

            style.Append(new Name { Val = styleName });
            if (basedOnStyleID != null)
            {
                style.Append(new BasedOn { Val = basedOnStyleID });
            }

            var rsid = new Rsid();

            // style.Append(rsid);
            var primaryStyle = new PrimaryStyle();
            style.Append(primaryStyle);
            if (nextStyleID != null)
            {
                style.Append(new NextParagraphStyle { Val = nextStyleID });
            }

            style.Append(rPr);
            style.Append(pPr);
            return style;
        }
        /// <summary>
        /// Creates a paragraph.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="style">The style.</param>
        /// <returns>A paragraph.</returns>
        private System.Windows.Documents.Paragraph CreateParagraph(string text, ParagraphStyle style)
        {
            var run = new Run { Text = text };
            if (style != null)
            {
                SetStyle(run, style);
            }

            return new System.Windows.Documents.Paragraph(run);
        }
        /// <summary>
        /// The set style.
        /// </summary>
        /// <param name="run">
        /// The run.
        /// </param>
        /// <param name="s">
        /// The s.
        /// </param>
        private static void SetStyle(TextElement run, ParagraphStyle s)
        {
            run.FontFamily = new FontFamily(s.FontFamily);
            run.FontSize = s.FontSize;
            run.FontWeight = s.Bold ? FontWeights.Bold : FontWeights.Normal;
            FontStyle fontStyle = FontStyles.Normal;
            if (s.Italic)
            {
                fontStyle = FontStyles.Italic;
            }

            run.FontStyle = fontStyle;
        }
Exemplo n.º 6
0
 /// <summary>
 /// Sets a paragraph style.
 /// </summary>
 /// <param name="style">The style.</param>
 /// <param name="ps">The ps.</param>
 private static void SetStyle(Style style, ParagraphStyle ps)
 {
     style.Font.Name = ps.FontFamily;
     style.Font.Size = Unit.FromPoint(ps.FontSize);
     style.Font.Bold = ps.Bold;
     style.Font.Italic = ps.Italic;
     style.Font.Color = ToMigraDocColor(ps.TextColor);
     style.ParagraphFormat.PageBreakBefore = ps.PageBreakBefore;
     style.ParagraphFormat.LeftIndent = Unit.FromPoint(ps.LeftIndentation);
     style.ParagraphFormat.RightIndent = Unit.FromPoint(ps.RightIndentation);
     style.ParagraphFormat.LineSpacing = Unit.FromPoint(ps.LineSpacing);
     style.ParagraphFormat.SpaceBefore = Unit.FromPoint(ps.SpacingBefore);
     style.ParagraphFormat.SpaceAfter = Unit.FromPoint(ps.SpacingAfter);
 }