Example #1
0
        /// <summary>
        /// Replaces a text (tag) by another inside the slide.
        /// </summary>
        /// <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.</param>
        /// <param name="replacementType">The type of replacement to perform.</param>
        public void ReplaceTagWithHtml(string tag, string newText, ReplacementType replacementType, string fontName = null, int fontSize = 0)
        {
            // Adjust ASCII characters
            //newText = newText.Replace("\n \n", "<br>");
            //newText = newText.Replace("\n", "");

            // Prepare hyperlinks for the slide
            var hyperlinks = ParseHttpUrls(newText);

            foreach (var item in hyperlinks)
            {
                bool   isExternal;
                Uri    uri;
                string relId;
                if (item.Value.Contains("http"))
                {
                    isExternal = true;
                    uri        = new Uri(item.Value, UriKind.Absolute);
                    relId      = item.Key;
                }
                else
                {
                    isExternal = false;
                    uri        = new Uri(item.Value, UriKind.Relative);
                    relId      = item.Key;
                }
                AddHyperlinkRelationship(uri, isExternal, relId);
            }

            // Process paragraphs
            foreach (A.Paragraph p in this.slidePart.Slide.Descendants <A.Paragraph>())
            {
                switch (replacementType)
                {
                case ReplacementType.Global:
                    PptxParagraph.ReplaceTagWithHtml(p, tag, newText, fontName, fontSize, hyperlinks);
                    break;

                case ReplacementType.NoTable:
                    var tables = p.Ancestors <A.Table>();
                    if (!tables.Any())
                    {
                        // If the paragraph has no table ancestor
                        PptxParagraph.ReplaceTagWithHtml(p, tag, newText, fontName, fontSize, hyperlinks);
                    }
                    break;
                }
            }

            this.Save();
        }