none
How to set table (Row) height in word table using c# RRS feed

  • Question

  • Hi,

    I have created word application thr. code in that i have added table with (1 row and 3 columns) in header.

    I want to set height of this table OR Row And how to set vertical alignment of cell as center.

    I have tried following ways but doesn't come up with solution.

     1)oTable.Cell(1, 1).Height = 1;
     oTable.Cell(1, 2).Height = 1;
     oTable.Cell(1, 3).Height = 1;
    
     2) oTable.Rows[1].Height = 1;
    
     3) oTable.Rows.SetHeight(20, WdRowHeightRule.wdRowHeightAtLeast);
    

    here is my code snippets

      Word.Range oRange = myDoc.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                    Word.Table oTable = myDoc.Tables.Add(oRange, 1, 3, ref oMissing, ref oMissing);
                    int r, c;
                    string strText="";
                    for (r = 1; r <= 1; r++)
                        for (c = 1; c <= 3; c++)
                        {
                            if (c == 1) {  strText = LHEADER ; }
                            else if (c == 2) { strText = CHEADER ; }
                            else if (c == 3) { strText = RHEADER; }
    
                            oTable.Cell(r, c).Range.Text = strText;
    
                            oTable.Cell(r, c).Range.Font.Name = "Times New Roman";
                            oTable.Cell(r, c).Range.Font.Size = 15;
                        }
                    //The col1 cell
                    oTable.Cell(1, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                    //The col2 cell
                    oTable.Cell(1, 2).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                    //The col3 cell
                    oTable.Cell(1, 3).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
    Thanks in advance...
     

    Experience the innovation with perfection.



    • Edited by Bhagawat Monday, August 6, 2012 11:59 AM
    Monday, August 6, 2012 11:55 AM

Answers

  •   Hi JosephFox, Thanks Wonderful help 

    I have tried like this and it works..

    oRange.Paragraphs.SpaceAfter = 0;
    oRange.Paragraphs.SpaceBefore = 0;

    Thanks once again.


    Experience the innovation with perfection.

    • Marked as answer by Bhagawat Wednesday, August 8, 2012 5:32 AM
    Tuesday, August 7, 2012 9:46 AM

All replies

  • Table.Rows[1].Height = 1;

    Should work...but bear in mind this is just the minimum height. If your font size is bigger than 1 (and it almost certainly is) the row height will shrink only to the font height.

    As for vertical centering, I'm not sure this is possible, except by inserting preceding rows of a particular size font.

    Monday, August 6, 2012 1:27 PM
  • Hi JosephFox yea you are right row height will shrink only to the font height (It's depends on font size), So how can achieve my goal is their is any solution. 

    Experience the innovation with perfection.

    Tuesday, August 7, 2012 4:44 AM
  • Well, if you want the row really small, you have to set the font size to be really small (e.g size 1 font).
    Tuesday, August 7, 2012 7:11 AM
  • Hi JosephFox, their is no any way to achieve this. I can't set font size small because it's header text of word document. Due to increased size of table more space is added in Header tab.

    Or

    1) how to set cell vertical alignment as center

    2) Line spacing- Remove space after header. 


    Experience the innovation with perfection.

    Tuesday, August 7, 2012 7:19 AM
  • I mean the font size of the table row; not of the whole header. Even if there's no text in the row.

    You can set different text sizes even within a header.

    Tables are designed so rows are at least as tall as the theoretical height of their letters. The thinking is; why would you want a row to cut off the tops of the letters? I'm not saying it's a perfect design.

    • Edited by JosephFox Tuesday, August 7, 2012 7:39 AM
    Tuesday, August 7, 2012 7:36 AM
  • Hi JosephFox, I have added table and set left  column header and right header. I have a solution for this  manually .

    1)Select table goto Home tab => Paragraph => Line spacing set 1.0 and Line spacing option => After=0 (default is 10pt)

    How to implement this thr. code.

    Thanks in advance...

     

    Experience the innovation with perfection.

    Tuesday, August 7, 2012 7:46 AM
  • here is my screen shot of word doc

    Experience the innovation with perfection.

    Tuesday, August 7, 2012 7:54 AM
  • I think it's one of the Paragraph object's properties. Get the paragraph, or paragraphs collection (every Range has a Paragraphs property) and then try ammending LineSpacingBefore. If that doesn't work try LineSpacing.
    Tuesday, August 7, 2012 8:38 AM
  •   Hi JosephFox, Thanks Wonderful help 

    I have tried like this and it works..

    oRange.Paragraphs.SpaceAfter = 0;
    oRange.Paragraphs.SpaceBefore = 0;

    Thanks once again.


    Experience the innovation with perfection.

    • Marked as answer by Bhagawat Wednesday, August 8, 2012 5:32 AM
    Tuesday, August 7, 2012 9:46 AM