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.

Wed Feb 14, 2024 9:23 am

hello,

I'm trying to prevent table lines splitting between document pages.

current result :
in case that specific 'signer' table rows is outside of page size, table rows are splitting to next page.

example:
[img]
Screenshot 2024-02-14 at 11.10.06.png

[/img]


desire result:
the entire 'signer' table, with all it's rows, will move to next page.

can you please assist?

my code:
Code: Select all
        Table table = section.addTable();
        for (Object obj : table.getRows()) {
            TableRow tableRow = (TableRow) obj;
            for (Object obj1 : tableRow.getCells()) {
                TableCell tableCell = (TableCell) obj1;
                for (Object obj2 : tableCell.getParagraphs()) {
                    Paragraph paragraph = (Paragraph) obj2;
                    paragraph.getFormat().setKeepFollow(true);
                }
            }
        }
        table.getTableFormat().isBreakAcrossPages(false);


thank you

adi.zahi
 
Posts: 7
Joined: Tue Jan 30, 2024 10:12 am

Thu Feb 15, 2024 4:34 am

Hello,

Thanks for your inquiry.
To meet your needs, please use the following test code for testing. If you have any questions, please feel free to reply at any time.
Code: Select all
 Document document = new Document();
 document.loadFromFile("tableExampleWord.docx");
 FixedLayoutDocument fixedDoc = new FixedLayoutDocument(document);
 for (int i = 0; i < document.getSections().getCount(); i++) {
     Section section = document.getSections().get(i);
     for (int j = 0; j < section.getBody().getChildObjects().getCount(); j++) {
         DocumentObject documentObject = section.getBody().getChildObjects().get(j);
         if(documentObject instanceof Table){
             Table table = (Table) documentObject;
             DocumentObject firstChild = table.getFirstChild();
             DocumentObject lastChild = table.getLastChild();
             LayoutCollection<LayoutElement> layoutEntitiesOfFirstChild = fixedDoc.getLayoutEntitiesOfNode(firstCh
             LayoutCollection<LayoutElement> layoutEntitiesOfLastChild = fixedDoc.getLayoutEntitiesOfNode(lastChil
             if (layoutEntitiesOfLastChild.get(0).getPageIndex() != layoutEntitiesOfFirstChild.get(0).getPageIndex
                 Paragraph paragraph = new Paragraph(document);
                 paragraph.appendBreak(BreakType.Page_Break);
                 section.getBody().getChildObjects().insert(j,paragraph.deepClone());
                 j++;
             }
         }
     }
 }
 document.saveToFile("res.docx");


Sincerely,
William
E-iceblue support team
User avatar

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

Thu Feb 15, 2024 10:02 am

thank you for the quick response

I use Spire.Doc version 11.7.0
we cannot upgrade to newer version (12.1.16) due to a number of issues.

also, I think the provided solution will not prevent splitting tables in case that the document will be edited manually.

Microsoft Word supply 'keep with next' option on Paragraph which I saw that is working as expected.

Screenshot 2024-02-15 at 11.50.56.png


how can I apply this 'keep with next' paragraph format on spire doc?

thank you
Adi

adi.zahi
 
Posts: 7
Joined: Tue Jan 30, 2024 10:12 am

Fri Feb 16, 2024 6:10 am

Hello,

Thank you for your feedback.
Please use the code below to implement this feature you mentioned.
Code: Select all
paragraph.getFormat().setKeepFollow(true);

If you have any further questions or need assistance with anything else, please don't hesitate to let me know.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1652
Joined: Wed Apr 07, 2021 2:50 am

Sun Feb 18, 2024 1:16 pm

hello,

I'm trying to recursively adjust the setKeepFollow on my entire document's paragraphs.

Code: Select all
Document document = new Document();
setKeepFollow(document.getChildObjects());
document.saveToFile(".../tableExampleWord.docx", FileFormat.Docx_2019);

public void setKeepFollow(DocumentObjectCollection childObjects) {
     for (int y=0; y < childObjects.getCount(); y++) {
         DocumentObject childObject = childObjects.get(y);
         if (childObject.getDocumentObjectType().equals(DocumentObjectType.Paragraph)) {
             Paragraph paragraph = (Paragraph) childObjects.get(y);
             paragraph.getFormat().setKeepFollow(true);
         }
         setKeepFollow(childObject.getChildObjects());
    }
}



BUT the rows on my table are still splitting between the document pages.

Screenshot 2024-02-18 at 15.05.23.png



also, if you will opened my attached result file "tableExampleWord.docx", and press CTRL+A - for selecting all document elements, then go and mark: Format -> Paragraph -> Line and page break -> 'keep with next',

it will do the work and move all split tables into the same page.

Screenshot 2024-02-18 at 15.08.48.png


pls see tableExampleWord_afterKeepFollow.docx" attached file.

how can I implement it using your SpireDoc version 11.7.0?

adi.zahi
 
Posts: 7
Joined: Tue Jan 30, 2024 10:12 am

Mon Feb 19, 2024 9:14 am

Hello,
Thanks for your feedback.

On my side, using "setKeepFollow (true)" can achieve the requirements you mentioned. I have attached the complete test code and result files for your reference. If it still doesn't work on your end, please provide us with your result document so that we can further investigate. Thank you in advance.
Code: Select all
Document document = new Document();
document.loadFromFile("tableExampleWord.docx");
for (Object childObject : document.getSections().get(1).getBody().getChildObjects()) {
    if(childObject instanceof Table){
        Table table = (Table) childObject;
        int count = table.getRows().getCount();
        for (int i = 0; i < count; i++) {
            int count1 = table.getRows().get(i).getCells().getCount();
            for (int i1 = 0; i1 < count1; i1++) {
                ParagraphCollection paragraphs = table.getRows().get(i).getCells().get(i1).getParagraphs();
                for (Object obj : paragraphs) {
                    Paragraph paragraph = (Paragraph) obj;
                    paragraph.getFormat().setKeepFollow(true);
                }
            }
        }
    }
}
document.saveToFile("tableExampleWord-res.docx", FileFormat.Docx_2016);


Sincerely,
William
E-iceblue support team
User avatar

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

Return to Spire.Doc