Page 1 of 1

How to combine paragraph to avoid page break

PostPosted: Wed Mar 28, 2012 8:32 pm
by yogen229
Hi,
I am generating a word document which contains several logical sections. Each logical section has its title and table. These section are cretaed at run time as per the input data. Since document is genrated run time and data is not fixed it happens some time that a section title comes on different page ( say page1) and table comes at different page (say page2). I want to display both tilte and table together at single page. How to do that ?

Re: How to combine paragraph to avoid page break

PostPosted: Wed Mar 28, 2012 9:04 pm
by jason
You need the paragraph property keepNext on the title paragraph:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
        <w:p>
            <w:pPr>
                <w:keepNext/>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


Use method setKeepNext.

http://grepcode.com/file/repo1.maven.or ... ultTrue%29

Re: How to combine paragraph to avoid page break

PostPosted: Thu Mar 29, 2012 6:09 pm
by yogen229
jason wrote:You need the paragraph property keepNext on the title paragraph:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
        <w:p>
            <w:pPr>
                <w:keepNext/>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


Use method setKeepNext.

http://grepcode.com/file/repo1.maven.or ... ultTrue%29


Thanks,
I did that, but still table is splitting across page. What I want is if title and table does not fit for current page , both should move on to next page so that tiltle and table always views together. Actually I am using a pre created .docx file as base(template) an in that .docx file I have selected title paragarph and checked "Keep Next" true. But what it do it keeps header and other initial rows on same page and rest moved to next page. But I want Tiltle and whole table together on same page.

Re: How to combine paragraph to avoid page break

PostPosted: Thu Mar 29, 2012 7:00 pm
by jason
As you'll see if you Google "word keep table rows together", you need to do 2 things:

1. Prevent rows from breaking internally, by setting the tr property “Allow row to break across pages.” to false

2. Keep rows together. Select the entire row and enable the “Keep with next” property. Do this for every row of the table except the last. I think this has the effect of setting w:keepNext on each w:p in the table, but you should try it in Word and look at the resulting XML to confirm.

Perhaps you could post your resulting code for the benefit of others? thanks.

Re: How to combine paragraph to avoid page break

PostPosted: Fri Mar 30, 2012 3:21 pm
by yogen229
Thanks, I found the way to do it in code.
Code: Select all
      
            // 1. Iterate through all rows of table and select last row.
            // 2. Set KepNext to false for last row of table.
            
            // 1. Iterate through all rows of table
            for( int i = 0; i < theRowCount; i++ ) {
               org.docx4j.wml.Tr tr = createSimilarNewTableRow(refTr, cols);
               tbl.getContent().add(tr);
               // 1.1 Select last row only
               if(i == theRowCount -1){
               // 2.1 Iterate through all content (Type org.docx4j.wml.Tc) of last row of table.
                  for( Object trContent : tr.getContent() ) {
                     if( trContent instanceof javax.xml.bind.JAXBElement ) {
                        if( ((JAXBElement) trContent).getDeclaredType().getName().equals("org.docx4j.wml.Tc") ) {
                           org.docx4j.wml.Tc tc = (org.docx4j.wml.Tc) ((JAXBElement) trContent).getValue();
                           if(tc != null){
                           // 2.2 Iterate through all para (Type org.docx4j.wml.P) of each content of last row of table.
                              for( Object tcContent : tc.getContent() ) {
                                 if(tcContent != null && tcContent instanceof P){
                                    P tcP = (P)tcContent;
                                    // 2.3 Set Para property KeepNext to false.
                                    BooleanDefaultTrue bd = new BooleanDefaultTrue();
                                    bd.setVal(false);
                                    tcP.getPPr().setKeepNext(bd);
                                 }
                              }
                           }
                        }

                     }
                  }
               }
            }

Re: How to combine paragraph to avoid page break

PostPosted: Fri Mar 30, 2012 3:25 pm
by yogen229
BUT BEFORE THTA YOU NEED TO MAKE SURE THAT IN YOUR BASE (TEMPLATE) WORD DOCUMENT TITLE PARAGRAPH AND TABLE ROWS (ALL) SHOULD HAVE PROPERTY KEEPNEXT CHECKED