none
Insert text into a table cell in PPTX file using Open XML SDK RRS feed

  • Question

  • Hi, I have a Power Point template which has a table in it. I want to place some text in a specific cell of that table using C#. Can anyone suggest me some code.
    Tuesday, October 4, 2016 6:45 PM

Answers

  • >>>I have a Power Point template which has a table in it. I want to place some text in a specific cell of that table using C#. Can anyone suggest me some code.

    According to your description, you could refer to below code:



    using (PresentationDocument presentationDocument = PresentationDocument.Open("D:\\sample.pptx", true))
    {
        // Pass the presentation and the slide index
        // to the next GetAllTextInSlide method, and
        // then return the array of strings it returns. 
        // Get the presentation part of the presentation document.
        PresentationPart presentationPart = presentationDocument.PresentationPart;
        int slideIndex = 0;
    
        // Verify that the presentation part and presentation exist.
        if (presentationPart != null && presentationPart.Presentation != null)
        {
            // Get the Presentation object from the presentation part.
            Presentation presentation = presentationPart.Presentation;
    
            // Verify that the slide ID list exists.
            if (presentation.SlideIdList != null)
            {
                // Get the collection of slide IDs from the slide ID list.
                DocumentFormat.OpenXml.OpenXmlElementList slideIds =
                    presentation.SlideIdList.ChildElements;
    
                // If the slide ID is in range...
                if (slideIndex < slideIds.Count)
                {
                    // Get the relationship ID of the slide.
                    string slidePartRelationshipId = (slideIds[slideIndex] as SlideId).RelationshipId;
    
                    // Get the specified slide part from the relationship ID.
                    SlidePart slidePart =
                        (SlidePart)presentationPart.GetPartById(slidePartRelationshipId);
                    if (slidePart.Slide != null)
                    {
                        A.Table table = slidePart.Slide.Descendants<A.Table>().First();
    
                        A.TableRow tableRow1 = table.Elements<A.TableRow>().ElementAt(1);
    
                        A.TableCell tableCell1 = tableRow1.GetFirstChild<A.TableCell>();
    
                        A.TextBody textBody1 = tableCell1.GetFirstChild<A.TextBody>();
    
                        A.Paragraph paragraph1 = textBody1.GetFirstChild<A.Paragraph>();
    
                        A.EndParagraphRunProperties endParagraphRunProperties1 = paragraph1.GetFirstChild<A.EndParagraphRunProperties>();
    
                        A.Run run1 = new A.Run();
                        A.RunProperties runProperties1 = new A.RunProperties() { Language = "en-US", AlternativeLanguage = "zh-CN", Dirty = false };
                        A.Text text1 = new A.Text();
                        text1.Text = "John";
    
                        run1.Append(runProperties1);
                        run1.Append(text1);
                        paragraph1.InsertBefore(run1, endParagraphRunProperties1);
                        endParagraphRunProperties1.Dirty = false;
    
                    }
                }
            }
            presentationPart.Presentation.Save();
        }
    }

    The result:

    In addition I suggest that you could install Open XML SDK 2.5 Productivity Tool. The Open XML SDK 2.5 Productivity Tool for Microsoft Office provides a number of features designed to improve your productivity and accelerate your learning while working with the SDK and Open XML files. Features include the ability to generate Open XML SDK 2.5 source code based on document content, compare source and target Open XML documents to reveal differences and to generate source code to create the target from the source, validate documents, and display documentation for the Open XML SDK 2.5 Classes, the ECMA376v1 standard, and the Microsoft Office implementation notes. 

    Thanks for your understanding.

    • Proposed as answer by David_JunFeng Friday, October 14, 2016 9:35 AM
    • Marked as answer by David_JunFeng Monday, October 17, 2016 2:32 PM
    Wednesday, October 5, 2016 3:19 AM

All replies

  • >>>I have a Power Point template which has a table in it. I want to place some text in a specific cell of that table using C#. Can anyone suggest me some code.

    According to your description, you could refer to below code:



    using (PresentationDocument presentationDocument = PresentationDocument.Open("D:\\sample.pptx", true))
    {
        // Pass the presentation and the slide index
        // to the next GetAllTextInSlide method, and
        // then return the array of strings it returns. 
        // Get the presentation part of the presentation document.
        PresentationPart presentationPart = presentationDocument.PresentationPart;
        int slideIndex = 0;
    
        // Verify that the presentation part and presentation exist.
        if (presentationPart != null && presentationPart.Presentation != null)
        {
            // Get the Presentation object from the presentation part.
            Presentation presentation = presentationPart.Presentation;
    
            // Verify that the slide ID list exists.
            if (presentation.SlideIdList != null)
            {
                // Get the collection of slide IDs from the slide ID list.
                DocumentFormat.OpenXml.OpenXmlElementList slideIds =
                    presentation.SlideIdList.ChildElements;
    
                // If the slide ID is in range...
                if (slideIndex < slideIds.Count)
                {
                    // Get the relationship ID of the slide.
                    string slidePartRelationshipId = (slideIds[slideIndex] as SlideId).RelationshipId;
    
                    // Get the specified slide part from the relationship ID.
                    SlidePart slidePart =
                        (SlidePart)presentationPart.GetPartById(slidePartRelationshipId);
                    if (slidePart.Slide != null)
                    {
                        A.Table table = slidePart.Slide.Descendants<A.Table>().First();
    
                        A.TableRow tableRow1 = table.Elements<A.TableRow>().ElementAt(1);
    
                        A.TableCell tableCell1 = tableRow1.GetFirstChild<A.TableCell>();
    
                        A.TextBody textBody1 = tableCell1.GetFirstChild<A.TextBody>();
    
                        A.Paragraph paragraph1 = textBody1.GetFirstChild<A.Paragraph>();
    
                        A.EndParagraphRunProperties endParagraphRunProperties1 = paragraph1.GetFirstChild<A.EndParagraphRunProperties>();
    
                        A.Run run1 = new A.Run();
                        A.RunProperties runProperties1 = new A.RunProperties() { Language = "en-US", AlternativeLanguage = "zh-CN", Dirty = false };
                        A.Text text1 = new A.Text();
                        text1.Text = "John";
    
                        run1.Append(runProperties1);
                        run1.Append(text1);
                        paragraph1.InsertBefore(run1, endParagraphRunProperties1);
                        endParagraphRunProperties1.Dirty = false;
    
                    }
                }
            }
            presentationPart.Presentation.Save();
        }
    }

    The result:

    In addition I suggest that you could install Open XML SDK 2.5 Productivity Tool. The Open XML SDK 2.5 Productivity Tool for Microsoft Office provides a number of features designed to improve your productivity and accelerate your learning while working with the SDK and Open XML files. Features include the ability to generate Open XML SDK 2.5 source code based on document content, compare source and target Open XML documents to reveal differences and to generate source code to create the target from the source, validate documents, and display documentation for the Open XML SDK 2.5 Classes, the ECMA376v1 standard, and the Microsoft Office implementation notes. 

    Thanks for your understanding.

    • Proposed as answer by David_JunFeng Friday, October 14, 2016 9:35 AM
    • Marked as answer by David_JunFeng Monday, October 17, 2016 2:32 PM
    Wednesday, October 5, 2016 3:19 AM
  • What is A in

      A.Table table = slidePart.Slide.Descendants<A.Table>().First();
    

    Wednesday, February 20, 2019 6:48 AM