Exemplo n.º 1
0
        /// <summary>
        /// Inserts this slide after a given target slide.
        /// </summary>
        /// <param name="newSlide">The new slide to insert.</param>
        /// <param name="prevSlide">The previous slide.</param>
        /// <remarks>
        /// This slide will be inserted after the slide specified as a parameter.
        /// <see href="http://startbigthinksmall.wordpress.com/2011/05/17/cloning-a-slide-using-open-xml-sdk-2-0/">Cloning a Slide using Open Xml SDK 2.0</see>
        /// </remarks>
        public static void InsertAfter(PowerpointSlide newSlide, PowerpointSlide prevSlide)
        {
            // Find the presentationPart
            var presentationPart = prevSlide.presentationPart;

            SlideIdList slideIdList = presentationPart.Presentation.SlideIdList;

            // Find the slide id where to insert our slide
            SlideId prevSlideId = null;

            foreach (SlideId slideId in slideIdList.ChildElements)
            {
                // See http://openxmldeveloper.org/discussions/development_tools/f/17/p/5302/158602.aspx
                if (slideId.RelationshipId == presentationPart.GetIdOfPart(prevSlide.slidePart))
                {
                    prevSlideId = slideId;
                    break;
                }
            }

            // Find the highest id
            uint maxSlideId = slideIdList.ChildElements.Cast <SlideId>().Max(x => x.Id.Value);

            // public override T InsertAfter<T>(T newChild, DocumentFormat.OpenXml.OpenXmlElement refChild)
            // Inserts the specified element immediately after the specified reference element.
            SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId);

            newSlideId.Id             = maxSlideId + 1;
            newSlideId.RelationshipId = presentationPart.GetIdOfPart(newSlide.slidePart);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Replaces the cells from the table (tbl).
        /// </summary>
        /// <returns>The list of remaining rows that could not be inserted, you will have to create a new slide.</returns>
        public List <Cell[]> SetRows(IList <Cell[]> rows)
        {
            PowerpointSlide slide = this.slideTemplate;

            A.Table tbl = slide.FindTable(this.tblId);

            int tblRowsCount = RowsCount(tbl);

            // done starts at 1 instead of 0 because we don't care about the first row
            // The first row contains the titles for the columns
            int done = 1;

            for (int i = 0; i < rows.Count(); i++)
            {
                Cell[] row = rows[i];

                if (done < tblRowsCount)
                {
                    // a:tr
                    A.TableRow tr = GetRow(tbl, done);

                    // a:tc
                    foreach (A.TableCell tc in tr.Descendants <A.TableCell>())
                    {
                        foreach (Cell cell in row)
                        {
                            ReplaceTag(slide, tc, cell);
                        }
                    }

                    done++;
                }
                else
                {
                    break;
                }
            }

            // Remove the last remaining rows if any
            for (int row = tblRowsCount - 1; row >= done; row--)
            {
                A.TableRow tr = GetRow(tbl, row);
                tr.Remove();
            }

            // Save the latest slide
            // Mandatory otherwise the next time SetRows() is run (on a different table)
            // the rows from the previous tables will not contained the right data (from PowerpointParagraph.ReplaceTag())
            slide.Save();

            // Computes the remaining rows if any
            List <Cell[]> remainingRows = new List <Cell[]>();

            for (int row = done - 1; row < rows.Count; row++)
            {
                remainingRows.Add(rows[row]);
            }

            return(remainingRows);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Sets a background picture for a table cell (a:tc).
        /// </summary>
        /// <remarks>
        /// <![CDATA[
        /// <a:tc>
        ///  <a:txBody>
        ///   <a:bodyPr/>
        ///   <a:lstStyle/>
        ///   <a:p>
        ///    <a:endParaRPr lang="fr-FR" dirty="0"/>
        ///   </a:p>
        ///  </a:txBody>
        ///  <a:tcPr> (TableCellProperties)
        ///   <a:blipFill dpi="0" rotWithShape="1">
        ///    <a:blip r:embed="rId2"/>
        ///    <a:srcRect/>
        ///    <a:stretch>
        ///     <a:fillRect b="12000" r="90000" t="14000"/>
        ///    </a:stretch>
        ///   </a:blipFill>
        ///  </a:tcPr>
        /// </a:tc>
        /// ]]>
        /// </remarks>
        private static void SetTableCellPropertiesWithBackgroundPicture(PowerpointSlide slide, A.TableCellProperties tcPr, Cell.BackgroundPicture backgroundPicture)
        {
            if (backgroundPicture.Content == null)
            {
                return;
            }

            ImagePart imagePart = slide.AddPicture(backgroundPicture.Content, backgroundPicture.ContentType);

            A.BlipFill blipFill = new A.BlipFill();
            A.Blip     blip     = new A.Blip()
            {
                Embed = slide.GetIdOfImagePart(imagePart)
            };
            A.SourceRectangle srcRect  = new A.SourceRectangle();
            A.Stretch         stretch  = new A.Stretch();
            A.FillRectangle   fillRect = new A.FillRectangle()
            {
                Top    = backgroundPicture.Top,
                Right  = backgroundPicture.Right,
                Bottom = backgroundPicture.Bottom,
                Left   = backgroundPicture.Left
            };
            stretch.AppendChild(fillRect);
            blipFill.AppendChild(blip);
            blipFill.AppendChild(srcRect);
            blipFill.AppendChild(stretch);
            tcPr.AppendChild(blipFill);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Replaces the cells from a table (tbl).
        /// Algorithm for a slide template containing multiple tables.
        /// </summary>
        /// <param name="slideTemplate">The slide template that contains the table(s).</param>
        /// <param name="tableTemplate">The table (tbl) to use, should be inside the slide template.</param>
        /// <param name="rows">The rows to replace the table's cells.</param>
        /// <param name="existingSlides">Existing slides created for the other tables inside the slide template.</param>
        /// <returns>The newly created slides if any.</returns>
        public static IEnumerable <PowerpointSlide> ReplaceTable_Multiple(PowerpointSlide slideTemplate, PowerpointTable tableTemplate, IList <PowerpointTable.Cell[]> rows, List <PowerpointSlide> existingSlides)
        {
            List <PowerpointSlide> slidesCreated = new List <PowerpointSlide>();

            string tag = tableTemplate.Title;

            PowerpointSlide lastSlide = slideTemplate;

            if (existingSlides.Count > 0)
            {
                lastSlide = existingSlides.Last();
            }

            PowerpointSlide lastSlideTemplate = lastSlide.Clone();

            foreach (PowerpointSlide slide in existingSlides)
            {
                PowerpointTable table = slide.FindTables(tag).First();
                List <PowerpointTable.Cell[]> remainingRows = table.SetRows(rows);
                rows = remainingRows;
            }

            // Force SetRows() at least once if there is no existingSlides
            // this means we are being called by ReplaceTable_One()
            bool loopOnce = existingSlides.Count == 0;

            while (loopOnce || rows.Count > 0)
            {
                PowerpointSlide newSlide = lastSlideTemplate.Clone();
                PowerpointTable table    = newSlide.FindTables(tag).First();
                List <PowerpointTable.Cell[]> remainingRows = table.SetRows(rows);
                rows = remainingRows;

                PowerpointSlide.InsertAfter(newSlide, lastSlide);
                lastSlide = newSlide;
                slidesCreated.Add(newSlide);

                if (loopOnce)
                {
                    loopOnce = false;
                }
            }

            lastSlideTemplate.Remove();

            return(slidesCreated);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Finds the slides matching a given note.
        /// </summary>
        /// <param name="note">Note to match the slide with.</param>
        /// <returns>The matching slides.</returns>
        public IEnumerable <PowerpointSlide> FindSlides(string note)
        {
            List <PowerpointSlide> slides = new List <PowerpointSlide>();

            for (int i = 0; i < this.SlidesCount(); i++)
            {
                PowerpointSlide      slide = this.GetSlide(i);
                IEnumerable <string> notes = slide.GetNotes();
                foreach (string tmp in notes)
                {
                    if (tmp.Contains(note))
                    {
                        slides.Add(slide);
                        break;
                    }
                }
            }

            return(slides);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Replaces a tag inside a given table cell (a:tc).
        /// </summary>
        /// <param name="slide">The PowerpointSlide.</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(PowerpointSlide slide, A.TableCell tc, Cell cell)
        {
            bool replacedAtLeastOnce = false;

            // a:p
            foreach (A.Paragraph p in tc.Descendants <A.Paragraph>())
            {
                bool replaced = PowerpointParagraph.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);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Replaces a tag inside the table (a:tbl).
        /// </summary>
        /// <param name="cell">Contains the tag, the new text and a pciture.</param>
        /// <returns><c>true</c> if a tag has been found and replaced, <c>false</c> otherwise.</returns>
        public bool ReplaceTag(Cell cell)
        {
            bool replacedAtLeastOnce = false;

            PowerpointSlide slide = this.slideTemplate;

            A.Table tbl = slide.FindTable(this.tblId);

            // a:tr
            foreach (A.TableRow tr in tbl.Descendants <A.TableRow>())
            {
                // a:tc
                foreach (A.TableCell tc in tr.Descendants <A.TableCell>())
                {
                    bool replaced = ReplaceTag(slide, tc, cell);
                    if (replaced)
                    {
                        replacedAtLeastOnce = true;
                    }
                }
            }

            return(replacedAtLeastOnce);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Replaces the cells from a table (tbl).
 /// Algorithm for a slide template containing one table.
 /// </summary>
 public static IEnumerable <PowerpointSlide> ReplaceTable_One(PowerpointSlide slideTemplate, PowerpointTable tableTemplate, IList <PowerpointTable.Cell[]> rows)
 {
     return(ReplaceTable_Multiple(slideTemplate, tableTemplate, rows, new List <PowerpointSlide>()));
 }
Exemplo n.º 9
0
 internal PowerpointTable(PowerpointSlide slideTemplate, int tblId, string title)
 {
     this.slideTemplate = slideTemplate;
     this.tblId         = tblId;
     this.Title         = title;
 }