none
Read/ Write tags in PowerPoint using OpenXML in C# RRS feed

  • Question

  • Hi,

    I need to read tags of a particular slide or of a particular shape or all presentation level tags. Also, I need to add tags to a particular shape and to a particular slide or at presentation level. The data types are in openXML format(for example SlidePart,DocumentFormat.OpenXML.Drawing.Table etc).

    How can I do these in C#?

    Sayan Chatterjee | Senior Consultant | PricewaterhouseCoopers India
    Friday, January 15, 2010 10:58 AM

Answers

  • Hello Sayan,

    We can use the codes like following to add tags to PowerPoint elments.

            UserDefinedTagsPart userDefinedTagsPart1 = part.AddNewPart<UserDefinedTagsPart>("rId2");
            GenerateUserDefinedTagsPart1Content(userDefinedTagsPart1);

            private void GenerateUserDefinedTagsPart1Content(UserDefinedTagsPart userDefinedTagsPart1)
            {
                TagList tagList1 = new TagList();
                Tag tag1 = new Tag(){ Name = "SHAPETAG", Val = "shapeTagValue" };
                tagList1.Append(tag1);
                userDefinedTagsPart1.TagList = tagList1;
            }
    In the above codes, part in the first line can be PresentationPart or SlidePart.

    When I use OpenXML SDK 2.0 Productivity tool to review a presentation file which I have already added tag to presentation, slide, and shape, I found that the both of slide and shape's Tags appear under the Slide's scope.

    So to summarize, in OpenXML SDK, we can call

    PresentationPart .AddNewPart<UserDefinedTagsPart>() or
    SlidePart.AddNewPart<UserDefinedTagsPart>()

    to insert a tag


    Best regards,
    Ji Zhou
    MSDN Subscriber Support in Forum


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, January 18, 2010 1:04 PM
    Moderator
  • The p:tags element can exist under several different types of elements, like p:nvPRr (for shapes), p:cSld (for slides), p:presentation (for presentations). The markup is the same, but the difference is in semantic meaning (tag on a slide vs. shape). In order to look for tags on shapes instead of tags on slides you will need to first get all p:nvPRr elements and then look under those elements for the p:tags element. Make sense?


    Zeyad Rajabi (MS)
    Tuesday, January 19, 2010 6:16 PM

All replies

  • Hello Sayan,

    We can use the codes like following to add tags to PowerPoint elments.

            UserDefinedTagsPart userDefinedTagsPart1 = part.AddNewPart<UserDefinedTagsPart>("rId2");
            GenerateUserDefinedTagsPart1Content(userDefinedTagsPart1);

            private void GenerateUserDefinedTagsPart1Content(UserDefinedTagsPart userDefinedTagsPart1)
            {
                TagList tagList1 = new TagList();
                Tag tag1 = new Tag(){ Name = "SHAPETAG", Val = "shapeTagValue" };
                tagList1.Append(tag1);
                userDefinedTagsPart1.TagList = tagList1;
            }
    In the above codes, part in the first line can be PresentationPart or SlidePart.

    When I use OpenXML SDK 2.0 Productivity tool to review a presentation file which I have already added tag to presentation, slide, and shape, I found that the both of slide and shape's Tags appear under the Slide's scope.

    So to summarize, in OpenXML SDK, we can call

    PresentationPart .AddNewPart<UserDefinedTagsPart>() or
    SlidePart.AddNewPart<UserDefinedTagsPart>()

    to insert a tag


    Best regards,
    Ji Zhou
    MSDN Subscriber Support in Forum


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, January 18, 2010 1:04 PM
    Moderator
  • Hi,

    Thanks a million for your reply. Your reply answers my second point. But still I could not find way to tag to a particular shape(E.g. Table,Picture).

    I found both shape and slide tags appear under slide's scope. Am I correct?

     I used the following code to read all tags of slide into a Dictionary object.

            public Dictionary<string,string> getSlideTags(openXMLPackage.SlidePart sldPart)
            {
                DocumentFormat.OpenXml.Presentation.TagList tagList = null;
                Dictionary<string, string> sldTags = new Dictionary<string, string>();

                foreach (openXMLPackage.UserDefinedTagsPart tagPart in sldPart.UserDefinedTagsParts)
                {
                    tagList = tagPart.TagList;

                    foreach(DocumentFormat.OpenXml.Presentation.Tag tag in tagList.ChildElements)
                    {
                        if (!sldTags.ContainsKey(tag.Name.ToString()))
                            sldTags.Add(tag.Name.ToString(),tag.Val.ToString());
                    }
                }

                return sldTags;
            }

    But this code lists all tags of a slide and all tags of the shapes in that slide. I could not find any way to distinguish between slide tags and shape tags.


    Sayan Chatterjee | Senior Consultant | PricewaterhouseCoopers India
    Tuesday, January 19, 2010 11:51 AM
  • The p:tags element can exist under several different types of elements, like p:nvPRr (for shapes), p:cSld (for slides), p:presentation (for presentations). The markup is the same, but the difference is in semantic meaning (tag on a slide vs. shape). In order to look for tags on shapes instead of tags on slides you will need to first get all p:nvPRr elements and then look under those elements for the p:tags element. Make sense?


    Zeyad Rajabi (MS)
    Tuesday, January 19, 2010 6:16 PM
  • What is the status of the issue in your side, Sayan? Have you tried Zeyad's suggestion to differentiate the slide tags and shape tags? If you need future help on this, just let us know. :-)


    Best regards,
    Ji Zhou
    MSDN Subscriber Support in Forum



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, January 22, 2010 1:31 AM
    Moderator
  • Hi,

    Zeyad's suggestion was very useful. Now I can successfully read presentation level, slide level and shape level tags.

    I did not try to add tags till now. But I think now I am in a position to do that.

    Thanks a lot :)
    Sayan Chatterjee | Senior Consultant | PricewaterhouseCoopers India
    Monday, January 25, 2010 10:28 AM
  • Hello Sayan,

    We can use the codes like following to add tags to PowerPoint elments.

            UserDefinedTagsPart userDefinedTagsPart1 = part.AddNewPart<USERDEFINEDTAGSPART>("rId2");
            GenerateUserDefinedTagsPart1Content(userDefinedTagsPart1);

            private void GenerateUserDefinedTagsPart1Content(UserDefinedTagsPart userDefinedTagsPart1)
            {
                TagList tagList1 = new TagList();
                Tag tag1 = new Tag(){ Name = "SHAPETAG", Val = "shapeTagValue" };
                tagList1.Append(tag1);
                userDefinedTagsPart1.TagList = tagList1;
            }
    In the above codes, part in the first line can be PresentationPart or SlidePart.

    When I use OpenXML SDK 2.0 Productivity tool to review a presentation file which I have already added tag to presentation, slide, and shape, I found that the both of slide and shape's Tags appear under the Slide's scope.

    So to summarize, in OpenXML SDK, we can call

    PresentationPart .AddNewPart<UserDefinedTagsPart>() or
    SlidePart.AddNewPart<UserDefinedTagsPart>()

    to insert a tag


    Best regards,
    Ji Zhou
    MSDN Subscriber Support in Forum
    </USERDEFINEDTAGSPART>


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thanks for your help! It's very useful.
    Tuesday, October 5, 2010 12:45 AM