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 4:37 am

Hi,
I am evaluating the latest version of Spire.Doc JAVA for our application.
I need to create tables manually for a report in Word document.

I have set the isHeader=true property for the header of a table.
But, when the table spans to the next page, the header does not appear in the second page.

Is this feature available in Spire.Doc?

Regards,
Suma

suma_ar8
 
Posts: 9
Joined: Fri Jun 21, 2024 4:28 am

Fri Jun 21, 2024 5:43 am

Hello,

Thanks for your inquiry.
To help us investigate further, please provide us with your input and output files as well as your test code. You can upload them here as an attachment or send them to this email address: [email protected]. Thanks in advance.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 732
Joined: Mon Dec 27, 2021 2:23 am

Fri Jun 21, 2024 8:49 am

Thanks for your reply.

I wanted to know if you have any method, that will allow me to set the parameter "Repeat Header Rows", that is available in MS Word.

In MS Word the below option is available.
Select the header row or rows that you want to repeat on each page. The selection must include the first row of the table.
Under Table Tools, on the Layout tab, in the Data group, click Repeat Header Rows.

Thanks,
Suma

suma_ar8
 
Posts: 9
Joined: Fri Jun 21, 2024 4:28 am

Fri Jun 21, 2024 9:36 am

Hello,

Thanks for your inquiry.
The corresponding code of "Repeat Header Rows" in our product is "row.IsHeader = true;". According to your previous description, you have added this code, which should be able to achieve the effect of repeating the header, but it seems not to be the case at present. So, in to help us investigate your issue more accurately, please share us your complete test code and input file (if any).

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 732
Joined: Mon Dec 27, 2021 2:23 am

Fri Jun 21, 2024 2:25 pm

Code: Select all
package wordTemplate;

import com.spire.doc.*;
import com.spire.doc.Table;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;

import java.awt.*;


public class CreateTable {

    public static void main(String[] args) {

        //Create a Document object
        Document document = new Document();

        //Add a section
        Section section = document.addSection();

        //Define the data for table
        String[] header = {"Name", "Capital", "Continent", "Area", "Population"};
        String[][] data =
                {
                        new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"},
                        new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"},
                        new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"},
                        new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"},
                        new String[]{"Chile", "Santiago", "South America", "756943", "13200000"},
                        new String[]{"Colombia", "Bogota", "South America", "1138907", "33000000"},
                        new String[]{"Cuba", "Havana", "North America", "114524", "10600000"},
                        new String[]{"Ecuador", "Quito", "South America", "455502", "10600000"},
                        new String[]{"El Salvador", "San Salvador", "North America", "20865", "5300000"},
                        new String[]{"Guyana", "Georgetown", "South America", "214969", "800000"},
                        new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"},
                        new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"},
                        new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"},
                        new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"},
                        new String[]{"Chile", "Santiago", "South America", "756943", "13200000"},
                        new String[]{"Colombia", "Bogota", "South America", "1138907", "33000000"},
                        new String[]{"Cuba", "Havana", "North America", "114524", "10600000"},
                        new String[]{"Ecuador", "Quito", "South America", "455502", "10600000"},
                        new String[]{"El Salvador", "San Salvador", "North America", "20865", "5300000"},
                        new String[]{"Guyana", "Georgetown", "South America", "214969", "800000"},
                        new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"},
                        new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"},
                        new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"},
                        new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"},
                        new String[]{"Chile", "Santiago", "South America", "756943", "13200000"},
                        new String[]{"Colombia", "Bogota", "South America", "1138907", "33000000"},
                        new String[]{"Cuba", "Havana", "North America", "114524", "10600000"},
                        new String[]{"Ecuador", "Quito", "South America", "455502", "10600000"},
                        new String[]{"El Salvador", "San Salvador", "North America", "20865", "5300000"},
                        new String[]{"Guyana", "Georgetown", "South America", "214969", "800000"},
                        new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"},
                        new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"},
                        new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"},
                        new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"},
                        new String[]{"Chile", "Santiago", "South America", "756943", "13200000"},
                        new String[]{"Colombia", "Bogota", "South America", "1138907", "33000000"},
                        new String[]{"Cuba", "Havana", "North America", "114524", "10600000"},
                        new String[]{"Ecuador", "Quito", "South America", "455502", "10600000"},
                        new String[]{"El Salvador", "San Salvador", "North America", "20865", "5300000"},
                        new String[]{"Guyana", "Georgetown", "South America", "214969", "800000"},

                };

        //Add a table
        Table table = section.addTable(true);

        TableRow rowH = table.addRow(header.length);

        rowH.isHeader(true);
        rowH.setHeight(20);
        rowH.setHeightType(TableRowHeightType.Exactly);
        rowH.getRowFormat().setBackColor(Color.gray);
        for (int i = 0; i < header.length; i++) {
            rowH.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
            Paragraph p = rowH.getCells().get(i).addParagraph();
            p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            TextRange txtRange = p.appendText(header[i]);
            txtRange.getCharacterFormat().setBold(true);
        }

        for(int k=0; k<data.length;k++){
            String[] rowData = data[k];
            TableRow row = table.addRow(rowData.length);

            for(int j=0;j< rowData.length;j++){

                    row.setHeight(25);
                    row.setHeightType(TableRowHeightType.Exactly);
                    row.getRowFormat().setBackColor(Color.white);

                    row.getCells().get(j).addParagraph().appendText(data[k][j]);


            }
        }



        //Save to file
        document.saveToFile("D:\\CreateTable.docx", FileFormat.Docx_2013);
    }
}



This is my complete test code.
Pls note that i need to use addRow() when creating rows for the table.
This is just an example illustration of how my code works.
Thanks

suma_ar8
 
Posts: 9
Joined: Fri Jun 21, 2024 4:28 am

Mon Jun 24, 2024 3:01 am

Hello,

Thanks for your reply.
If you use the addRow() method to add rows, the added row will use the format of the previous row. This means that if you set isHeader for the first row, the rows added later will also use this setting, causing all rows in the entire table to become headers. To solve this problem, you can comment out "rowH.isHeader(true);" and then use " table.getRows().get(0).isHeader(true);" to set the first row as the header after the table is created. I have uploaded the modified code snippet for your reference. If you have any other questions, please feel free to write to us.
Code: Select all
...
Table table = section.addTable(true);
TableRow rowH = table.addRow(header.length);
//rowH.isHeader(true);
rowH.setHeight(20);
rowH.setHeightType(TableRowHeightType.Exactly);
rowH.getRowFormat().setBackColor(Color.gray);
for (int i = 0; i < header.length; i++) {
    rowH.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
    Paragraph p = rowH.getCells().get(i).addParagraph();
    p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
    TextRange txtRange = p.appendText(header[i]);
    txtRange.getCharacterFormat().setBold(true);
}
for(int k=0; k<data.length;k++){
    String[] rowData = data[k];
    TableRow row = table.addRow(rowData.length);
    for(int j=0;j< rowData.length;j++){
        row.setHeight(25);
        row.setHeightType(TableRowHeightType.Exactly);
        row.getRowFormat().setBackColor(Color.white);
        row.getCells().get(j).addParagraph().appendText(data[k][j]);
    }
}
// Repeat header
table.getRows().get(0).isHeader(true);
...


Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 732
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.Doc

cron