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.

Mon Aug 22, 2022 8:05 pm

Dears,
please support as when i try to justfy column width to be autofit to content by Fundtion " Auto_Fit_To_Contents " i got the below error

Exception in thread "main" java.lang.NullPointerException

Code: Select all

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
public class Test4 {
    public static void main(String [] args){
        Document doc = new Document();
        doc.loadFromFile("Scope.docx");

        //Find the keyword. it may like this "${table_holder}".
        //here I just use the whole paragraph for example.
        TextSelection selection= doc.findString("${table_holder}",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,"table_bookmark");
        BookmarkEnd end=new BookmarkEnd(doc,"table_bookmark");
        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);

        // AutoFit Column to Content
       table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents); /////  <<<<<<<<<>>>>>>>>>>>>Here is the problem<<<<<<<<<<<>>>>>>>>>>>>>>>>>


        //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("table_bookmark");
        //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("output.docx", FileFormat.Docx);


    }

}

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

Tue Aug 23, 2022 9:34 am

Hi,

Thanks for your feedback.

Please place this method (table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents)) after "navigator.replaceBookmarkContent(body)". This is because the Table needs to have a clear parent container when executing this method, otherwise there will be a problem that the parent container is empty.
Code: Select all
        //....
        navigator.replaceBookmarkContent(body);
        table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents);
        //....
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Tue Aug 23, 2022 8:50 pm

sorry
problem still existing
the below error show

Exception in thread "main" java.lang.NullPointerException

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

Wed Aug 24, 2022 1:55 am

Hi,

Thanks for your reply.

Since we've recently made some fixes and refinements to the "table.autoFit" method, I'm afraid you'll need to update to a newer version to use it properly in this case.
I suggest you update directly to the latest commercial version 7.8.6 of Spire.Doc For Java and test again.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Sat Aug 27, 2022 11:48 am

Thank you its worikng :) :)

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

Return to Spire.Doc