How to use C# Microsoft.Office.Interop.Word to generate a repeated Table Header which is not on the first row of the Table on the first page in a Word document?

BlueSky2020 1 Reputation point
2021-03-01T01:14:24.12+00:00

Now I am going to use C# Microsoft.Office.Interop.Word to implement the functionality of repeated table headers across multiple pages in a word document. However, the expected output Word document (please refer to the URL https://onedrive.live.com/edit.aspx?cid=8ffa4a1c66cfced9&page=view&resid=8FFA4A1C66CFCED9!1071&parId=8FFA4A1C66CFCED9!1070&app=Word for a sample document) is quite special at the requirement of repeated table header: the table header (see the row with the blue background) is in the third row on the first page, but will be in the first row as normal across all subsequent pages. Please kindly advise on a solution. You are welcome to give some sample C# codes on this regard. Thanks.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,310 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Timon Yang-MSFT 9,576 Reputation points
    2021-03-04T08:29:21.397+00:00

    I did some tests, but the results may disappoint you.

    We can use "wordTable.Rows1.HeadingFormat = -1;" to make certain rows of the table become header rows to make them appear in all pages.

    The number in square brackets is how many rows are the heads, but the header rows must start from the first row.

    I tried to generate the table first and then add the first two rows, but unfortunately, the rows above the title row that we added later was detected by Word.

    It treats these rows as part of the header row, and then presents them on the following pages, this is the same as we directly set the first 3 rows as the header row at the beginning.

    My conclusion is that Microsoft.Office.Interop.Word does not support this. I don't know if other packages are possible, but I think it may not. After all, Word does not seem to support such operations itself.

    Maybe you can split the first two rows as a new table and insert it above the current table, but the result obtained in this way does not look elegant.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. BlueSky2020 1 Reputation point
    2021-03-05T02:17:15.167+00:00

    Dear TimonYang-MSFT,

    "Maybe you can split the first two rows as a new table and insert it above the current table, but the result obtained in this way does not look elegant."

    You exactly hit the point! It is exactly my new idea of the solution. I have written a simulation C# program to implement the idea, by creating two tables, the upper table gets only two rows, with the Table Header painted in green background, and the lower table, which gets a blue-background Table Header, and is extensible to multiple pages. This is exactly to simulate the scenario of "split table". Between the two tables, I inserted a paragraph, with an empty text of minimum font size of 1 point (cannot set smaller font size).

    My simulation C# program Form1.cs is shown below.

    74566-form1.txt

    The generated document (in PDF format, frankly speaking I feel quite surprised that this Microsoft forum cannot support the upload of a Word document!)

    74565-output.pdf

    The remaining issue is that there is still a thin gap (around 1 point) between the two tables (see the attached screenshot below). Please kindly be noted that you cannot simply delete the inserted paragraph after drawing the lower table, because this will cause the two tables join together, and thus the Table Headers of all subsequent pages will convert to green color instead of blue color.

    74526-gap-between-two-tables.jpg

    Now our challenges are: 1) whether we can further compress the height of the paragraph, by setting the font size (defined by the property objPara3.Range.Font.Size) to be fewer than 1, or by some else approaches; 2) whether we can draw a thin horizontal line to fill in the gap between the two tables, then it appears to be a unified table.

    Thank you for any further help!

    Rgds,
    Daniel

    0 comments No comments

  3. Timon Yang-MSFT 9,576 Reputation points
    2021-03-05T08:57:01.847+00:00

    I opened Word and manually added two tables with a blank row in the middle. At this time, we only need to click the Delete key to delete this extra blank row.

    74761-1.gif

    So we only need to implement it programmatically. I tried it for a while, but I didn't find a way to do it in interop. Maybe you can do it.
    My current approach is to use FreeSpire. Doc and then use the following code:

       class Program  
        {  
            static void Main(string[] args)  
            {  
                Document doc = new Document();  
                doc.LoadFromFile(@"d:\test\word\test.docx");  
      
                foreach (Section section in doc.Sections)  
                {  
                    for (int i = 0; i < section.Body.ChildObjects.Count; i++)  
                    {  
                        if (section.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.Paragraph)  
                        {  
                            if (String.IsNullOrEmpty((section.Body.ChildObjects[i] as Paragraph).Text.Trim()))  
                            {  
                                section.Body.ChildObjects.Remove(section.Body.ChildObjects[i]);  
                                i--;  
                            }  
                        }  
      
                    }  
                }  
                string result = @"d:\test\word\test1.docx";  
                doc.SaveToFile(result, FileFormat.Docx2013);  
      
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  4. BlueSky2020 1 Reputation point
    2021-03-05T10:32:35.933+00:00

    Dear TimonYang-MSFT,

    Right now I have used your FreeSpire codes to remove the small gap (with the height of 1 point) between the two tables. The C# program can delete the gap and merge the two tables. However, to be disappointed, and also as my expected, after the merging of the two tables, all table headers across the subsequent pages will align with the table header in the upper table to become green. Please see the output document output1.pdf below.

    74814-output1.pdf

    My expectation is that the table headers across all the subsequent pages will align with the table header of the original lower table to become blue instead of green. So now my strategy is to try to compress (instead of deleting) the gap (whatever is paragraph, row, section) further to be shorter than 1 point, then it will look like a join table but the table headers across all subsequent pages can still be blue color.

    Do you have any idea to further compress (not delete) the gap between the two tables? Alternatively, can you try to insert a thin horizontal line to fill into the gap to make the two tables appear to join together?

    Thank you.

    Rgds,
    Daniel