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.

Tue Aug 23, 2022 10:10 pm

HI Dears,
iam working on Application and there is function in these appalication need to create tables after each other
SO , iam create class to create table , but when i gentrare these class after each other to create Two table
i found three blank spacing .... i need to remove these spacing

Main Class

Code: Select all
public class TestTableWord {

    public static void main(String[] args) {
        String filename="Scope.docx";
        String newFilename="Scope.docx_updated2.docx";
        String bookmark_0="${table_holder}";
        String bookmark_1="${table_holder_1}";
        String table_bookmark_0="table_bookmark";
        String table_bookmark_1="table_bookmark_1";

        new ClassTest(filename,newFilename,bookmark_0,table_bookmark_0);
        new ClassTest(newFilename,newFilename,bookmark_1,table_bookmark_1);



    }



}




Class to create Tabls

Code: Select all
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;

public class ClassTest {


    ClassTest(String filename, String newfilename, String bm, String tbm){
    Document doc = new Document();
    doc.loadFromFile(filename);

    //Find the keyword. it may like this "${table_holder}".
    //here I just use the whole paragraph for example.
    TextSelection selection= doc.findString(bm,false,true);
    //Get the TextRange of the key word
    TextRange range=selection.getAsOneRange();
    //Get the owner paragraph of the key word
    Paragraph owner=range.getOwnerParagraph();

    //Get the index of this range in the owner paragraph
    int index=owner.getChildObjects().indexOf(range);
    //Insert a temporary bookmark to include the key word
    BookmarkStart start=new BookmarkStart(doc,tbm);
    BookmarkEnd end=new BookmarkEnd(doc,tbm);
    owner.getChildObjects().insert(index,start);
    owner.getChildObjects().insert(index+2,end);

    //init a new table object
    Table table=new Table(doc,true);
    //reset rows and columns
    table.resetCells(5,2);
   
    //put some values in the table
    table.get(0,0).addParagraph().appendText("Position:").getCharacterFormat().setBold(true);
    table.get(1,0).addParagraph().appendText("Project:").getCharacterFormat().setBold(true);
    table.get(2,0).addParagraph().appendText("Company:").getCharacterFormat().setBold(true);
    table.get(3,0).addParagraph().appendText("Scope:").getCharacterFormat().setBold(true);
    table.applyHorizontalMerge(3,0,1);
    table.applyHorizontalMerge(4,0,1);



    //init a TextBodyPart object
    TextBodyPart body=new TextBodyPart(doc);
    //add the table to the body
    body.getBodyItems().add(table);


    //Find the bookmark by name
    BookmarksNavigator navigator=new BookmarksNavigator(doc);
    navigator.moveToBookmark(tbm);
    //Replace all content in the bookmark with the new body
    navigator.replaceBookmarkContent(body);



    //If you need, remove the bookmark after replacement
    doc.getBookmarks().remove(navigator.getCurrentBookmark());


    //Save the file
    doc.saveToFile(newfilename, FileFormat.Docx);
}
    }

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

Wed Aug 24, 2022 9:59 am

Hi,

Kindly note the BookmarkStart and the BookmarkEnd are in the same paragraph before replacing the table, but the table cannot be inserted into the paragraph, so the BookmarkEnd will be split into a new paragraph. Please refer to the code below to achieve your need. If you have any questions please feel free to contact us.
Code: Select all
        Document doc = new Document();
        doc.loadFromFile(filename);

        //Find the keyword. it may like this "${table_holder}".
        //here I just use the whole paragraph for example.
        TextSelection selection= doc.findString(bm,false,true);
        //Get the TextRange of the key word
        TextRange range=selection.getAsOneRange();
        //Get the owner paragraph of the key word
        Paragraph owner=range.getOwnerParagraph();

        //Get the index of this range in the owner paragraph
        int index=owner.getChildObjects().indexOf(range);
        //Insert a temporary bookmark to include the key word
        BookmarkStart start=new BookmarkStart(doc,tbm);
        BookmarkEnd end=new BookmarkEnd(doc,tbm);
        owner.getChildObjects().insert(index,start);
        owner.getChildObjects().insert(index+2,end);

        //init a new table object
        Table table=new Table(doc,true);
        //reset rows and columns
        table.resetCells(5,2);

        //put some values in the table
        table.get(0,0).addParagraph().appendText("Position:").getCharacterFormat().setBold(true);
        table.get(1,0).addParagraph().appendText("Project:").getCharacterFormat().setBold(true);
        table.get(2,0).addParagraph().appendText("Company:").getCharacterFormat().setBold(true);
        table.get(3,0).addParagraph().appendText("Scope:").getCharacterFormat().setBold(true);
        table.applyHorizontalMerge(3,0,1);
        table.applyHorizontalMerge(4,0,1);

        //init a TextBodyPart object
        TextBodyPart body=new TextBodyPart(doc);
        //add the table to the body
        body.getBodyItems().add(table);

        //Find the bookmark by name
        BookmarksNavigator navigator=new BookmarksNavigator(doc);
        navigator.moveToBookmark(tbm);
        //Replace all content in the bookmark with the new body
        navigator.replaceBookmarkContent(body);

        Paragraph paragraph= navigator.getCurrentBookmark().getBookmarkEnd().getOwnerParagraph();
        DocumentObject parent=paragraph.getOwner();
        parent.getChildObjects().remove(paragraph);

        paragraph= navigator.getCurrentBookmark().getBookmarkStart().getOwnerParagraph();
        parent=paragraph.getOwner();
        parent.getChildObjects().remove(paragraph);

        //Save the file
        doc.saveToFile(newfilename, FileFormat.Docx);

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:36 pm

Thank you very much and appreciation :)

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

Thu Aug 25, 2022 6:58 am

Hi,

If you need assistance in the future, please don't hesitate to contact us.
Wish you a nice day :D

Sincerely,
Kylie
E-iceblue support team
User avatar

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

Return to Spire.Doc