简体   繁体   English

Apache poi 分页符

[英]Apache poi page breaks

I need to create a simple word document for printing from a java program.我需要创建一个简单的 word 文档以从 java 程序打印。 It is necessary to have the output printed on separate pages.有必要将输出打印在单独的页面上。 I'm using the following code:我正在使用以下代码:

 XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText("TITLE"); run.addCarriageReturn(); run.setText("some text and stuff here"); run.addBreak(BreakType.PAGE); run.setText("more text"); run.addCarriageReturn(); run.setText("one more line");

Trouble is, anything I try to insert after this page break appears on its own on a page (the last two lines of text in the above example will appear on different pages).问题是,在此分页符之后我尝试插入的任何内容都会单独出现在页面上(上例中的最后两行文本将出现在不同的页面上)。 It's as if there is now an 'auto-page-break' after every statement.就好像现在每个语句之后都有一个“自动分页符”。 I've tried using new run, or new paragraph, but the result is always the same.我试过使用新的运行或新的段落,但结果总是一样的。 Any ideas?有任何想法吗? Starting to get very frustrated here....在这里开始变得非常沮丧......

XWPFDocument document = new XWPFDocument();
...
XWPFParagraph paragraph = document.createParagraph();
paragraph.setPageBreak(true);

Have found an answer - not sure it's the best way.找到了答案 - 不确定这是最好的方法。 It's necessary to add a carriage return after the last line of the page, or it too moves to the next page.需要在页面最后一行后加一个回车,不然也跳到下一页。 Then add Break (WORD_WRAPPING), and start a new run for the next page.然后添加 Break (WORD_WRAPPING),并开始下一页的新运行。 (The only problem with this solution is it leaves a blank line at the top of each new page!) (此解决方案的唯一问题是它会在每个新页面的顶部留下一个空行!)

 XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText("TITLE"); run.addCarriageReturn(); run.setText("some text and stuff here"); run.addCarriageReturn(); //separate previous text from break run.addBreak(BreakType.PAGE); run.addBreak(BreakType.WORD_WRAPPING); //cancels effect of page break WXPFRun run2 = paragraph.createRun(); //create new run run2.setText("more text"); run2.addCarriageReturn(); run2.setText("one more line");

XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("Text");
run.addBreak(BreakType.PAGE);
XWPFRun run2 = paragraph.createRun();
run.setText("Another text");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM