[英]Word autoformat with Apache-POI
I'd like to use the autoformat feature of word with Apache-POI in an XWPFDocument. 我想在XWPFDocument中将word的自动格式化功能与Apache-POI一起使用。
By autoformat I mean, if you type eg "---" and press return, a horizontal line is drawn across the page of the word document. 通过自动套用格式,我的意思是,如果您键入“ ---”并按回车键,则会在word文档的页面上绘制一条水平线。
I'd like to use this in a header. 我想在标题中使用它。
I tried 我试过了
XWPFHeader header = doc.createHeader(HeaderFooterType.FIRST);
paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
run = paragraph.createRun();
run.setText("---\r");
or 要么
run.setText("---\r\n");
or 要么
run.setText("---");
run.addCarriageReturn();
None of those work. 这些都不起作用。
Is it even possible to use the autoformat function with POI? POI甚至可以使用自动格式化功能吗?
Regards, Maik 问候,迈克
I'm using POI 4.0.0, btw... 我正在使用POI 4.0.0,顺便说一句...
The Autoformat is a feature of Word
's GUI. 自动套用格式是Word
的GUI的功能。 But apache poi
is creating what is stored in a *.docx
file. 但是apache poi
正在创建存储在*.docx
文件中的内容。 After the Autoformat has replaced "---" Enter with a bottom border line of the paragraph, only this bottom border line of the paragraph is stored in the file. 在自动套用格式将“ ---” Enter替换为段落的底部边框线之后,仅该段落的底部边框线存储在文件中。
So: 所以:
import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.wp.usermodel.HeaderFooterType;
public class CreateWordHeader {
public static void main(String[] args) throws Exception {
XWPFDocument doc = new XWPFDocument();
// the body content
XWPFParagraph paragraph = doc.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("The Body...");
// create header
XWPFHeader header = doc.createHeader(HeaderFooterType.FIRST);
paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
run = paragraph.createRun();
run.setText("First Line in Header...");
// bottom border line of the paragraph = what Autoformat creates after "---"[Enter]
paragraph.setBorderBottom(Borders.SINGLE);
paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
run = paragraph.createRun();
run.setText("Next Line in Header...");
FileOutputStream out = new FileOutputStream("CreateWordHeader.docx");
doc.write(out);
doc.close();
out.close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.