Exemplo n.º 1
0
        private static string ExtractTextFromParagraph(MDShapeText mdShape, Drawing.Paragraph paragraph)
        {
            StringBuilder paragraphText = new StringBuilder();

            foreach (var run in paragraph.Descendants <Drawing.Run>())
            {
                var schemeColor = run.Descendants <Drawing.SchemeColor>().FirstOrDefault();
                var color       = run.Descendants <Drawing.RgbColorModelHex>().FirstOrDefault();
                mdShape.IsMultiCode = color != null && color.Val != null && color.Val == "8CF4F2";

                mdShape.IsCode = schemeColor != null && schemeColor.Val == Drawing.SchemeColorValues.Accent5;

                var paragraphProperties = paragraph.Descendants <Drawing.ParagraphProperties>().FirstOrDefault();
                mdShape.IndentCount = paragraphProperties != null && paragraphProperties.Level != null ? paragraphProperties.Level.Value : 0;

                var text = run.Descendants <Drawing.Text>().FirstOrDefault();
                if (text != null)
                {
                    if (mdShape.IsCode)
                    {
                        paragraphText.AppendFormat("`{0}`", text.Text);
                    }
                    else
                    {
                        paragraphText.Append(text.Text);
                    }
                }
            }

            return(paragraphText.ToString());
        }
Exemplo n.º 2
0
        public void AddTextRun_CustomAttributes_Valid()
        {
            Shape rectangle = PowerpointHelper.CreateRectangleShape(50, 50, 50, 50);

            D.Paragraph paragraph = PowerpointHelper.AddParagraph(rectangle, "text");

            int oldNbTextRun = paragraph.Descendants <D.Run>().Count();

            PowerpointHelper.AddTextRun(paragraph, "text", 400, "487315", true);

            var errors = validator.Validate(paragraph);

            Assert.IsTrue(errors.Count() == 0);
            Assert.IsTrue(paragraph.Descendants <D.Run>().Count() - oldNbTextRun == 1);
        }
Exemplo n.º 3
0
 private static void ReplaceWordRun(OXD.Paragraph paragraph, OXD.Run initRun, OXD.Run finalRun)
 {
     if (null != paragraph.Descendants <OXD.Run>())
     {
         List <OXD.Run> runs = paragraph.Descendants <OXD.Run>().ToList();
         foreach (var run in runs)
         {
             if (initRun != run)
             {
                 paragraph.RemoveChild <OXD.Run>(run);
             }
         }
         paragraph.ReplaceChild <OXD.Run>(finalRun, initRun);
     }
 }
Exemplo n.º 4
0
        public static void FormatTitle(SlidePart slidePart)
        {
            if (slidePart == null)
            {
                throw new ArgumentNullException("presentationDocument");
            }

            if (slidePart.Slide != null)
            {
                Shape titleShape = slidePart.Slide.Descendants <Shape>().Where(d => IsTitleShape(d) && d.InnerText.Contains(Constants._INPUTSLIDE_)).FirstOrDefault();
                if (titleShape != default(Shape))
                {
                    D.Paragraph paragraph = titleShape.TextBody.Descendants <D.Paragraph>().FirstOrDefault();
                    if (paragraph != default(D.Paragraph))
                    {
                        D.Run run = paragraph.Descendants <D.Run>().FirstOrDefault();
                        run.Text = new D.Text(Constants._OUTPUTSLIDE_);
                        paragraph.InsertAt(new D.ParagraphProperties()
                        {
                            Alignment = D.TextAlignmentTypeValues.Center
                        }, 0);
                        run.RunProperties.InsertAt(new D.ComplexScriptFont()
                        {
                            CharacterSet = -78, PitchFamily = 2, Typeface = "Beirut"
                        }, 0);
                        run.RunProperties.InsertAt(new D.LatinFont()
                        {
                            CharacterSet = -78, PitchFamily = 2, Typeface = "Beirut"
                        }, 0);
                    }
                }
            }
        }
        private void InsertPageDetasils(SlidePart tempSlidepart, GanttChartAutomation.GanttReportObject objganttReport)
        {
            Xdr.Paragraph pageDetails    = _shpPageDetails.TextBody.Descendants <Xdr.Paragraph>().FirstOrDefault();
            Xdr.Run       pagedetails    = pageDetails.Descendants <Xdr.Run>().FirstOrDefault();
            Xdr.Paragraph ContactDetails = _shpContactDetails.TextBody.Descendants <Xdr.Paragraph>().FirstOrDefault();
            Xdr.Run       contactdetails = ContactDetails.Descendants <Xdr.Run>().FirstOrDefault();

            //Xdr.Paragraph StatDetails = _shpStatsDetail.TextBody.Descendants<Xdr.Paragraph>().FirstOrDefault();
            //Xdr.Run statDetails = StatDetails.Descendants<Xdr.Run>().FirstOrDefault();
            Xdr.Paragraph HeadingIntegrated = _shpHeadingIntegrated.TextBody.Descendants <Xdr.Paragraph>().FirstOrDefault();
            Xdr.Run       headingIntegrated = HeadingIntegrated.Descendants <Xdr.Run>().FirstOrDefault();

            Xdr.Paragraph TitleDetails   = _shpTitle.TextBody.Descendants <Xdr.Paragraph>().FirstOrDefault();
            Xdr.Run       titleDetails   = TitleDetails.Descendants <Xdr.Run>().FirstOrDefault();
            Xdr.Paragraph HeadingDetails = _shpHeading.TextBody.Descendants <Xdr.Paragraph>().FirstOrDefault();
            Xdr.Run       headingDetails = HeadingDetails.Descendants <Xdr.Run>().FirstOrDefault();


            Xdr.Paragraph FooterDetails = _shpFooterDetails.TextBody.Descendants <Xdr.Paragraph>().FirstOrDefault();
            Xdr.Run       footerDetails = FooterDetails.Descendants <Xdr.Run>().FirstOrDefault();

            pagedetails.Text.Text       = objganttReport.PageDetails;
            contactdetails.Text.Text    = objganttReport.RIdetails;
            headingIntegrated.Text.Text = objganttReport.Title2;
            titleDetails.Text.Text      = objganttReport.Title1;
            headingDetails.Text.Text    = objganttReport.Title3;
            //statDetails.Text.Text = objganttReport.StatusDate.ToString();
            footerDetails.Text.Text = objganttReport.FooterText;
        }
Exemplo n.º 6
0
        public void AddTextRun_InvalidColor_Throws()
        {
            Shape rectangle = PowerpointHelper.CreateRectangleShape(50, 50, 50, 50);

            D.Paragraph paragraph = PowerpointHelper.AddParagraph(rectangle, "text");

            int oldNbTextRun = paragraph.Descendants <D.Run>().Count();

            PowerpointHelper.AddTextRun(paragraph, "text", 100, "invalid");
        }
Exemplo n.º 7
0
        public void AddTextRun_NegativeFontSize_Throws()
        {
            Shape rectangle = PowerpointHelper.CreateRectangleShape(50, 50, 50, 50);

            D.Paragraph paragraph = PowerpointHelper.AddParagraph(rectangle, "text");

            int oldNbTextRun = paragraph.Descendants <D.Run>().Count();

            PowerpointHelper.AddTextRun(paragraph, "text", -100);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Returns all the texts found inside a given paragraph.
        /// </summary>
        /// <remarks>
        /// If all A.Text in the given paragraph are empty, returns an empty string.
        /// </remarks>
        internal static string GetTexts(A.Paragraph p)
        {
            StringBuilder concat = new StringBuilder();

            foreach (A.Text t in p.Descendants <A.Text>())
            {
                concat.Append(t.Text);
            }
            return(concat.ToString());
        }
Exemplo n.º 9
0
        /// <summary>
        /// Replaces text tag with hyperlink id. Hyperlink identified by Id hes to be a part of the slide.
        /// </summary>
        /// <param name="p">The paragraph (a:p).</param>
        /// <param name="tag">The tag to replace by newText, if null or empty do nothing; tag is a regex string.</param>
        /// <param name="newText">The new text to replace the tag with, if null replaced by empty string and not visible.</param>
        /// <param name="relationshipId">Hyperlink relationship Id. Relationship has to be existing on slide level.</param>
        /// <param name="fontName">Font name</param>
        /// <param name="fontSize">Font size. E.g. 800 is 8pt (small) font. If value is less than 100 it will be multiplied by 100 to keep up with PPT notation.</param>
        /// <returns></returns>
        internal static bool ReplaceTagWithHyperlink(A.Paragraph p, string tag, string newText, string relationshipId, string fontName = "Calibri", int fontSize = 800)
        {
            bool replaced = false;

            if (string.IsNullOrEmpty(tag))
            {
                return(replaced);
            }

            if (newText == null)
            {
                newText = string.Empty;
            }
            newText = RemoveInvalidXMLChars(newText);

            while (true)
            {
                // Search for the tag
                Match match = Regex.Match(GetTexts(p), tag);
                if (!match.Success)
                {
                    break;
                }

                p.RemoveAllChildren(); // remove exisitng children then add new

                A.HyperlinkOnClick link = new A.HyperlinkOnClick()
                {
                    Id = relationshipId
                };
                A.Run r = new A.Run();
                r.RunProperties = new A.RunProperties();
                A.Text at = new A.Text(newText);
                r.RunProperties.AppendChild(link);
                r.AppendChild(at);
                p.Append(r);

                var run = p.Descendants <A.Run>();
                foreach (var item in run)
                {
                    item.RunProperties.RemoveAllChildren <A.LatinFont>();
                    var latinFont = new A.LatinFont();
                    latinFont.Typeface = fontName;
                    item.RunProperties.AppendChild(latinFont);
                    item.RunProperties.FontSize = (fontSize > 99) ? fontSize : fontSize * 100; // e.g. translate value 8 (Power Point UI font size) to 800 for API
                }

                replaced = true;
            }

            return(replaced);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Gets all the TextIndex for a given paragraph.
        /// </summary>
        private static List <TextIndex> GetTextIndexList(A.Paragraph p)
        {
            List <TextIndex> texts = new List <TextIndex>();

            StringBuilder concat = new StringBuilder();

            foreach (A.Text t in p.Descendants <A.Text>())
            {
                int startIndex = concat.Length;
                texts.Add(new TextIndex(t, startIndex));
                concat.Append(t.Text);
            }

            return(texts);
        }
        public static a.Paragraph SetFont(this a.Paragraph paragraph, Font font, Color?fontColor)
        {
            var runs      = paragraph.Descendants <a.Run>();
            var runsCount = 0;

            foreach (var run in runs)
            {
                run.SetFont(font, fontColor);
                runsCount++;
            }
            if (runsCount == 0)
            {
                var r = new a.Run().Init("", font, fontColor);
                paragraph.Append(r);
            }
            return(paragraph);
        }
Exemplo n.º 12
0
 private static void ModifyPowerPointParagraphTextContent(OXD.Paragraph paragraph, string txt)
 {
     if (null != paragraph)
     {
         OXD.Run run = paragraph.Descendants <OXD.Run>().FirstOrDefault();
         if (null != run)
         {
             OXD.Run  final_run  = run.CloneNode(true) as OXD.Run;
             OXD.Text text       = final_run.Descendants <OXD.Text>().FirstOrDefault();
             OXD.Text final_text = (null == text ? new OXD.Text() : text.CloneNode(true) as OXD.Text);
             final_text.Text = txt;
             try
             { final_run.ReplaceChild <OXD.Text>(final_text, text); }
             catch
             { throw; }
             ReplaceWordRun(paragraph, run, final_run);
         }
         else
         {
             run = new OXD.Run();
         }
     }
 }
Exemplo n.º 13
0
        private static string ParseText(Drawing.Paragraph paragraph, bool isInMultiLineCode)
        {
            List <string> texts = new List <string>();

            var runs = paragraph.Descendants <Drawing.Run>();

            foreach (var run in runs)
            {
                bool isCode      = run.Descendants <Drawing.SchemeColor>().Any(sc => sc.Val == Drawing.SchemeColorValues.Accent5);
                bool isMultiCode = run.Descendants <Drawing.RgbColorModelHex>().Any(cm => cm.Val == "8CF4F2");

                var text = run.Descendants <Drawing.Text>().FirstOrDefault();
                if (text != null)
                {
                    string textToInsert = text.Text;
                    if (!isInMultiLineCode)
                    {
                        if (isCode && !string.IsNullOrWhiteSpace(textToInsert))
                        {
                            textToInsert = string.Format("**{0}**", textToInsert.Trim());
                        }
                    }

                    texts.Add(textToInsert);
                }
            }

            string result = string.Join("", texts);

            foreach (var replace in textReplace)
            {
                result = result.Replace(replace.Key, replace.Value);
            }

            return(result);
        }
Exemplo n.º 14
0
 public static bool IsEmpty(this A.Paragraph aParagraph)
 {
     return(!aParagraph.Descendants <A.Text>().Any());
 }
Exemplo n.º 15
0
        /// <summary>
        /// Replaces a tag inside a paragraph (a:p) with parsed HTML
        /// </summary>
        /// <param name="p">The paragraph (a:p).</param>
        /// <param name="tag">The tag to replace by newText, if null or empty do nothing; tag is a regex string.</param>
        /// <param name="newText">The new text to replace the tag with, if null replaced by empty string and not visible.</param>
        /// <param name="fontName">Font name</param>
        /// <param name="fontSize">Font size. E.g. 800 is 8pt (small) font. If value is less than 100 it will be multiplied by 100 to keep up with PPT notation.</param>
        /// <param name="hyperlinks">URL Relationships dictionary. Relationship has to be defined on slide level.</param>
        /// <returns><c>true</c> if a tag has been found and replaced, <c>false</c> otherwise.</returns>
        internal static bool ReplaceTagWithHtml(A.Paragraph p, string tag, string newText, string fontName = null, int fontSize = 0, IDictionary <string, string> hyperlinks = null)
        {
            newText = CorrectUnhandledHtmlTags(newText); // e.g. deal with ul/li html tags
            bool isFirstLine = true;                     // avoiding unintentional empty line at the begining of the text
            bool replaced    = false;

            string[] closingTags = new string[] { "div", "p" }; // tags that force line break in PPTX paragraph

            if (string.IsNullOrEmpty(tag))
            {
                return(replaced);
            }

            if (newText == null)
            {
                newText = string.Empty;
            }
            newText = RemoveInvalidXMLChars(newText);

            while (true)
            {
                // Search for the tag
                Match match = Regex.Match(GetTexts(p), tag);
                if (!match.Success)
                {
                    break;
                }

                p.RemoveAllChildren(); // // remove exisitng children then add new

                HtmlParser hp = new HtmlParser(newText);
                MariGold.HtmlParser.IHtmlNode nodes = null;

                try
                {
                    nodes = hp.FindBodyOrFirstElement();
                }
                catch
                {
                    Console.WriteLine(String.Format("WARNING: HTML is empty or HTML schema has errors. Parsed HTML[]: [{0}]", newText));
                }

                while (nodes != null)
                {
                    foreach (var item in nodes.Children)
                    {
                        bool  skipLineBreak = false;
                        A.Run r             = new A.Run();
                        r.RunProperties = new A.RunProperties();

                        if (item.Html.Contains("<b>") || item.Html.Contains("<strong>"))
                        {
                            r.RunProperties.Bold = new DocumentFormat.OpenXml.BooleanValue(true);
                        }
                        if (item.Html.Contains("<i>") || item.Html.Contains("<em>"))
                        {
                            r.RunProperties.Italic = new DocumentFormat.OpenXml.BooleanValue(true);
                        }
                        if (item.Html.Contains("<u>") || item.Html.Contains("text-decoration: underline"))
                        {
                            r.RunProperties.Underline = new DocumentFormat.OpenXml.EnumValue <A.TextUnderlineValues>(A.TextUnderlineValues.Dash);
                        }
                        if (item.Html.Contains("<a"))
                        {
                            string uriId = null;
                            try
                            {
                                string url = PptxSlide.ParseHttpUrls(item.Html).First().Value;
                                uriId = hyperlinks.Where(q => q.Value == url).FirstOrDefault().Key;
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("URL is no available");
                            }
                            if (uriId != null)
                            {
                                A.HyperlinkOnClick link = new A.HyperlinkOnClick()
                                {
                                    Id = uriId
                                };
                                r.RunProperties.AppendChild(link);
                            }
                        }

                        A.Text at = new A.Text(PptxTemplater.TextTransformationHelper.HtmlToPlainTxt(item.InnerHtml) + " "); // clear not interpreted html tags
                        if (at.InnerText.Trim() == "" && isFirstLine)
                        {
                            at          = new A.Text(); // avoid excessive new lines
                            isFirstLine = false;
                        }
                        r.AppendChild(at);
                        p.Append(r);

                        // LINE BREAK -- if outer tag is div add line break
                        if (closingTags.Contains(item.Parent.Tag) && skipLineBreak == false)
                        {
                            p.Append(new A.Break());
                        }
                    }

                    // remove parsed html part
                    newText = newText.Substring(nodes.Html.Length);
                    if (newText.Trim() == "")
                    {
                        break;
                    }
                    hp    = new HtmlParser(newText);
                    nodes = hp.FindBodyOrFirstElement();
                }

                var run = p.Descendants <A.Run>();
                foreach (var item in run)
                {
                    if (fontName != null)
                    {
                        item.RunProperties.RemoveAllChildren <A.LatinFont>();
                        var latinFont = new A.LatinFont();
                        latinFont.Typeface = fontName;
                        item.RunProperties.AppendChild(latinFont);
                    }
                    if (fontSize > 0)
                    {
                        item.RunProperties.FontSize = (fontSize > 99) ? fontSize : fontSize * 100; // e.g. translate value 8 (Power Point UI font size) to 800 for API
                    }
                }

                replaced = true;
            }

            return(replaced);
        }