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();
        }
Example #2
0
        /// <summary>
        /// Replaces a tag inside a given table cell (a:tc).
        /// </summary>
        /// <param name="slide">The PptxSlide.</param>
        /// <param name="tc">The table cell (a:tc).</param>
        /// <param name="cell">Contains the tag, the new text and a picture.</param>
        /// <returns><c>true</c> if a tag has been found and replaced, <c>false</c> otherwise.</returns>
        private static bool ReplaceTag(PptxSlide slide, A.TableCell tc, Cell cell)
        {
            bool replacedAtLeastOnce = false;

            // a:p
            foreach (A.Paragraph p in tc.Descendants <A.Paragraph>())
            {
                bool replaced = PptxParagraph.ReplaceTag(p, cell.Tag, cell.NewText);
                if (replaced)
                {
                    replacedAtLeastOnce = true;

                    // a:tcPr
                    if (cell.Picture != null)
                    {
                        A.TableCellProperties tcPr = tc.GetFirstChild <A.TableCellProperties>();
                        SetTableCellPropertiesWithBackgroundPicture(slide, tcPr, cell.Picture);
                    }
                }
            }

            return(replacedAtLeastOnce);
        }
Example #3
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 ReplaceTag(string tag, string newText, ReplacementType replacementType)
        {
            foreach (A.Paragraph p in this.slidePart.Slide.Descendants <A.Paragraph>())
            {
                switch (replacementType)
                {
                case ReplacementType.Global:
                    PptxParagraph.ReplaceTag(p, tag, newText);
                    break;

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

            this.Save();
        }
Example #4
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 ReplaceTagWithHyperlink(string tag, string newText, ReplacementType replacementType, string relationshipId, string fontName = "Calibri", int fontSize = 800)
        {
            foreach (A.Paragraph p in this.slidePart.Slide.Descendants <A.Paragraph>())
            {
                switch (replacementType)
                {
                case ReplacementType.Global:
                    PptxParagraph.ReplaceTagWithHyperlink(p, tag, newText, relationshipId, fontName, fontSize);
                    break;

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

            this.Save();
        }
Example #5
0
        /// <summary>
        /// Gets all the notes associated with the slide.
        /// </summary>
        /// <returns>All the notes.</returns>
        /// <remarks>
        /// <see href="http://msdn.microsoft.com/en-us/library/office/gg278319.aspx">Working with Notes Slides</see>
        /// </remarks>
        public IEnumerable <string> GetNotes()
        {
            var notes = new List <string>();

            if (this.slidePart.NotesSlidePart != null)
            {
                notes.AddRange(this.slidePart.NotesSlidePart.NotesSlide.Descendants <A.Paragraph>().Select(p => PptxParagraph.GetTexts(p)));
            }
            return(notes);
        }
Example #6
0
        /// <summary>
        /// Gets the slide title if any.
        /// </summary>
        /// <returns>The title or an empty string.</returns>
        public string GetTitle()
        {
            string title = string.Empty;

            // Find the title if any
            Shape titleShape = this.slidePart.Slide.Descendants <Shape>().FirstOrDefault(sp => IsShapeATitle(sp));

            if (titleShape != null)
            {
                title = string.Join(" ", titleShape.Descendants <A.Paragraph>().Select(p => PptxParagraph.GetTexts(p)));
            }

            return(title);
        }
Example #7
0
 /// <summary>
 /// Gets all the texts found inside the slide.
 /// </summary>
 /// <returns>The list of texts detected into the slide.</returns>
 /// <remarks>
 /// Some strings inside the array can be empty, this happens when all A.Text from a paragraph are empty
 /// <see href="http://msdn.microsoft.com/en-us/library/office/cc850836">How to: Get All the Text in a Slide in a Presentation</see>
 /// </remarks>
 public IEnumerable <string> GetTexts()
 {
     return(this.slidePart.Slide.Descendants <A.Paragraph>().Select(p => PptxParagraph.GetTexts(p)));
 }