【问题标题】:poi XWPF double spacepoi XWPF 双倍空间
【发布时间】:2015-06-29 22:15:42
【问题描述】:

我正在使用 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是一样的。

感谢您的帮助。

【问题讨论】:

    标签: java apache apache-poi xwpf


    【解决方案1】:

    似乎没有高级方法可用于您要实现的目标。在这种情况下,您需要深入研究 Apache POI 的低级 API。下面是一种方法。我并不是说这是最好的方法,我只是在我想重新创建 MS Word 的一些奇怪功能时才发现它对我有用。

    1。找出信息在文档中的存储位置

    如果您需要手动调整某些内容,请创建 2 个内容尽可能少的文档:一个包含您想要做的事情,另一个不包含。将两者都保存为 Office XML 文档,因为这样便于阅读。区分这些文件 - 应该只有少量更改,并且您应该在文档结构中找到您的位置。

    对于您的情况,这就是您要寻找的东西。

    未修改的文档:

    <w:body><w:p> <!-- only included here so you know where to look -->
    <w:pPr>
        <w:jc w:val="both" />
        <w:rPr>
            <w:lang w:val="nl-BE" />
        </w:rPr>
    </w:pPr>
    

    修改文档:

    <w:body><w:p>
    <w:pPr>
        <w:spacing w:line="480" w:lineRule="auto" /> <!-- BINGO -->         
        <w:jc w:val="both" />
        <w:rPr>
            <w:lang w:val="nl-BE" />
        </w:rPr>
    </w:pPr>
    

    所以现在您知道您需要一个称为间距的对象,它具有一些属性,并且存储在 Paragraph 对象的某个位置。

    2。尽可能通过低级 API 到达该位置

    这部分很棘手,因为 XML 节点名称有些晦涩难懂,而且您可能不太了解这些术语。此外,API 并不总是节点名称的 1:1 映射,因此您必须进行一些猜测并尝试逐步执行方法调用。专业提示:下载 Apache POI 的源代码!你会踏入一些死胡同,你可能无法通过最短的路径到达你想到达的地方,但当你这样做时,你会感觉自己是一个神秘的 POI 大师。然后你在问答网站上写关于它的幸灾乐祸的帖子。

    对于您在 MS Word 中的情况,这是您可能采用的路径(不一定是最好的,我不是高级 API 方面的专家):

    // you probably don't need this first line
    // but you'd need it if you were manipulating an existing document
    IBody body = doc.getBodyElements().get(0).getBody();
    for (XWPFParagraph par : body.getParagraphs()) {
        // work the crazy abbreviated API magic
        CTSpacing spacing = par.getCTP().getPPr().getSpacing();
        if (spacing == null) {
            // it looks hellish to create a CTSpacing object yourself
            // so let POI do it by setting any Spacing parameter
            par.setSpacingLineRule(LineSpacingRule.AUTO);
            // now the Paragraph's spacing shouldn't be null anymore
            spacing = par.getCTP().getPPr().getSpacing();
        }
        // you can set your value, as demonstrated by the XML
        spacing.setAfter(BigInteger.valueOf(480));
        // not sure if this one is necessary
        spacing.setLineRule(STLineSpacingRule.Enum.forString("auto"));
    }
    

    3。沐浴在你的荣耀中!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-02
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多