Esempio n. 1
0
        /*private static void CreateDefaultStyles(StyleDefinitionsPart stylePart)
         * {
         *  AddNewStyle(stylePart, "MyFootnoteText", "my footnote text", StyleValues.Paragraph,
         *      runStyle =>
         *      {
         *          runStyle.AppendChild(new FontSize { Val = "24" });
         *          runStyle.AppendChild(new FontSizeComplexScript { Val = "24" });
         *      },
         *      paraStyle =>
         *      {
         *          paraStyle.AppendChild(new SpacingBetweenLines {
         *              After = "0",
         *              LineRule = LineSpacingRuleValues.Auto,
         *              Line = "240"
         *          });
         *      });
         *
         *  AddNewStyle(stylePart, "MyFootnoteReference", "my footnote reference", StyleValues.Character,
         *      runStyle =>
         *      {
         *          runStyle.AppendChild(new VerticalTextAlignment() { Val = VerticalPositionValues.Superscript });
         *          runStyle.AppendChild(new FontSize { Val = "28" });
         *          runStyle.AppendChild(new FontSizeComplexScript { Val = "28" });
         *      },
         *      paraStyle =>
         *      {
         *
         *      }, basedOn: null);
         *
         *  AddNewStyle(stylePart, "MyFootnoteMarker", "my footnote marker", StyleValues.Character, runStyle =>
         *      {
         *          runStyle.AppendChild(new VerticalTextAlignment() { Val = VerticalPositionValues.Superscript });
         *          runStyle.AppendChild(new FontSize { Val = "28" });
         *          runStyle.AppendChild(new FontSizeComplexScript { Val = "28" });
         *      },
         *      paraStyle =>
         *      {
         *      }, basedOn: null);
         *
         *  AddNewStyle(stylePart, "Verse", "verse", StyleValues.Paragraph, runStyle =>
         *      {
         *          runStyle.AppendChild(new FontSize { Val = "28" });
         *          runStyle.AppendChild(new FontSizeComplexScript { Val = "28" });
         *      },
         *      paraStyle =>
         *      {
         *          paraStyle.AppendChild(new SpacingBetweenLines
         *          {
         *              After = "120",
         *              LineRule = LineSpacingRuleValues.Auto
         *          });
         *      });
         *
         *  AddNewStyle(stylePart, "MyHeading", "My heading 2", StyleValues.Paragraph,
         *  runStyle =>
         *      {
         *          runStyle.AppendChild(new FontSize { Val = "28" });
         *          runStyle.AppendChild(new FontSizeComplexScript { Val = "28" });
         *          runStyle.AppendChild(new Italic());
         *          runStyle.AppendChild(new ItalicComplexScript());
         *
         *      },
         *      paraStyle =>
         *      {
         *          paraStyle.AppendChild(new SpacingBetweenLines
         *          {
         *              After = "0",
         *              LineRule = LineSpacingRuleValues.Auto
         *          });
         *          paraStyle.AppendChild(new KeepNext { Val = OnOffValue.FromBoolean(true) });
         *      });
         *
         *  AddNewStyle(stylePart, "English", "English Text", StyleValues.Character,
         *  runStyle =>
         *      {
         *          runStyle.AppendChild(new Font { Name = "Arial" });
         *          runStyle.AppendChild(new FontSize { Val = "20" });
         *      },
         *      paraStyle =>
         *      {
         *          paraStyle.AppendChild(new Font { Name = "Arial" });
         *          paraStyle.AppendChild(new FontSize { Val = "20" });
         *      });
         *
         *  AddNewStyle(stylePart, "Arabic", "Arabic Text", StyleValues.Character,
         *  runStyle =>
         *      {
         *          runStyle.AppendChild(new Font { Name = "Arial" });
         *          runStyle.AppendChild(new FontSize { Val = "36" });
         *      },
         *      paraStyle =>
         *      {
         *          paraStyle.AppendChild(new Font { Name = "Arial" });
         *          paraStyle.AppendChild(new FontSize { Val = "36" });
         *      });
         * }*/

        private static void AddSuperscriptText(Paragraph p, string text)
        {
            Run run = new Run2(new Text2(text));

            run.RunProperties.AppendChild(
                new VerticalTextAlignment()
            {
                Val = VerticalPositionValues.Superscript
            });
            p.AppendChild(run);
        }
Esempio n. 2
0
        /// <summary>
        /// Add a note to the FootNotes part and ensure it exists.
        /// </summary>
        /// <param name="description">The description of an acronym, abbreviation, some book references, ...</param>
        /// <returns>Returns the id of the footnote reference.</returns>
        private int AddFootnoteReference(FootnotesPart fpart, StyleDefinitionsPart stylePart, string description)
        {
            var footnotesRef = 0;
            if (fpart.Footnotes == null)
            {
                // Insert a new Footnotes reference
                new Footnotes(
                    new Footnote(
                        new Paragraph(
                            new ParagraphProperties(
                                new SpacingBetweenLines()
                                    { After = "0", Before="0", Line = "240", LineRule = LineSpacingRuleValues.Auto }),
                                new Run2(
                                    new SeparatorMark())
                        )
                    ) { Type = FootnoteEndnoteValues.Separator, Id = -1 },
                    new Footnote(
                        new Paragraph(
                            new ParagraphProperties(
                                new SpacingBetweenLines()
                                    {Before="0",  After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto }),
                                new Run2(
                                    new ContinuationSeparatorMark())
                        )
                    ) { Type = FootnoteEndnoteValues.ContinuationSeparator, Id = 0 }).Save(fpart);
                footnotesRef = 1;
            }
            else
            {
                // The footnotesRef Id is a required field and should be unique. You can assign yourself some hard-coded
                // value but that's absolutely not safe. We will loop through the existing Footnote
                // to retrieve the highest Id.
                foreach (var p in fpart.Footnotes.Elements<Footnote>())
                {
                    if (p.Id.HasValue && p.Id > footnotesRef) footnotesRef = (int)p.Id.Value;
                }
                footnotesRef++;
            }

            Run2 markerRun;
            Paragraph footnotePara = new Paragraph(
                        new ParagraphProperties(
                            new ParagraphStyleId() { Val = GetStyleIdFromStyleName(stylePart, "My Footnote Text") }),
                        markerRun = new Run2(
                            new RunProperties(
                                new RunStyle() { Val = GetStyleIdFromStyleName(stylePart, "My Footnote Reference") }),
                            new FootnoteReferenceMark())
                    );
            AddText(footnotePara, description, stylePart);

            fpart.Footnotes.AppendChild(
                new Footnote(
                    footnotePara
                ) { Id = footnotesRef });

            //if (!styles.DoesStyleExists("footnote reference"))
            //{
            //    // Force the superscript style because if the footnote text style does not exists,
            //    // the rendering will be awful.
            //    markerRun.RunProperties.First().AppendChild(new VerticalTextAlignment() { Val = VerticalPositionValues.Superscript });
            //}
            fpart.Footnotes.Save();

            return footnotesRef;
        }
Esempio n. 3
0
        private static void AddText(Paragraph p, string text, StyleDefinitionsPart stylePart)
        {
            //var characters = text.ToCharArray();
            //var arabicCharacters = characters.Where(c => c > '؁' && c < 'ݭ');
            //if (arabicCharacters.Count() > 0)
            //    Debug.WriteLine(arabicCharacters.ToArray());

            var regex = new Regex(@"([^<]*)<([^>]*)>([^<]*)</([^>]*)>([^<]*)");
            var matches = regex.Matches(text);
            if (matches.Count == 0)
            {
                text = SanitizeText(text);
                if (!text.Contains('<'))
                    p.AppendChild(new Run2(new Text2(text)));
                else
                    AddText(p, text, stylePart);
            }

            foreach (Match match in matches)
            {
                var beforeText = match.Groups[1].Value;
                var tag = match.Groups[2].Value;
                var insideText = match.Groups[3].Value;
                var afterText = match.Groups[5].Value;

                if (beforeText.Length > 0)
                {
                    beforeText = SanitizeText(beforeText);
                    if (!beforeText.Contains('<'))
                        p.AppendChild(new Run2(new Text2(beforeText)));
                    else
                        AddText(p, beforeText, stylePart);

                }

                Run r = new Run2(new Text2(insideText));
                if (tag == "em")
                {
                    r.RunProperties.AppendChild(new Italic());
                    r.RunProperties.AppendChild(new ItalicComplexScript());
                }
                else if (tag == "b")
                {
                    r.RunProperties.AppendChild(new Bold());
                    r.RunProperties.AppendChild(new BoldComplexScript());
                }
                else if (tag == "sup")
                {
                    r.RunProperties.AppendChild(new VerticalTextAlignment() { Val = VerticalPositionValues.Superscript });
                }
                else if (tag == "x")
                {
                    // Arabic
                    r = new Run(new Text2(insideText));
                    r.RunProperties = new RunProperties {
                        RightToLeftText = new RightToLeftText(),
                        RunFonts = new RunFonts { Ascii = "Arial", HighAnsi = "Arial" },
                        RunStyle = new RunStyle { Val = GetStyleIdFromStyleName(stylePart, "Arabic Char") }
                    };
                }
                else if (tag == "z")
                {
                    // English
                    r = new Run(new Text2(insideText));
                    r.RunProperties = new RunProperties {
                        RunFonts = new RunFonts { Ascii = "Arial", HighAnsi = "Arial" },
                        RunStyle = new RunStyle { Val = GetStyleIdFromStyleName(stylePart, "English") }
                    };
                }

                p.AppendChild(r);

                if (afterText.Length > 0)
                {
                    afterText = SanitizeText(afterText);
                    if (!afterText.Contains('<'))
                        p.AppendChild(new Run2(new Text2(afterText)));
                    else
                        AddText(p, afterText, stylePart);
                }
            }
        }
Esempio n. 4
0
        /*private static void CreateDefaultStyles(StyleDefinitionsPart stylePart)
        {
            AddNewStyle(stylePart, "MyFootnoteText", "my footnote text", StyleValues.Paragraph,
                runStyle =>
                {
                    runStyle.AppendChild(new FontSize { Val = "24" });
                    runStyle.AppendChild(new FontSizeComplexScript { Val = "24" });
                },
                paraStyle =>
                {
                    paraStyle.AppendChild(new SpacingBetweenLines {
                        After = "0",
                        LineRule = LineSpacingRuleValues.Auto,
                        Line = "240"
                    });
                });

            AddNewStyle(stylePart, "MyFootnoteReference", "my footnote reference", StyleValues.Character,
                runStyle =>
                {
                    runStyle.AppendChild(new VerticalTextAlignment() { Val = VerticalPositionValues.Superscript });
                    runStyle.AppendChild(new FontSize { Val = "28" });
                    runStyle.AppendChild(new FontSizeComplexScript { Val = "28" });
                },
                paraStyle =>
                {

                }, basedOn: null);

            AddNewStyle(stylePart, "MyFootnoteMarker", "my footnote marker", StyleValues.Character, runStyle =>
                {
                    runStyle.AppendChild(new VerticalTextAlignment() { Val = VerticalPositionValues.Superscript });
                    runStyle.AppendChild(new FontSize { Val = "28" });
                    runStyle.AppendChild(new FontSizeComplexScript { Val = "28" });
                },
                paraStyle =>
                {
                }, basedOn: null);

            AddNewStyle(stylePart, "Verse", "verse", StyleValues.Paragraph, runStyle =>
                {
                    runStyle.AppendChild(new FontSize { Val = "28" });
                    runStyle.AppendChild(new FontSizeComplexScript { Val = "28" });
                },
                paraStyle =>
                {
                    paraStyle.AppendChild(new SpacingBetweenLines
                    {
                        After = "120",
                        LineRule = LineSpacingRuleValues.Auto
                    });
                });

            AddNewStyle(stylePart, "MyHeading", "My heading 2", StyleValues.Paragraph,
            runStyle =>
                {
                    runStyle.AppendChild(new FontSize { Val = "28" });
                    runStyle.AppendChild(new FontSizeComplexScript { Val = "28" });
                    runStyle.AppendChild(new Italic());
                    runStyle.AppendChild(new ItalicComplexScript());

                },
                paraStyle =>
                {
                    paraStyle.AppendChild(new SpacingBetweenLines
                    {
                        After = "0",
                        LineRule = LineSpacingRuleValues.Auto
                    });
                    paraStyle.AppendChild(new KeepNext { Val = OnOffValue.FromBoolean(true) });
                });

            AddNewStyle(stylePart, "English", "English Text", StyleValues.Character,
            runStyle =>
                {
                    runStyle.AppendChild(new Font { Name = "Arial" });
                    runStyle.AppendChild(new FontSize { Val = "20" });
                },
                paraStyle =>
                {
                    paraStyle.AppendChild(new Font { Name = "Arial" });
                    paraStyle.AppendChild(new FontSize { Val = "20" });
                });

            AddNewStyle(stylePart, "Arabic", "Arabic Text", StyleValues.Character,
            runStyle =>
                {
                    runStyle.AppendChild(new Font { Name = "Arial" });
                    runStyle.AppendChild(new FontSize { Val = "36" });
                },
                paraStyle =>
                {
                    paraStyle.AppendChild(new Font { Name = "Arial" });
                    paraStyle.AppendChild(new FontSize { Val = "36" });
                });
        }*/
        private static void AddSuperscriptText(Paragraph p, string text)
        {
            Run run = new Run2(new Text2(text));
            run.RunProperties.AppendChild(
                new VerticalTextAlignment() { Val = VerticalPositionValues.Superscript });
            p.AppendChild(run);
        }
Esempio n. 5
0
        /// <summary>
        /// Add a note to the FootNotes part and ensure it exists.
        /// </summary>
        /// <param name="description">The description of an acronym, abbreviation, some book references, ...</param>
        /// <returns>Returns the id of the footnote reference.</returns>
        private int AddFootnoteReference(FootnotesPart fpart, StyleDefinitionsPart stylePart, string description)
        {
            var footnotesRef = 0;

            if (fpart.Footnotes == null)
            {
                // Insert a new Footnotes reference
                new Footnotes(
                    new Footnote(
                        new Paragraph(
                            new ParagraphProperties(
                                new SpacingBetweenLines()
                {
                    After = "0", Before = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto
                }),
                            new Run2(
                                new SeparatorMark())
                            )
                        )
                {
                    Type = FootnoteEndnoteValues.Separator, Id = -1
                },
                    new Footnote(
                        new Paragraph(
                            new ParagraphProperties(
                                new SpacingBetweenLines()
                {
                    Before = "0", After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto
                }),
                            new Run2(
                                new ContinuationSeparatorMark())
                            )
                        )
                {
                    Type = FootnoteEndnoteValues.ContinuationSeparator, Id = 0
                }).Save(fpart);
                footnotesRef = 1;
            }
            else
            {
                // The footnotesRef Id is a required field and should be unique. You can assign yourself some hard-coded
                // value but that's absolutely not safe. We will loop through the existing Footnote
                // to retrieve the highest Id.
                foreach (var p in fpart.Footnotes.Elements <Footnote>())
                {
                    if (p.Id.HasValue && p.Id > footnotesRef)
                    {
                        footnotesRef = (int)p.Id.Value;
                    }
                }
                footnotesRef++;
            }

            Run2      markerRun;
            Paragraph footnotePara = new Paragraph(
                new ParagraphProperties(
                    new ParagraphStyleId()
            {
                Val = GetStyleIdFromStyleName(stylePart, "My Footnote Text")
            }),
                markerRun = new Run2(
                    new RunProperties(
                        new RunStyle()
            {
                Val = GetStyleIdFromStyleName(stylePart, "My Footnote Reference")
            }),
                    new FootnoteReferenceMark())
                );

            AddText(footnotePara, description, stylePart);

            fpart.Footnotes.AppendChild(
                new Footnote(
                    footnotePara
                    )
            {
                Id = footnotesRef
            });

            //if (!styles.DoesStyleExists("footnote reference"))
            //{
            //    // Force the superscript style because if the footnote text style does not exists,
            //    // the rendering will be awful.
            //    markerRun.RunProperties.First().AppendChild(new VerticalTextAlignment() { Val = VerticalPositionValues.Superscript });
            //}
            fpart.Footnotes.Save();

            return(footnotesRef);
        }
Esempio n. 6
0
        private static void AddText(Paragraph p, string text, StyleDefinitionsPart stylePart)
        {
            //var characters = text.ToCharArray();
            //var arabicCharacters = characters.Where(c => c > '؁' && c < 'ݭ');
            //if (arabicCharacters.Count() > 0)
            //    Debug.WriteLine(arabicCharacters.ToArray());

            var regex   = new Regex(@"([^<]*)<([^>]*)>([^<]*)</([^>]*)>([^<]*)");
            var matches = regex.Matches(text);

            if (matches.Count == 0)
            {
                text = SanitizeText(text);
                if (!text.Contains('<'))
                {
                    p.AppendChild(new Run2(new Text2(text)));
                }
                else
                {
                    AddText(p, text, stylePart);
                }
            }

            foreach (Match match in matches)
            {
                var beforeText = match.Groups[1].Value;
                var tag        = match.Groups[2].Value;
                var insideText = match.Groups[3].Value;
                var afterText  = match.Groups[5].Value;

                if (beforeText.Length > 0)
                {
                    beforeText = SanitizeText(beforeText);
                    if (!beforeText.Contains('<'))
                    {
                        p.AppendChild(new Run2(new Text2(beforeText)));
                    }
                    else
                    {
                        AddText(p, beforeText, stylePart);
                    }
                }

                Run r = new Run2(new Text2(insideText));
                if (tag == "em")
                {
                    r.RunProperties.AppendChild(new Italic());
                    r.RunProperties.AppendChild(new ItalicComplexScript());
                }
                else if (tag == "b")
                {
                    r.RunProperties.AppendChild(new Bold());
                    r.RunProperties.AppendChild(new BoldComplexScript());
                }
                else if (tag == "sup")
                {
                    r.RunProperties.AppendChild(new VerticalTextAlignment()
                    {
                        Val = VerticalPositionValues.Superscript
                    });
                }
                else if (tag == "x")
                {
                    // Arabic
                    r = new Run(new Text2(insideText));
                    r.RunProperties = new RunProperties {
                        RightToLeftText = new RightToLeftText(),
                        RunFonts        = new RunFonts {
                            Ascii = "Arial", HighAnsi = "Arial"
                        },
                        RunStyle = new RunStyle {
                            Val = GetStyleIdFromStyleName(stylePart, "Arabic Char")
                        }
                    };
                }
                else if (tag == "z")
                {
                    // English
                    r = new Run(new Text2(insideText));
                    r.RunProperties = new RunProperties {
                        RunFonts = new RunFonts {
                            Ascii = "Arial", HighAnsi = "Arial"
                        },
                        RunStyle = new RunStyle {
                            Val = GetStyleIdFromStyleName(stylePart, "English")
                        }
                    };
                }

                p.AppendChild(r);

                if (afterText.Length > 0)
                {
                    afterText = SanitizeText(afterText);
                    if (!afterText.Contains('<'))
                    {
                        p.AppendChild(new Run2(new Text2(afterText)));
                    }
                    else
                    {
                        AddText(p, afterText, stylePart);
                    }
                }
            }
        }