This project has moved. For the latest updates, please go here.

Formatting Tables

Feb 3, 2012 at 9:39 AM

I've been trying to use your library to produce word reports with images and tables. 

Although I'm able to insert images, I cannot set the size of the Cell and therefore of the column.

How can I effectively set the column with? At which level do I set it? Row, Paragraph or Cell? 

I tried to set table autofit property to ColumnWidth or Content with no luck.

My last effort was to format the entire table after it was populated:

for (int row = 0; row < table.Rows.Count; row++){
   for (int col = 0; col < table.Rows[row].Cells.Count; col++){

       table.Rows[row].Cells[col].MarginBottom = 0;       

       table.Rows[row].Cells[col].MarginLeft = 0;

       table.Rows[row].Cells[col].MarginRight = 0;

       table.Rows[row].Cells[col].MarginTop = 0;

       table.Rows[row].Paragraphs[col].IndentationAfter = 0;

       table.Rows[row].Paragraphs[col].IndentationBefore = 0;

       table.Rows[row].Paragraphs[col].IndentationFirstLine = 0;

       table.Rows[row].Paragraphs[col].IndentationHanging = 0;
       table.Rows[row].Cells[col].Paragraphs[0].IndentationAfter = 0;

       table.Rows[row].Cells[col].Paragraphs[0].IndentationBefore = 0;

       table.Rows[row].Cells[col].Paragraphs[0].IndentationFirstLine = 0;

       table.Rows[row].Cells[col].Paragraphs[0].IndentationHanging = 0;

       table.Rows[row].Paragraphs[col].LineSpacing = 0;

       table.Rows[row].Paragraphs[col].LineSpacingAfter = 0;

       table.Rows[row].Paragraphs[col].LineSpacingBefore = 0;

 

       // Commented this lines has it always raises an exception

       //table.Rows[row].Cells[col].Paragraphs[0].LineSpacing = 0;

       //table.Rows[row].Cells[col].Paragraphs[0].LineSpacingAfter = 0;

       //table.Rows[row].Cells[col].Paragraphs[0].LineSpacingBefore = 0;

       
       double width=0;

       switch(col){

           case 0 : width=1; break;

           case 1 : width=2; break;

           case 2 : width=1; break;

           case 3 : width=1; break;

           case 4 : width=1;break; 

           case 5 : width=1;break; 

          case 14: width=1; break; 

           case 15: width = 1; break;

           case 16: width=1; break;

           default: width = 1; break;

        }

       table.Rows[row].Cells[col].Width = width;    

 }

}

Thanks in advance!

CP

Developer
Feb 6, 2012 at 10:10 PM

You can try using AutoFit property.

 

                Table tabelaAkcji = saldoPoczatkowe.InsertTableAfterSelf(rowsCountAll, 4);
                tabelaAkcji.AutoFit = AutoFit.Window;    // AutoFit.Multipleoptions here
                tabelaAkcji.Design = TableDesign.LightListAccent3;
                tabelaAkcji.Alignment = Alignment.center;

 

or/and you can try:

                tabelaAkcji.Rows[0].Cells[0].Width = 50;
                tabelaAkcji.Rows[0].Cells[1].Width = 70;
                tabelaAkcji.Rows[0].Cells[2].Width = 70;
                tabelaAkcji.Rows[0].Cells[3].Width = 300;

But it's really hard to find the right values for me. It's pixels or so.

Feb 7, 2012 at 8:56 AM

As I said before, none of those options seems to work properly.

I tried to set the width in all table cells and this doesn't have any effect on the layout.

Can anyone confirm that Cell's width is in pixels?

Thanks

CP

Developer
Feb 7, 2012 at 9:49 AM

AutoFit.Window and alike works fine for me. It also works when I do changes with Width (but without AutoFit turned on). It takes some times to get things right.

Developer
Feb 7, 2012 at 10:34 AM

Here's a bit of my testing. It shows different behavior for different approaches. Not very elegant and not always showing what's up but should show you what you need.

using (DocX documentWord = DocX.Create("C:\\Test.docx")) {
                const int columns = 3;
                const int rows = 5;
                // Table based on width only set per column
                Paragraph paragraph = documentWord.InsertParagraph("Table based on width only set per column");
                Table tabelaOplat = paragraph.InsertTableAfterSelf(rows, columns);
                tabelaOplat.Design = TableDesign.TableGrid;
                tabelaOplat.Rows[0].Cells[0].Paragraphs[0].InsertText("Opis", false);
                tabelaOplat.Rows[0].Cells[1].Paragraphs[0].InsertText("Data naliczenia", false);
                tabelaOplat.Rows[0].Cells[2].Paragraphs[0].InsertText("Kwota brutto", false);
                tabelaOplat.Rows[0].Cells[0].Paragraphs[0].Alignment = Alignment.left;
                tabelaOplat.Rows[0].Cells[1].Paragraphs[0].Alignment = Alignment.left;
                tabelaOplat.Rows[0].Cells[2].Paragraphs[0].Alignment = Alignment.left;
                tabelaOplat.Rows[0].Cells[0].Width = 1000;
                tabelaOplat.Rows[0].Cells[1].Width = 2000;
                tabelaOplat.Rows[0].Cells[2].Width = 500;
                // table based on autofit window and width.. as you can see when autofit is set Width is ignored.
                Paragraph paragraph1 = documentWord.InsertParagraph("table based on autofit window and width.. as you can see when autofit is set Width is ignored.");
                Table tabelaOplat1 = paragraph1.InsertTableAfterSelf(rows, columns);
                tabelaOplat1.AutoFit = AutoFit.Window;
                tabelaOplat1.Design = TableDesign.TableGrid;
                tabelaOplat1.Rows[0].Cells[0].Paragraphs[0].InsertText("Opis", false);
                tabelaOplat1.Rows[0].Cells[1].Paragraphs[0].InsertText("Data naliczenia", false);
                tabelaOplat1.Rows[0].Cells[2].Paragraphs[0].InsertText("Kwota brutto", false);
                tabelaOplat1.Rows[0].Cells[0].Paragraphs[0].Alignment = Alignment.left;
                tabelaOplat1.Rows[0].Cells[1].Paragraphs[0].Alignment = Alignment.left;
                tabelaOplat1.Rows[0].Cells[2].Paragraphs[0].Alignment = Alignment.left;
                tabelaOplat1.Rows[0].Cells[0].Width = 300;
                tabelaOplat1.Rows[0].Cells[1].Width = 500;
                tabelaOplat1.Rows[0].Cells[2].Width = 500;
                // table based on autofit window and no width.. example to show that width is ignored when autofit is on.
                Paragraph paragraph3 = documentWord.InsertParagraph("table based on autofit window and no width.. example to show that width is ignored when autofit is on.");
                Table tabelaOplat3 = paragraph3.InsertTableAfterSelf(rows, columns);
                tabelaOplat3.AutoFit = AutoFit.Window;
                tabelaOplat3.Design = TableDesign.TableGrid;
                tabelaOplat3.Rows[0].Cells[0].Paragraphs[0].InsertText("Opis", false);
                tabelaOplat3.Rows[0].Cells[1].Paragraphs[0].InsertText("Data naliczenia", false);
                tabelaOplat3.Rows[0].Cells[2].Paragraphs[0].InsertText("Kwota brutto", false);
                tabelaOplat3.Rows[0].Cells[0].Paragraphs[0].Alignment = Alignment.left;
                tabelaOplat3.Rows[0].Cells[1].Paragraphs[0].Alignment = Alignment.left;
                tabelaOplat3.Rows[0].Cells[2].Paragraphs[0].Alignment = Alignment.left;
                // Example with autofit to columnwidth
                Paragraph paragraph2 = documentWord.InsertParagraph("Example with autofit to columnwidth");
                Table tabelaOplat2 = paragraph2.InsertTableAfterSelf(rows, columns);
                tabelaOplat2.AutoFit = AutoFit.ColoumnWidth;
                tabelaOplat2.Design = TableDesign.TableGrid;
                tabelaOplat2.Rows[0].Cells[0].Paragraphs[0].InsertText("Opis", false);
                tabelaOplat2.Rows[0].Cells[1].Paragraphs[0].InsertText("Data naliczenia", false);
                tabelaOplat2.Rows[0].Cells[2].Paragraphs[0].InsertText("Kwota brutto - TEST TEST TEST TEST TEST TEST", false);
                tabelaOplat2.Rows[0].Cells[0].Paragraphs[0].Alignment = Alignment.left;
                tabelaOplat2.Rows[0].Cells[1].Paragraphs[0].Alignment = Alignment.left;
                tabelaOplat2.Rows[0].Cells[2].Paragraphs[0].Alignment = Alignment.left;
                for (int row = 0; row < rows - 1; row++) {
                    const string testToFil = "ssssssssssssssss ssssssssss s ssss sssssssssssssssssss";
                    Paragraph tableParagraph = tabelaOplat2.Rows[row + 1].Cells[0].Paragraphs[0];
                    tableParagraph.InsertText(testToFil);
                    tableParagraph = tabelaOplat2.Rows[row + 1].Cells[1].Paragraphs[0];
                    tableParagraph.InsertText(testToFil);
                    tableParagraph = tabelaOplat2.Rows[row + 1].Cells[2].Paragraphs[0];
                    tableParagraph.InsertText(testToFil);
                }
                // Example without any AutoFit
                Paragraph paragraph5 = documentWord.InsertParagraph("Example without any AutoFit");
                Table tabelaOplat5 = paragraph5.InsertTableAfterSelf(rows, columns);
                tabelaOplat5.Design = TableDesign.TableGrid;
                tabelaOplat5.Rows[0].Cells[0].Paragraphs[0].InsertText("Opis", false);
                tabelaOplat5.Rows[0].Cells[1].Paragraphs[0].InsertText("Data naliczenia", false);
                tabelaOplat5.Rows[0].Cells[2].Paragraphs[0].InsertText("Kwota brutto - TEST TEST TEST TEST TEST TEST", false);
                tabelaOplat5.Rows[0].Cells[0].Paragraphs[0].Alignment = Alignment.left;
                tabelaOplat5.Rows[0].Cells[1].Paragraphs[0].Alignment = Alignment.left;
                tabelaOplat5.Rows[0].Cells[2].Paragraphs[0].Alignment = Alignment.left;
                for (int row = 0; row < rows - 1; row++) {
                    const string testToFil = "bbbbbbbbbb b bbbbbbbbbbbb       b bbbbbbbbb   bbb";
                    Paragraph tableParagraph = tabelaOplat5.Rows[row + 1].Cells[0].Paragraphs[0];
                    tableParagraph.InsertText(testToFil);
                    tableParagraph = tabelaOplat5.Rows[row + 1].Cells[1].Paragraphs[0];
                    tableParagraph.InsertText(testToFil);
                    tableParagraph = tabelaOplat5.Rows[row + 1].Cells[2].Paragraphs[0];
                    tableParagraph.InsertText(testToFil);
                }
                // Example with AutoFit to content and filled in content
                Paragraph paragraph6 = documentWord.InsertParagraph("Example with AutoFit to content");
                Table tabelaOplat6 = paragraph6.InsertTableAfterSelf(rows, columns);
                tabelaOplat6.AutoFit = AutoFit.Contents;
                tabelaOplat6.Design = TableDesign.TableGrid;
                tabelaOplat6.Rows[0].Cells[0].Paragraphs[0].InsertText("Opis", false);
                tabelaOplat6.Rows[0].Cells[1].Paragraphs[0].InsertText("Data naliczenia", false);
                tabelaOplat6.Rows[0].Cells[2].Paragraphs[0].InsertText("Kwota brutto - TEST TEST TEST TEST TEST TEST", false);
                tabelaOplat6.Rows[0].Cells[0].Paragraphs[0].Alignment = Alignment.left;
                tabelaOplat6.Rows[0].Cells[1].Paragraphs[0].Alignment = Alignment.left;
                tabelaOplat6.Rows[0].Cells[2].Paragraphs[0].Alignment = Alignment.left;
                for (int row = 0; row < rows - 1; row++) {
                    const string testToFil = "ccccccccccc ccc ccccccccc       cccc cccc   cccccc";
                    Paragraph tableParagraph = tabelaOplat6.Rows[row + 1].Cells[0].Paragraphs[0];
                    tableParagraph.InsertText(testToFil);
                    tableParagraph = tabelaOplat6.Rows[row + 1].Cells[1].Paragraphs[0];
                    tableParagraph.InsertText(testToFil);
                    tableParagraph = tabelaOplat6.Rows[row + 1].Cells[2].Paragraphs[0];
                    tableParagraph.InsertText(testToFil);
                }
                documentWord.Save();
            }

Apr 5, 2012 at 7:32 PM

Hi,

Maybe this helps:

http://docx.codeplex.com/discussions/351361

Roger