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.

Sun Aug 21, 2022 6:02 pm

HI dears,
kindly i need your help to know How to Write Table or Paragrph in Spcific page ? by (JAVA)Code
for example : i need to create Table in page 2 or Page 3 ,, not page 1 by (JAVA)Code

shadywardy
 
Posts: 19
Joined: Thu Aug 18, 2022 2:25 pm

Mon Aug 22, 2022 3:48 am

Hi,

Thank you for your inquiry.
Our Spire.Doc is based on Microsoft Word's document specification, and the word document consists of one or more sections. Sorry, Spire.Doc cannot directly add the table and paragraph to a specified "page" at present, but you can add them to the specified section, please refer to the code below. If you have any questions, please feel free to contact us.
Code: Select all
        Document doc=new Document();
        //Load the document
        doc.loadFromFile("test.docx");
        //Get the second section
        Section section=doc.getSections().get(1);
        //Add a paragraph
        Paragraph paragraph=section.addParagraph();
        paragraph.setText("Add a new paragraph");

        String[] header = {"name", "sex"};
        String[][] data =
                {
                        new String[]{"Winny", "f"},
                        new String[]{"Lois", "f"},
                        new String[]{"Jois", "m"},
                        new String[]{"Moon", "f"},
                        new String[]{"Vinit", "f"},
                };
        //Add table
        Table table = section.addTable(true);
        table.resetCells(data.length + 1, header.length);
        //Set the first row as the header
        TableRow row = table.getRows().get(0);
        row.isHeader(true);
        for (int i = 0; i < header.length; i++) {
            Paragraph p = row.getCells().get(i).addParagraph();
            TextRange range1 = p.appendText(header[i]);
            range1.getCharacterFormat().setFontName("Arial");
            range1.getCharacterFormat().setFontSize(12f);
        }
        //Add data to other rows
        for (int r = 0; r < data.length; r++) {
            TableRow dataRow = table.getRows().get(r + 1);
            for (int c = 0; c < data[r].length; c++) {
                TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
                range2.getCharacterFormat().setFontName("Arial");
                range2.getCharacterFormat().setFontSize(10f);
            }
        }
        //Save the document
        doc.saveToFile("output.docx", FileFormat.Docx_2013);

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Mon Aug 22, 2022 6:32 pm

there is the below error

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:659)
at java.util.ArrayList.get(ArrayList.java:435)
at com.spire.office.packages.sprcfqa.spr┃┝—(Unknown Source)
at com.spire.doc.collections.SectionCollection.get(Unknown Source)
at Test4.main(Test4.java:17)

Code: Select all
 Document doc=new Document();
        //Load the document
        doc.loadFromFile("Scope.docx");
        //Get the second section
        Section section=doc.getSections().get(1); ////// Error ,, will work only by "   get(0) "
        //Add a paragraph
        Paragraph paragraph=section.addParagraph();
        paragraph.setText("Add a new paragraph");

shadywardy
 
Posts: 19
Joined: Thu Aug 18, 2022 2:25 pm

Tue Aug 23, 2022 2:31 am

Hi,

Thank you for your reply.
Based on your exception information, I can infer that your Word only has one section. That's the reason why you got "IndexOutOfBoundsException" when getting the second section. Please note one section could contain one or more "page". As I said earlier, our Spire.Doc doesn't support adding the table and paragraph to a specified "page" at present. Please use the Section section=doc.getSections().get(0); to get the first section and add content on it. If there is still any question, please provide your sample Word for further investigtaion.

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Tue Aug 23, 2022 8:54 pm

Thanks for your support , kindly find my sample word file

shadywardy
 
Posts: 19
Joined: Thu Aug 18, 2022 2:25 pm

Wed Aug 24, 2022 6:45 am

Hi,

Thank you for your sharing.
I did a test with your input document. As I said before, the reason why the "IndexOutOfBoundsException" message was thrown is that your document has only one section. Please kindly note that Spire.Doc is based on Microsoft word. The Word file are composed of sections, and there is no concept of "page". So you cannot insert content into specify "page". Sorry for the inconvenience caused and hope you can understand.

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Wed Aug 24, 2022 5:49 pm

Thanks you and appreciated

shadywardy
 
Posts: 19
Joined: Thu Aug 18, 2022 2:25 pm

Return to Spire.Doc