removeContentControls( document);
List<SectPr> sectPrs = getSectPrs(document);
// Local vars
List<ConversionSectionWrapper> conversionSections = new ArrayList<ConversionSectionWrapper>();
ConversionSectionWrapper currentSectionWrapper = null;
HeaderFooterPolicy previousHF = null;
int conversionSectionIndex = 0;
List<Object> sectionContent = new ArrayList<Object>();
// Now go through the document content,
int sectPrIndex = 0; // includes continuous ones
for (Object o : document.getBody().getContent() ) {
if (o instanceof org.docx4j.wml.P) {
if (((org.docx4j.wml.P)o).getPPr() != null ) {
org.docx4j.wml.PPr ppr = ((org.docx4j.wml.P)o).getPPr();
if (ppr.getSectPr()!=null) {
/* If the *following* section is continuous,
* don't add *this* section, because we want our
* sectionWrapper to surround the content preceding
* both sectPr.
*
* When we do eventually create that section wrapper,
* it must use the header/footer settings from
* *this* section.
*
* There is a special case to consider:
*
<w:sectPr>
<w:type w:val="continuous"/>
<w:pgSz <---- different values from previous sectPr
*
* In this case, Word will render a page break,
* but:
* 1. still show it as continuous
* 2. still use the headers/footers from this section
*
*/
boolean ignoreThisSection = false;
SectPr followingSectPr = sectPrs.get(++sectPrIndex);
if ( followingSectPr.getType()!=null
&& followingSectPr.getType().getVal().equals("continuous")) {
log.info("following sectPr is continuous; this section wrapper must include its contents ");
ignoreThisSection = true;
}
if (ignoreThisSection) {
// In case there are some headers/footers that apply to both this content and the
// content before the continuous sectPr,
// or that need to get inherited by the section after the continuous sectPr
previousHF = new HeaderFooterPolicy(ppr.getSectPr(), previousHF, rels, evenAndOddHeaders);
PgSz pgSzThis = ppr.getSectPr().getPgSz();
PgSz pgSzNext = followingSectPr.getPgSz();
if (insertPageBreak( pgSzThis, pgSzNext)) {
ppr.setPageBreakBefore(new BooleanDefaultTrue());
}
//ppr.setSectPr(null); // Don't do this, since we have to process the docx (inc sectPrs) multiple times for a single PDF output
} else {
currentSectionWrapper = createSectionWrapper(
ppr.getSectPr(), previousHF, rels, evenAndOddHeaders,
++conversionSectionIndex, sectionContent, dummyPageNumbering);
conversionSections.add(currentSectionWrapper);
previousHF = currentSectionWrapper.getHeaderFooterPolicy();
sectionContent = new ArrayList<Object>();
}
}
}