Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.
Fri Jun 21, 2024 10:42 am
hi,
how can i repeat table header through pages?
-

emamanu_1987
-
- Posts: 14
- Joined: Fri Jun 21, 2024 10:23 am
Mon Jun 24, 2024 2:18 am
Hello,
Thanks for your inquiry.
Please refer to the following code to set up a repeating header. If you have any other questions, please feel free to write back.
- Code: Select all
// Create a Document object
Document doc = new Document();
// Add a section
Section sec = doc.AddSection();
// Add a table
Table table = sec.AddTable(true);
table.ResetCells(3, 3);
// Get first row as head
TableRow head = table.Rows[0];
// Repeat head
head.IsHeader = true;
// Add content
Paragraph paragraph = head.Cells[0].AddParagraph();
TextRange textRange = paragraph.AppendText("hello");
doc.SaveToFile("res.docx",FileFormat.Docx2016);
Sincerely,
William
E-iceblue support team
-


William.Zhang
-
- Posts: 732
- Joined: Mon Dec 27, 2021 2:23 am
Mon Jun 24, 2024 9:09 am
hi,
i tried beginning from a table altready existing in a template, but not working.
this is my code.
- Code: Select all
public void test()
{
Spire.Doc.Document document = new Spire.Doc.Document();
document.LoadFromFile(@"\template.docx");
foreach (Spire.Doc.Section section in document.Sections)
{
foreach (Spire.Doc.Table table in section.Tables)
{
Spire.Doc.TableRow row_head = table.Rows[0];
Spire.Doc.TableRow row_base = table.Rows[table.Rows.Count - 1];//.Clone();
table.Rows[0].IsHeader = true;
for (int i = 0; i < 100; i++)
{
table.Rows.Insert(table.Rows.Count, row_base.Clone());
}
}
}
document.SaveToFile(@"\tmp\template.docx");
}
-

emamanu_1987
-
- Posts: 14
- Joined: Fri Jun 21, 2024 10:23 am
Mon Jun 24, 2024 9:40 am
Hello,
Thanks for your reply.
I simulated a document and tested the code you provided with our latest
Spire.Doc for Java version: 12.6.2. It can achieve the effect of repeating the header. I have uploaded my test files and results files for your reference. Did you test it with the latest version? If not, please upgrade to the latest version for testing. If it still doesn't work, please provide us with your input file to help us further investigate. Thanks in advance.
Sincerely,
William
E-iceblue support team
Login to view the files attached to this post.
-


William.Zhang
-
- Posts: 732
- Joined: Mon Dec 27, 2021 2:23 am
Mon Jun 24, 2024 10:19 pm
hi,
ok, only first row may be header?
thank you
-

emamanu_1987
-
- Posts: 14
- Joined: Fri Jun 21, 2024 10:23 am
Tue Jun 25, 2024 2:40 am
Hello,
Thanks for your reply.
Kindly note that you can select multiple rows as table headers, but it must start from the first row. For example, if you want to use the first two rows as table headers, you can use the following code.
- Code: Select all
table.Rows[0].IsHeader = true;
table.Rows[1].IsHeader = true;
Sincerely,
William
E-iceblue support team
-


William.Zhang
-
- Posts: 732
- Joined: Mon Dec 27, 2021 2:23 am
Tue Jun 25, 2024 6:52 am
yes,
i tried with your example and working, also with 2 rows header.
but, with my template, not. what is wrong?
https://we.tl/t-IdGfoTjIbM- Code: Select all
public void test()
{
Spire.Doc.Document document = new Spire.Doc.Document();
document.LoadFromFile(@"template_2 - Copia (4).docx");
foreach (Spire.Doc.Section section in document.Sections)
{
foreach (Spire.Doc.Table table in section.Tables)
{
Spire.Doc.TableRow row_head = table.Rows[0];
Spire.Doc.TableRow row_base = table.Rows[table.Rows.Count - 1];//.Clone();
//row_head.IsHeader = true;
//row_base.IsHeader = true;
table.Rows[0].IsHeader = true;
table.Rows[1].IsHeader = true;
//table.Rows[2].IsHeader = true;
for (int i = 0; i < 100; i++)
{
table.Rows.Insert(table.Rows.Count, row_base.Clone());
}
}
}
document.SaveToFile(@"test.docx");
}
-

emamanu_1987
-
- Posts: 14
- Joined: Fri Jun 21, 2024 10:23 am
Tue Jun 25, 2024 8:27 am
Hello,
Thanks for your reply.
Because the first three rows of your original Word document are set as headers, as shown in the screenshot below. The rows you clone later will retain the format of the third row. In order to achieve the effect of repeating the header, you need to cancel the header setting of the third row. The following is the modified code. If you have other questions, please feel free to write back.
Header.png
- Code: Select all
Spire.Doc.TableRow row_base = table.Rows[table.Rows.Count - 1];//.Clone();
// Added code
row_base.IsHeader = false;
table.Rows[0].IsHeader = true;
table.Rows[1].IsHeader = true;
for (int i = 0; i < 100; i++)
{
table.Rows.Insert(table.Rows.Count, row_base.Clone());
}
Sincerely,
William
E-iceblue support team
Login to view the files attached to this post.
-


William.Zhang
-
- Posts: 732
- Joined: Mon Dec 27, 2021 2:23 am
Tue Jun 25, 2024 9:50 am
thank you!

-

emamanu_1987
-
- Posts: 14
- Joined: Fri Jun 21, 2024 10:23 am