此处以docx文件通过代码的方式首页插入分页为例:
直接上码:
package test;
import java.io.File;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.QName;
public class test {
public static void main(String[] args) {
try {
dom4j();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void dom4j() throws Exception {
String readFileToString = FileUtils.readFileToString(new File("C:/Users/rr/DeskTop/document.xml"), "UTF-8");
Document doc = DocumentHelper.parseText(readFileToString);
Element rootElement = doc.getRootElement();
System.out.println(rootElement.getStringValue());
boolean insertPage = false;
List<Element> elements = rootElement.elements();
for (Element element : elements) {
if ("body".equals(element.getName()) &&a