none
Adding a title above a table and notes right below a table RRS feed

  • Question

  • Hi,

    Using C# 4, how can I add a title right above a Table and notes right below my Table?

    Note that I want my table title to stay left aligned with my table left edge if, for example, I horizontally center my table in the page.
    I'd also like my table title to be at a very precise vertical distance from my table in terms of pixels or points.

    Same thing with the notes below the table.

    If this is possible, please provide some C# sample code.

    Thanks.

    Eric

    • Changed type Bruce Song Monday, February 14, 2011 9:09 AM
    Tuesday, February 8, 2011 10:33 PM

Answers

  • Hi Bruce,

    Thanks for the code snippet.

    I tried it, but I have had a hard time using it though because wordTable.Rows.LeftIndent does not seem to return the left indent of the wordTable itself.  It did not solve my
    problem of adding some precise vertical space between the title and the table itself.

    I also would have to keep a precise count of the paragraphs to know which paragraphs correspond to my table notes (especially since notes may contain several paragraphs).

    I resolved to solve the problem as follows (somewhat simplified):

    • add a row at the top of the table for the table title
    • merge all cells in this row
    • remove borders for this row (except the bottom border)
    • set the BottomPadding property of this top row to whatever vertical space I want between the table title and the table itself
    • add a row at the bottom of the table for the table notes
    • merge all cells in this row
    • remove borders for this row (except the top border)
    • set the TopPadding property of this bottom row to whatever vertical space I want between the table title and the table itself

    This solved my problem, with the drawback that now, setting properties at the table level applies the properties on all table cells including the top row (title) and bottom row (notes), so I have to create cover properties for each Table property I want to use and apply the property to rows instead (i.e. all rows except the top and bottom one).

    Eric

    • Marked as answer by Bruce Song Monday, February 14, 2011 9:11 AM
    Thursday, February 10, 2011 9:58 AM

All replies

  • Hi Ericmar,

    Thank you for posting and we are glad to help with you.

    >>Note that I want my table title to stay left aligned with my table left edge if, for example, I horizontally center my table in the page.

    I did some research about your problem. I think it can be achieved by setting the LeftIndent of paragraph to be the same with table. Below is my code snippet:

                    float lIndent = 0.0f;
                    float tbWidth = 0.0f;
                    // get table width 
                    foreach(Word.Cell eachCell in wordTable.Rows[1].Cells)
                    {
                        tbWidth += eachCell.Width;
                    }

                    // get table LeftIndent
                    lIndent = wordTable.Rows.LeftIndent;
                    // set title LeftIndent
                    wordDoc.Paragraphs[1].LeftIndent = lIndent;
                    // set footer LeftIndent
                    wordDoc.Paragraphs[23].LeftIndent = lIndent + tbWidth;

    Besides, I think it’s really hard to set a VERY precise vertical distance by pixels or points. 

     

    I hope it can help you and feel free to follow up after you tried.

    Best Regards,

     


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 9, 2011 7:40 AM
  • Hi Bruce,

    Thanks for the code snippet.

    I tried it, but I have had a hard time using it though because wordTable.Rows.LeftIndent does not seem to return the left indent of the wordTable itself.  It did not solve my
    problem of adding some precise vertical space between the title and the table itself.

    I also would have to keep a precise count of the paragraphs to know which paragraphs correspond to my table notes (especially since notes may contain several paragraphs).

    I resolved to solve the problem as follows (somewhat simplified):

    • add a row at the top of the table for the table title
    • merge all cells in this row
    • remove borders for this row (except the bottom border)
    • set the BottomPadding property of this top row to whatever vertical space I want between the table title and the table itself
    • add a row at the bottom of the table for the table notes
    • merge all cells in this row
    • remove borders for this row (except the top border)
    • set the TopPadding property of this bottom row to whatever vertical space I want between the table title and the table itself

    This solved my problem, with the drawback that now, setting properties at the table level applies the properties on all table cells including the top row (title) and bottom row (notes), so I have to create cover properties for each Table property I want to use and apply the property to rows instead (i.e. all rows except the top and bottom one).

    Eric

    • Marked as answer by Bruce Song Monday, February 14, 2011 9:11 AM
    Thursday, February 10, 2011 9:58 AM
  • Hi Ericmar,

    Thank you for sharing your solution here. It will be very beneficial for other community members who have similar questions. Appreciate your workaround.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 14, 2011 9:11 AM