我需要在打印一些段落后插入分页符。我找到了一个插入分页符的函数,但它只插入一个空段而不是分页符。
这是功能:
private static void addPageBreak() {
MainDocumentPart documentPart = wp.getMainDocumentPart();
Br breakObj = new Br();
breakObj.setType(STBrType.PAGE);
P paragraph = factory.createP();
paragraph.getContent().add(breakObj);
documentPart.getJaxbElement().getBody().getContent().add(paragraph);
}
如何修复此功能以插入完整的分页符?
答案 0 :(得分:5)
在运行中添加w:br,而不是直接在w:p
中添加P p = wmlObjectFactory.createP();
// Create object for r
R r = wmlObjectFactory.createR();
p.getContent().add( r);
// Create object for br
Br br = wmlObjectFactory.createBr();
r.getContent().add( br);
br.setType(org.docx4j.wml.STBrType.PAGE);
如果您的docx包含要复制的内容,则可以使用docx4j webapp生成类似的代码。
答案 1 :(得分:1)
确保正确调用addPageBreak()方法。请尝试以下代码来测试您的方法:
public class AddingAPageBreak {
private static ObjectFactory factory;
private static WordprocessingMLPackage wordMLPackage;
public static void main (String[] args) throws Docx4JException {
wordMLPackage = WordprocessingMLPackage.createPackage();
factory = Context.getWmlObjectFactory();
wordMLPackage.getMainDocumentPart().addParagraphOfText("Hello Word!");
addPageBreak();
wordMLPackage.getMainDocumentPart().addParagraphOfText("This is page 2!");
wordMLPackage.save(new java.io.File("src/main/files/HelloWord11.docx") );
}
/**
* Adds a page break to the document.
*/
private static void addPageBreak() {
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
Br breakObj = new Br();
breakObj.setType(STBrType.PAGE);
P paragraph = factory.createP();
paragraph.getContent().add(breakObj);
documentPart.getJaxbElement().getBody().getContent().add(paragraph);
}
}
答案 2 :(得分:0)
documentPart.getJaxbElement().getBody().getContent().add(paragraph);
方式似乎根本不起作用。
您是否尝试过使用documentPart.addObject(paragraph);
?
请参阅在两段之间插入分页符的完整示例:
public class PageBreakExample {
private static ObjectFactory objectFactory = new ObjectFactory();
public static void main(String[] args) throws InvalidFormatException,
Docx4JException {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
// create new paragraph with a run containing text and add it to the document.
P paragraph1 = objectFactory.createP(); // create new paragraph
R run1 = objectFactory.createR(); // create new run
Text text1 = objectFactory.createText(); // create text
text1.setValue("This is text in paragraph 1");
run1.getContent().add(text1); // add text ton the run
paragraph1.getContent().add(run1); // add run to paragraph
wordMLPackage.getMainDocumentPart().addObject(paragraph1);
addPageBreak(wordMLPackage.getMainDocumentPart());
// proceed to create another paragraph with a run containing text.
P paragraph2 = objectFactory.createP(); // create new paragraph
R run2 = objectFactory.createR(); // create new run
Text text2 = objectFactory.createText(); // create text
text2.setValue("This is text in paragraph 2");
run2.getContent().add(text2); // add text ton the run
paragraph2.getContent().add(run2); // add run to paragraph
wordMLPackage.getMainDocumentPart().addObject(paragraph2); // add to main document part
wordMLPackage.save(new java.io.File("two_paragraphs_page_break.docx")); // save
}
private static void addPageBreak(MainDocumentPart documentPart) {
P paragraph = objectFactory.createP();
R run = objectFactory.createR();
P p = objectFactory.createP();
// Create object for r
R r = objectFactory.createR();
p.getContent().add(r);
// Create object for br
Br br = objectFactory.createBr();
r.getContent().add(br);
br.setType(org.docx4j.wml.STBrType.PAGE);
documentPart.addObject(p);
}
}