我正在使用 apache POI.XWPF 库来创建 word 文档。在过去的几天里,我一直在寻找如何为整个文档添加双空格。我已经检查了 Apache javadocs,只是通过搜索互联网,但似乎找不到任何答案。
我找到了该addBreak()
方法,但它不起作用,因为用户将输入多个段落并将这些段落分成单行似乎不合理。如果每个段落都使用此方法,那么它不会在每行之间而是在每个段落之间创建双倍空格。
这是我目前拥有的一小部分代码。
public class Paper {
public static void main(String[] args) throws IOException, XmlException {
ArrayList<String> para = new ArrayList<String>();
para.add("The first paragraph of a typical business letter is used to state the main point of the letter. Begin with a friendly opening; then quickly transition into the purpose of your letter. Use a couple of sentences to explain the purpose, but do not go in to detail until the next paragraph.");
para.add("Beginning with the second paragraph, state the supporting details to justify your purpose. These may take the form of background information, statistics or first-hand accounts. A few short paragraphs within the body of the letter should be enough to support your reasoning.");
XWPFDocument document = new XWPFDocument();
//Calls on createParagraph() method which creates a single paragraph
for(int i=0; i< para.size(); i++){
createParagraph(document, para.get(i));
}
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream("ResearchPaper.docx");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
document.write(outStream);
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//Creates a single paragraph with a one tab indentation
private static void createParagraph(XWPFDocument document, String para) {
XWPFParagraph paraOne = document.createParagraph();
paraOne.setFirstLineIndent(700); // Indents first line of paragraph to the equivalence of one tab
XWPFRun one = paraOne.createRun();
one.setFontSize(12);
one.setFontFamily("Times New Roman");
one.setText(para);
}
}
为了确保我的问题很清楚,我试图找出如何将 word 文档 (.docx) 加倍空格。所以每一行之间应该有一行空格。这与编辑 word 文档时按 ctrl+2 相同。
感谢您的任何帮助。