wordpress hit counter
Creating Table with SDK - without a Template - PresentationML - Formats - OpenXML Developer

Creating Table with SDK - without a Template

Formats

Discussions about working with different Open XML Formats

Creating Table with SDK - without a Template

  • rated by 0 users
  • This post has 6 Replies |
  • 1 Follower
  • I've seen and used the code to clone a table in a presentation from an existing table in a presentation.  I'm trying to use the 2.0 SDK to add the table without a template - but am running into problems.

    I can create the table, grid, cells, etc, objects. However,  I can't seem to get the correct object to add to the slide or presentation.  Once I can add it - I should be able to get a reference and add to the ShapeTree.

    Comparing the output XML appears to be correct (to my untrained eye):

    SDK generated XML:

    <a:graphicFrame xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
            <a:nvGraphicFramePr>
              <a:cNvPr id="4" name="Content Placeholder 3" />
              <a:cNvGraphicFramePr>
                <a:graphicFrameLocks noGrp="1" />
              </a:cNvGraphicFramePr>
              <p:nvPr>
                <p:ph type="tbl" idx="1" />
              </p:nvPr>
            </a:nvGraphicFramePr>
            <p:xfrm>
              <a:off x="612775" y="1600200" />
              <a:ext cx="8153400" cy="4450080" />
            </p:xfrm>
            <a:graphic>
              <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/table">
                <a:tbl>
                  <a:tblPr bandRow="1">
                    <a:tableStyleId>5c22544a-7ee6-4342-b048-85bdc9fd1c3a</a:tableStyleId>
                  </a:tblPr>
                  <a:tblGrid>
                    <a:gridCol w="1630680" />
                    <a:gridCol w="1630680" />
                    <a:gridCol w="1630680" />
                    <a:gridCol w="1630680" />
                    <a:gridCol w="1630680" />
                  </a:tblGrid>
                  <a:tr>
                    <a:tc>
                      <a:txBody>
                        <a:bodyPr />
                        <a:p>
                          <a:r>
                            <a:t>aa</a:t>
                            <a:rPr dirty="0" err="1" smtClean="0" />
                          </a:r>
                        </a:p>
                      </a:txBody>
                      <a:tcPr />
                    </a:tc>
                    <a:tc>

    PowerPoint generated XML:

         <p:graphicFrame>
            <p:nvGraphicFramePr>
              <p:cNvPr id="4" name="Content Placeholder 3"/>
              <p:cNvGraphicFramePr>
                <a:graphicFrameLocks noGrp="1"/>
              </p:cNvGraphicFramePr>
              <p:nvPr>
                <p:ph sz="quarter" idx="1"/>
              </p:nvPr>
            </p:nvGraphicFramePr>
            <p:xfrm>
              <a:off x="612775" y="1600200"/>
              <a:ext cx="8153400" cy="4450080"/>
            </p:xfrm>
            <a:graphic>
              <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/table">
                <a:tbl>
                  <a:tblPr firstRow="1" bandRow="1">
                    <a:tableStyleId>{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}</a:tableStyleId>
                  </a:tblPr>
                  <a:tblGrid>
                    <a:gridCol w="1630680"/>
                    <a:gridCol w="1630680"/>
                    <a:gridCol w="1630680"/>
                    <a:gridCol w="1630680"/>
                    <a:gridCol w="1630680"/>
                  </a:tblGrid>
                  <a:tr h="370840">
                    <a:tc>
                      <a:txBody>
                        <a:bodyPr/>
                        <a:lstStyle/>
                        <a:p>
                          <a:r>
                            <a:rPr lang="en-US" dirty="0" err="1" smtClean="0"/>
                            <a:t>Sdfsdf</a:t>
                          </a:r>
                          <a:endParaRPr lang="en-US" dirty="0"/>
                        </a:p>
                      </a:txBody>
                      <a:tcPr/>
                    </a:tc>
                    <a:tc>

    The code is not much... but it's missing something that causes the output XML to be invalid.

    var guid = new Guid("5C22544A-7EE6-4342-B048-85BDC9FD1C3A");

    var slide = presentationPart.PresentationPart.SlideParts.Last();

    var tableProperties = new TableProperties { BandRow = new BooleanValue(true) };

    tableProperties.Append(new TableStyleId(guid.ToString()));

    var tableGrid = new TableGrid();

    for (var i = 0; i < 5; i++)

    tableGrid.Append(new GridColumn { Width = 1630680 });

    var table = new Table(tableProperties, tableGrid);

    for (var i = 0; i < 5; i++)

    {

    var tableRow = new TableRow();

    tableRow.Append(CreateTextCell("aa"));

    tableRow.Append(CreateTextCell("bb"));

    tableRow.Append(CreateTextCell("cc"));

    tableRow.Append(CreateTextCell("dd"));

    tableRow.Append(CreateTextCell("ee"));

    table.Append(tableRow);

    }

    var graphicFrameDrawingProperties = new NonVisualGraphicFrameDrawingProperties

    {GraphicFrameLocks = new GraphicFrameLocks {NoGrouping = new BooleanValue(true)}};

    var appNonVisualDrawingProperties = new AppNonVisualDrawingProperties(new PlaceholderShape {Index = 1, Type = PlaceholderValues.Table});

    var extents = new Extents

    {

    Cx = 8153400,

    Cy = 4450080

    };

    var transform = new Transform(

    new Offset

    {

    X = 612775,

    Y = 1600200

    }, extents);

    var graphicFrameProperties = new NonVisualGraphicFrameProperties

    (

    new NonVisualDrawingProperties

    {

    Id = 4,

    Name = "Content Placeholder 3"

    },

    graphicFrameDrawingProperties,

    appNonVisualDrawingProperties

    );

    var graphicFrame = new GraphicFrame(graphicFrameProperties, transform)

    {

    Graphic = new Graphic(

    new GraphicData(

    tbl)

    {

    Uri = "http://schemas.openxmlformats.org/drawingml/2006/table"

    })

    };

    slide.Slide.CommonSlideData.ShapeTree.Append(graphicFrame);

    private static TableCell CreateTextCell(string text)

    {

    return new TableCell(

    new TextBody(

    new BodyProperties(),

    new Paragraph(

    new Run(

    new Text(text),

    new RunProperties

    {

    Dirty = new BooleanValue(false),

    SmartTagClean = new BooleanValue(false),

    SpellingError = new BooleanValue(true)

    }))),

    new TableCellProperties());

    }

  • I can't see much wrong with your output that I know wold cause a failure.

    But I suspect that having the Run Properties and the Text in reverse order from the specification could cause a problem.

    i.e. elements should go in order <a:rPr/> <a:t>Text</a:t> as the XSD sepcifies a SEQUENCE of child elements.

    I would have expected the SDK v2.0 'Validate' method to pick this up.

    The tools with SDK 2.0 are very helpful for this sort of thing.
    There is OpenXmlDiff  to help you identify the discrepancies,
    and - the most useful - OpenXmlClassesExplorer - to show you all the detail about an element from the spec. - including all possible ancestor trees, all legal Child elements, XSD specification (why is everything in a SEQUENCE?) and the different namespaces the element occurs in.
  • Hi

    can you send me if you have updated snippet to insert table in presentation.

    And also i want to insert an image in the presentation.

    Regards
    Sanjay
  • Here's a class I use to build a table from a DataTable...


    internal class TableHelper
    {
    private static int callCount;
    public static GraphicFrame AddTable(DataTable dt)
    {
    var tableProperties = new TableProperties { BandRow = new BooleanValue(true), FirstRow = new BooleanValue(true) };
    TableStyleId tableStyleId;
    // alternative table colors for better appearance
    switch (callCount++ % 4)
    {
    case 0:
    tableStyleId = new TableStyleId("{3B4B98B0-60AC-42C2-AFA5-B58CD77FA1E5}");
    break;
    case 1:
    tableStyleId = new TableStyleId("{C083E6E3-FA7D-4D7B-A595-EF9225AFEA82}");
    break;
    case 2:
    tableStyleId = new TableStyleId("{0E3FDE45-AF77-4B5C-9715-49D594BDF05E}");
    break;
    default:
    tableStyleId = new TableStyleId("{D27102A9-8310-4765-A935-A1911B00CA55}");
    break;
    }
    tableProperties.Append(tableStyleId);

    // make first column larger than the rest
    var tableGrid = new TableGrid();
    var width = 8153400 / (dt.Columns.Count + 1);
    for (var i = 0; i
    tableGrid.Append(new GridColumn { Width = i == 0 ? width * 2 : width });

    var table = new Table(tableProperties, tableGrid);
    var tableHeader = new TableRow { Height = 370840L };
    foreach (DataColumn dc in dt.Columns)
    tableHeader.Append(CreateCenteredTableCellWithText(dc.ColumnName));
    table.Append(tableHeader);

    foreach (DataRow dataRow in dt.Rows)
    {
    var tableRow = new TableRow { Height = 370840L };
    foreach (DataColumn dataColumn in dt.Columns)
    tableRow.Append(CreateTableCellWithText(dataRow[dataColumn].ToString()));
    table.Append(tableRow);
    }
    return CreateGraphicFrame(table);
    }

    private static TableCell CreateTableCellWithText(string text)
    {
    return new TableCell(
    new TextBody(
    new BodyProperties(),
    new ListStyle(),
    new Paragraph(
    new Run(
    new RunProperties { FontSize = 1200 },
    new Text(text)),
    new EndParagraphRunProperties { FontSize = 1200 })),
    new TableCellProperties());
    }

    private static TableCell CreateCenteredTableCellWithText(string text)
    {
    return new TableCell(
    new TextBody(
    new BodyProperties(),
    new ListStyle(),
    new Paragraph(
    new ParagraphProperties { Alignment = TextAlignValues.Center },
    new Run(
    new RunProperties { FontSize = 1200 },
    new Text(text)),
    new EndParagraphRunProperties { FontSize = 1200 })),
    new TableCellProperties { Anchor = TextAnchoringValues.Center }
    );
    }

    private static GraphicFrame CreateGraphicFrame(OpenXmlElement tbl)
    {
    return new GraphicFrame(
    new NonVisualGraphicFrameProperties(
    new NonVisualDrawingProperties { Id = (UInt32Value)3U, Name = "Content Placeholder 3" },
    new NonVisualGraphicFrameDrawingProperties(
    new GraphicFrameLocks { NoGrouping = true }),
    new AppNonVisualDrawingProperties(
    new PlaceholderShape
    {
    Size = PlaceholderSizeValues.Quarter,
    Type = PlaceholderValues.Table,
    Index = (UInt32Value)1U
    })),
    new Transform(
    new Offset { X = 612775L, Y = 1600200L },
    new Extents { Cx = 8153400L, Cy = 741680L }),
    new Graphic(
    new GraphicData(tbl) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/table" })
    );
    }
    }
  • I'm trying to get Chimera's Tablehelper class to work in a presentation project. I was wondering if there was something missing where the line reads:
    for (var i = 0; i

    By chance did the rest of the for loop iteration definition not get copied for some reason or another?

    If anyone has any updates or suggestions to getting this class to work, I'm all ears. This is exactly what I need for a task I have and would serve me a great service to get it running.

    Thanks in advance.
  • Ok, I think I got Chimer's TableHelper class to build. Now how to figure out how to use it. Anybody have a clue as to how I can run this to taret a blank (templateless) ppt doc? Here is what I have thus far but the file blows up when I try to open it.

    Any suggestions would be greatly appreciated.
    Thanks in advance!

    private void BuildDynamicTable(SlidePart slidePart1, DataTable dataTable)
    {
    Slide slide1 = slidePart1.Slide;

    CommonSlideData commonSlideData1 = slide1.GetFirstChild();

    ShapeTree shapeTree1 = commonSlideData1.GetFirstChild();

    GraphicFrame graphicFrame1 = TableHelper.AddTable(dataTable);

    shapeTree1.Append(graphicFrame1);
    }
  • Yeah I have a couple working apps that use it all the time... And any missing tag in the file causes it to blow up. Can you post the code to create the presentation?

Page 1 of 1 (7 items)