如何仅在最后一页设置页脚差异。我是说,在最后一页里没有页脚也没有不同之处。在第一页和其他都是一样的。
在这里输入链接描述
这是我的文档
发布于 2022-03-03 07:25:37
Microsoft中的最后一页不存在页眉/页脚。
有不同的页眉/页脚设置可能对第一页,甚至页和默认页。在这里,默认的页眉/页脚将被用于每个页面,如果没有其他页面的话。因此,如果偶数页有页眉/页脚,奇数页和偶数页将不同,则默认页眉/页脚仅用作奇数页的页眉/页脚。如果没有这样的设置,那么每个页面都会使用默认的页眉/页脚。这是除了第一页,如果有一个特殊的页眉/页脚的第一页集。
但是Word文档可以分为不同的部分。而且每个部分都可能有自己的页眉/页脚设置。因此,如果并且只有当最后一页在它自己的部分中时,那么只能为最后一页(最后一节)有一个不同的页眉/页脚。
不幸的是,apache poi没有提供在XWPF中设置节的方法。它只提供了设置文档中三种不同的页眉/页脚类型的方法。因此,要将XWPFDocument分离为各节,并为这些节设置不同的页眉/页脚设置,就需要进行一点欺骗。
您可以创建三种可能的页眉/页脚类型之一,只是将其设置为以后某个节的默认页眉/页脚。下面的示例说明了这一点。它为文档中的偶数页创建了一个页脚,这些页脚从未被使用过,但后来被引用为最后一节的默认页脚。注意代码中的进一步注释。
当然,只有在至少知道最后一页从哪里开始的情况下,这才能工作。这必须是已知的,因为分段中断需要在lase页面的内容之前设置。
import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.wp.usermodel.HeaderFooterType;
public class CreateWordDifferentFooters {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph;
XWPFRun run;
XWPFFooter footer;
//create footers
//footer for first page
footer = document.createFooter(HeaderFooterType.FIRST);
paragraph = footer.createParagraph();
run = paragraph.createRun();
run.setText("Footer FIRST");
//default footer = footer for each what is not else defined
footer = document.createFooter(HeaderFooterType.DEFAULT);
paragraph = footer.createParagraph();
run = paragraph.createRun();
run.setText("Footer DEFAULT");
//footer for even pages - gets default footer for page in last section later
footer = document.createFooter(HeaderFooterType.EVEN);
paragraph = footer.createParagraph();
run = paragraph.createRun();
run.setText("Footer EVEN = DEFAULT in last section");
//the body content
//section 1
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("First page in first section ...");
paragraph = document.createParagraph();
run = paragraph.createRun();
run.addBreak(BreakType.PAGE);
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("Second page in first section ...");
paragraph = document.createParagraph();
run = paragraph.createRun();
run.addBreak(BreakType.PAGE);
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("Third page in first section ...");
paragraph = document.createParagraph();
//paragraph with section setting for section above and section break
paragraph = document.createParagraph();
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr ctSectPrSect1 = paragraph.getCTP().addNewPPr().addNewSectPr(); //we need ctSectPrSect1 later
//section 2
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("Fourth and last page. Only page in last section ...");
//section setting for section above = last section in document
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1 ctDocument = document.getDocument();
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody ctBody = ctDocument.getBody();
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr ctSectPrLastSect = ctBody.getSectPr(); //there must be a SectPr already because of the footer settings above
//move first and default footer to section 1
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtrRef ctHdrFtrRef0 = ctSectPrLastSect.getFooterReferenceArray(0); // first footer
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtrRef ctHdrFtrRef1 = ctSectPrLastSect.getFooterReferenceArray(1); // default footer
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtrRef[] ctHdrFtrRefs = new org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtrRef[]{ctHdrFtrRef0, ctHdrFtrRef1};
ctSectPrSect1.setFooterReferenceArray(ctHdrFtrRefs);
//set "there is a title page" for section 1 to make first footer work
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff ctOnOff = org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff.Factory.newInstance();
ctOnOff.setVal(true);
ctSectPrSect1.setTitlePg(ctOnOff);
//set footer reference of even footer to be default footer reference for last section
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtrRef ctHdrFtrRef = ctSectPrLastSect.getFooterReferenceArray(2);
ctHdrFtrRef.setType(org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr.DEFAULT); //change this from STHdrFtr.EVEN to STHdrFtr.DEFAULT
//unset "there is a title page" for the last section
ctSectPrLastSect.unsetTitlePg();
//remove first and old default footer references from last section
ctSectPrLastSect.removeFooterReference(1);
ctSectPrLastSect.removeFooterReference(0);
FileOutputStream out = new FileOutputStream("./CreateWordDifferentFooters.docx");
document.write(out);
out.close();
document.close();
}
}这是测试和工作使用当前apache poi 5.2.0。
https://stackoverflow.com/questions/71325868
复制相似问题