question

BlueSky2020-0459 avatar image
0 Votes"
BlueSky2020-0459 asked ·

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?

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.

dotnet-csharp
· 2
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @BlueSky2020-0459, the file seems to have been deleted or expired, I cannot get it, please check and provide a new document or edit the question to add some screenshots so that we can understand your question.

0 Votes 0 ·

Hi, TimonYang-MSFT,

74101-pso-sample.pdf




I upload the sample document (in PDF format, as cannot upload a Word document).

Please check it. Thanks!

0 Votes 0 ·
pso-sample.pdf (143.4 KiB)
TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered ·

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

We can use "wordTable.Rows[1].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.

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Dear TimonYang-MSFT,

Can you try to provide a solution for me to further compress the gap between the two tables?
Can u help me to introduce somebody who is expert at C# Microsoft.Office.Interop.Word to give me some help in this regard?

Thank you.

0 Votes 0 ·
BlueSky2020-0459 avatar image
0 Votes"
BlueSky2020-0459 answered ·

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





form1.txt (4.6 KiB)
output.pdf (123.6 KiB)
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered ·

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.


1.gif (468.8 KiB)
· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, TimonYang-MSFT,

"At this time, we only need to click the Delete key to delete this extra blank row."

Sorry I have to say that this approach cannot work. If you manually delete the extra blank row, the two tables will be joined as a single table, but the Table Headers across all the subsequent pages will align with the Table Header of the original upper table (which is green) instead of the Table Header of the original lower table (which is blue). Thereafter, all the Table Headers across the subsequent pages will convert to green rather than blue, which is not the expected.

The output document by "click the Delete key" is shown below.

74737-output-20210305.pdf


0 Votes 0 ·
output-20210305.pdf (123.1 KiB)
BlueSky2020-0459 avatar image
0 Votes"
BlueSky2020-0459 answered ·

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




output1.pdf (123.0 KiB)
· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@BlueSky2020-0459
I searched and tried for a period of time, but unfortunately did not find a feasible solution.
Maybe you can first ask Microsoft Community if there is a way to do this manually in MS Word. If it does, we will try to simulate it with code.

0 Votes 0 ·

Thank you, TimonYang!

0 Votes 0 ·