none
Table Alignment issue in MSWord using C# RRS feed

  • Question

  • Im generating few tables in word document, one of my table is having more number of columns, so it is going beyond my document page width and few columns in it are not visible, if im trying to set its width then the column names in the table are wrapping up

    My goal is :

    1. I want the table to autofit in the page irrespective of the no of columns.

    2. The column names should not wrap up, i.e the column names should be displayed in single line,even if there are more no of columns the font size should be reduced

    Friday, November 16, 2012 5:04 AM

Answers

  • The only way Word can do this is using the Table.AllowAutoFit property. But that will respect the minimum cell width.

    If you know the value you want, then you can change the column width using: Table.Columns[index].Width


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, November 16, 2012 9:02 AM
    Moderator

All replies

  • Im generating few tables in word document, one of my table is having more number of columns, so it is going beyond my document page and few columns in it are not visisble, if im trying to set its width then the column names in the table are wrapping up

    now my goal is
    -i want the table to autofit in the page irrespective of the no of columns
    -the column names should not wrap up, i.e the column names should be displayed in single line
    • Moved by Jason Dot Wang Monday, November 19, 2012 3:38 AM This thread is about Word for Developers (From:Visual C# Language)
    • Merged by Cindy Meister MVPModerator Monday, November 19, 2012 8:44 AM same issue
    Thursday, November 15, 2012 12:21 PM
  • Hi,

    What is your exact question? Did you try for yourself and couldn't find the answer, or are you experiencing trouble getting the behavior you're looking for?

    What environment and tools are you using?
    - Office Open XML?
    - Word automation?
    - ASP.NET, WinForms, VBA...?

    Searching Bing returned this (among other useful results): AutoFit.

    Hope this helps


    Please mark the best replies as answers - Twitter: @rickvdbosch - Blog: http://bloggingabout.net/blogs/rick

    Thursday, November 15, 2012 12:31 PM
  • Hi,

    What is your exact question? Did you try for yourself and couldn't find the answer, or are you experiencing trouble getting the behavior you're looking for?

    What environment and tools are you using?
    - Office Open XML?
    - Word automation?
    - ASP.NET, WinForms, VBA...?

    Searching Bing returned this (among other useful results): AutoFit.

    Hope this helps


    Please mark the best replies as answers - Twitter: @rickvdbosch - Blog: http://bloggingabout.net/blogs/rick

    winforms, facing problem, when im trying to set the width of the table less than width of the page, the column names are wrapping up
    Thursday, November 15, 2012 12:34 PM
  • Hi,

    You should set the WordWrap property of the Cells you do not want to wrap.

    Hope this helps


    Please mark the best replies as answers - Twitter: @rickvdbosch - Blog: http://bloggingabout.net/blogs/rick

    Thursday, November 15, 2012 1:07 PM
  • I encountered a similar problem when I convert word to PDF. My problem is that some word tables containing Est Asian string are go beyond my PDF page. But now, it has been solved by the support. What I  use a .net word library, I think it can solve your probelm, you can give it a try.

    Friday, November 16, 2012 3:26 AM
  • I encountered a similar problem when I convert word to PDF. My problem is that some word tables containing Est Asian string are go beyond my PDF page. But now, it has been solved by the support. What I  use a .net word library, I think it can solve your probelm, you can give it a try.

    is that a 3rd party control?
    Friday, November 16, 2012 4:06 AM
  • How many columns are we talking about, and is the page in portrait or landscape orientation? There is a practical limit to what Word can handle on a printed page...

    The only option Word has for automatically adjusting the font size to the column width is the Cell.FitText property. You can also deactivate Cell.WordWrap. The results of this are, however, not particularly "pretty"...

    While a table width can be set to 100% of the page, Word does have an internal minimum width for cell columns which it will not override on its own. When the number of columns exceeds what can be shown reasonably within the width of the page, the width of the table will go beyond the page margin. In this case, your only recourse is to adjust the column widths individually. There is, however, no event that will trigger allowing you to make this change "on-the-fly".


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, November 16, 2012 8:11 AM
    Moderator
  • How many columns are we talking about, and is the page in portrait or landscape orientation? There is a practical limit to what Word can handle on a printed page...

    The only option Word has for automatically adjusting the font size to the column width is the Cell.FitText property. You can also deactivate Cell.WordWrap. The results of this are, however, not particularly "pretty"...

    While a table width can be set to 100% of the page, Word does have an internal minimum width for cell columns which it will not override on its own. When the number of columns exceeds what can be shown reasonably within the width of the page, the width of the table will go beyond the page margin. In this case, your only recourse is to adjust the column widths individually. There is, however, no event that will trigger allowing you to make this change "on-the-fly".


    Cindy Meister, VSTO/Word MVP, my blog

    there can be any no of columns, yes, how can we adjust the column widths individually, like i have a columns for ID, its taking much space for it, so i want ti limit the column widths based on the values..how can v adjust the width of the columns based on the values..
    Friday, November 16, 2012 8:42 AM
  • The only way Word can do this is using the Table.AllowAutoFit property. But that will respect the minimum cell width.

    If you know the value you want, then you can change the column width using: Table.Columns[index].Width


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, November 16, 2012 9:02 AM
    Moderator
  • Yes, it is a third party library to read,write,edit and manipulate doc/docx file.
    Monday, November 19, 2012 1:45 AM
  • Hi porcupine87,

      Welcome to MSDN Forum Support.
      You are more likely to get more efficient responses to Word for Developers issues at 
    http://social.msdn.microsoft.com/Forums/mr/worddev/threads where you can contact Word for Developers experts,so I would like to redirect you to appropriate forum for better responses.

      Sincerely,

      Jason Wang


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, November 19, 2012 3:41 AM