wordpress hit counter
Deciding table column width on the fly on Presentation slides... - .Net - Development Tools - OpenXML Developer

Deciding table column width on the fly on Presentation slides...

Development Tools

Discussions about working with Open XML using a wide range of development tools

Deciding table column width on the fly on Presentation slides...

  • rated by 0 users
  • This post has 18 Replies |
  • 2 Followers
  • Hi,

    I want to show the data in tabular format on presentation file. And for this data will be fetched from data base.

    I am able to do this for fixed column width. But it look good only for less number of data columns.

    But if number of columns increases my table goes out of pptx slide.

    Is there any way to to avoid this?

    One way I think of is if I can find out the width of the slide then I can decide the width of column on the fly (instead of hard coding it.)

    Below is my code.

    ----------------------------------------------

    public static void AddTableToSlide(this SlidePart sld, DataTable ds)
            {
                Slide slide1 = sld.Slide;

                CommonSlideData commonSlideData1 = slide1.GetFirstChild<CommonSlideData>();

                ShapeTree shapeTree1 = commonSlideData1.GetFirstChild<ShapeTree>();

                GraphicFrame graphicFrame1 = new GraphicFrame();

                NonVisualGraphicFrameProperties nonVisualGraphicFrameProperties1 = new NonVisualGraphicFrameProperties();
                NonVisualDrawingProperties nonVisualDrawingProperties1 = new NonVisualDrawingProperties() { Id = (UInt32Value)2U, Name = "Table 1" };

                NonVisualGraphicFrameDrawingProperties nonVisualGraphicFrameDrawingProperties1 = new NonVisualGraphicFrameDrawingProperties();
                A.GraphicFrameLocks graphicFrameLocks1 = new A.GraphicFrameLocks() { NoGrouping = true };

                nonVisualGraphicFrameDrawingProperties1.Append(graphicFrameLocks1);
                ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties1 = new ApplicationNonVisualDrawingProperties();

                nonVisualGraphicFrameProperties1.Append(nonVisualDrawingProperties1);
                nonVisualGraphicFrameProperties1.Append(nonVisualGraphicFrameDrawingProperties1);
                nonVisualGraphicFrameProperties1.Append(applicationNonVisualDrawingProperties1);

                Transform transform1 = new Transform();
                A.Offset offset1 = new A.Offset() { X = 1524000L, Y = 1397000L };
                A.Extents extents1 = new A.Extents() { Cx = 6096000L, Cy = 1483360L };

                transform1.Append(offset1);
                transform1.Append(extents1);

                A.Graphic graphic1 = new A.Graphic();

                A.GraphicData graphicData1 = new A.GraphicData() { Uri = "http://schemas.openxmlformats.org/drawingml/2006/table" };

                A.Table table1 = new A.Table();

                A.TableProperties tableProperties1 = new A.TableProperties() { FirstRow = true, BandRow = true };
                A.TableStyleId tableStyleId1 = new A.TableStyleId();
                tableStyleId1.Text = "{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}";

                tableProperties1.Append(tableStyleId1);

                A.TableGrid tableGrid1 = new A.TableGrid();

                //Column Creation
                for (int i = 0; i < ds.Columns.Count; i++)
                {
                    A.GridColumn gridColumn = new A.GridColumn() { Width = 2032000L };
                    tableGrid1.Append(gridColumn);
                }

                table1.Append(tableProperties1);
                table1.Append(tableGrid1);

                //row Header Creation
                A.TableRow tableRow = new A.TableRow() { Height = 370840L };

                for (int i = 0; i < ds.Columns.Count; i++)
                {
                    A.TableCell tableCell = new A.TableCell();

                    A.TextBody textBody = new A.TextBody();
                    A.BodyProperties bodyProperties = new A.BodyProperties();
                    A.ListStyle listStyle = new A.ListStyle();

                    A.Paragraph paragraph = new A.Paragraph();

                    A.Run run = new A.Run();
                    A.RunProperties runProperties = new A.RunProperties() { Language = "en-US", Dirty = false, SmartTagClean = false };
                    A.Text text = new A.Text();
                    text.Text = ds.Columns[i].ColumnName;

                    run.Append(runProperties);
                    run.Append(text);
                    A.EndParagraphRunProperties endParagraphRunProperties = new A.EndParagraphRunProperties() { Language = "en-US", Dirty = false };

                    paragraph.Append(run);
                    paragraph.Append(endParagraphRunProperties);

                    textBody.Append(bodyProperties);
                    textBody.Append(listStyle);
                    textBody.Append(paragraph);
                    A.TableCellProperties tableCellProperties = new A.TableCellProperties();
                    tableCell.Append(textBody);
                    tableCell.Append(tableCellProperties);
                    tableRow.Append(tableCell);
                }

                table1.Append(tableRow);

                //data row Creation
                foreach (DataRow row in ds.Rows)
                {
                    A.TableRow tableRow1 = new A.TableRow() { Height = 370840L };

                    foreach (var item in row.ItemArray)
                    {
                        A.TableCell tableCell = new A.TableCell();

                        A.TextBody textBody = new A.TextBody();
                        A.BodyProperties bodyProperties = new A.BodyProperties();
                        A.ListStyle listStyle = new A.ListStyle();

                        A.Paragraph paragraph = new A.Paragraph();

                        A.Run run = new A.Run();
                        A.RunProperties runProperties = new A.RunProperties() { Language = "en-US", Dirty = false, SmartTagClean = false };
                        A.Text text = new A.Text();
                        text.Text = item.ToString();

                        run.Append(runProperties);
                        run.Append(text);
                        A.EndParagraphRunProperties endParagraphRunProperties = new A.EndParagraphRunProperties() { Language = "en-US", Dirty = false };

                        paragraph.Append(run);
                        paragraph.Append(endParagraphRunProperties);

                        textBody.Append(bodyProperties);
                        textBody.Append(listStyle);
                        textBody.Append(paragraph);
                        A.TableCellProperties tableCellProperties = new A.TableCellProperties();
                        tableCell.Append(textBody);
                        tableCell.Append(tableCellProperties);
                        tableRow1.Append(tableCell);
                    }
                    table1.Append(tableRow1);
                }

                graphicData1.Append(table1);

                graphic1.Append(graphicData1);

                graphicFrame1.Append(nonVisualGraphicFrameProperties1);
                graphicFrame1.Append(transform1);
                graphicFrame1.Append(graphic1);
                shapeTree1.Append(graphicFrame1);
                sld.Slide.Save();
            }

    ----------------------------------------------

    In above code you can see all the "Red" lines of codes are hard coded.

    I wan to get rid of all of them and to decide all of them on the fly.

    Thanks,

    Pranay

  • Hi Pranay,

                     One way to avoid such a thing is, have a predefined template file (*.potx file), where you will have predefined slide layouts. You can then create slides using those layouts. If you do so, you can actually get the height and width of the shape (Transform2D->Extents property). In that case you can avoid these hard coding of values.

  • Hi Pramod,

    Thanks for reply.

    Actually I am using some existing pptx fle as template. But the slide on which I need to add chart currently does not have that graphic element section (which will further have Transform2D->Extents property).

    Thus, I think If I can get that slide height and width with some Open XML API or any other way (Which I don't have idea..) then I can calculate the width and height of table as well as Transform2D->Extents property on the fly.

    Do you have any idea of how to get the width and height of a slide?

    Also, Please help me with some sample code.

    If you want I can share my sample code for your review.

    Thanks,

    Pranay

  • Hi Pranay,

                I would definitely share some sample solution which will create presentation file using a template file and insert a slide with a table...

  • Hi Pramod,

    I was waiting for your sample code..

    Thanks,

    Pranay

  • Hi Pramod,

    Seems you r quite busy. Haven't seen you on forum from last 2 days.

    I was waiting for your sample code.. As I was not able to found any thing on google... :(

    Thanks,

    Pranay

  • DynamicTransform2D.rar

    Hi Pranay,

                    I was bit busy in other tasks. I have attached a sample solution, that  creates a presentation with a slide and a table in it. You might get a generic error while opening the presentation, please ignore it.

    Thanks

  • Hi Pramodh,

    Thanks for sample code. But after looking at your code seems you have also hard coaded the numver of column and it width as:

    ---------------

    ODrawing.GridColumn gridColumn1 = new ODrawing.GridColumn() { Width = 2671600L };

               ODrawing.GridColumn gridColumn2 = new ODrawing.GridColumn() { Width = 671600L };

               ODrawing.GridColumn gridColumn3 = new ODrawing.GridColumn() { Width = 1071600L };

               ODrawing.GridColumn gridColumn4 = new ODrawing.GridColumn() { Width = 1571600L };

               ODrawing.GridColumn gridColumn5 = new ODrawing.GridColumn() { Width = 771600L };

               ODrawing.GridColumn gridColumn6 = new ODrawing.GridColumn() { Width = 1071600L };

    ---------------

    and my requirement was to get rid of these kind of hard coading.

    Is there any way we can calculate the width of columns on basis of numbers of columns a table have in database.

    Somewere like below scheduo code:

    Step 1: Get the width ad height of a slide on which table needs to be inserted.

    Step 2: Get the coun of columns from data.

    Step 3: Set gridColumn =(widht of slide from step 1) / (count of columns from step 2).

    Is it possible in ablove way.

    Pranay.........

  • Yes this is possible. If your column width is fixed, then you can actually do that.

    for example, if you have a data table with 9 columns, then you can actually get

    gridColumnWidth = Convert.Toint64((referenceShape.ShapeProperties.Transform2D.Extents.Cx)/9);

  • I will try it. And will let you know...

    Thanks,

    Pranay

  • Hi Pramod,

    I have tried your sugessation and it works thanks for this.

    I did some more experiemnts with potx file. And stuck with one problem. And here is my problem.

    I tried to add more custom layouts by following way.

    Step 1: Right click on potx file and select open.

    Step2: In opened potx file goto Slide master view.

    Step 3: Add one more custome slide layout.

    Step 4: Click on save and then close it.

    Now when I look the slideLayout1.xml of potx file in VS2010 Open XML power tool.

    The title='TableHolder' property of Place holder was removed from this layout. Thus I need to add it manually for each and every custom layout which I add.

    But I don't understand why that property removed and is there any wayout of this problem?

    Hope you got my problem.

    Pranay..

  • Hi Pranay,

               By default when you add any layout, the Title property is not set to any of the Shapes (any placeholder). Only the Name property is set when you create a new Shape/Placeholder. You can set Title property by right clicking Shape, then Properties, then Alt text and enter some value in Title field.

    If you add two new custom layouts, the application names them as Custom Layout and 1_Custom Layout. But if you changed the second template name also as Custom Layout, and the first custom layout has TableHolder, then it would consider only the second layout and first layout TableHolder would not appear for you.

  • Hi Pamodh,

    I got your point. So, the point it:

    1. Each and every layout names should be different.

    but when I tried following your step as you said (You can set Title property by right clicking Shape, then Properties, then Alt text and enter some value in Title field).

    I am not able to find Property option on shape.

    Below is the context menu which I got after selecting shape and right clicking on it.

     

  • Hi

      I meant Format Shape not the Properties. Sorry for that.

  • Hi Pramodh,

    I tried "Format Shape" also. But didn't found the option for setting Title of PlaceHolder.

Page 1 of 2 (19 items) 12