【问题标题】:Copy contents from docx with bullets intact with Apache POI使用 Apache POI 从 docx 复制内容,项目符号完好无损
【发布时间】:2015-05-21 12:34:05
【问题描述】:

我最终试图将 docx 文件中的内容复制到剪贴板。到目前为止我想出的代码是:

package config;

public class buffer {

    public static void main(String[] args) throws IOException, XmlException {
        XWPFDocument srcDoc = new XWPFDocument(new FileInputStream("D:\\rules.docx"));

        XWPFDocument destDoc = new XWPFDocument();
        OutputStream out = new FileOutputStream("D:\\test.docx");

        for (IBodyElement bodyElement : srcDoc.getBodyElements()) {
            XWPFParagraph srcPr = (XWPFParagraph) bodyElement;
            XWPFParagraph dstPr = destDoc.createParagraph();
            dstPr.createRun();
            int pos = destDoc.getParagraphs().size() - 1;
            destDoc.setParagraph(srcPr, pos);
        }

        destDoc.write(out);
        out.close();
    }
}

这确实会获取项目符号,但会对其进行编号。我想保留原来的项目符号格式。有没有办法做到这一点?

【问题讨论】:

    标签: java apache-poi docx docx4j


    【解决方案1】:

    您需要正确处理编号定义(在编号部分)。

    最可靠的做法是复制定义(实例列表和抽象列表),然后重新编号(即给它一个新的 ID),使其唯一。

    当然,您需要更新段落中的 ID 以匹配。

    请注意,以上只是针对您提出的问题的解决方案。

    如果您的内容包含与其他部分(例如图像)的关联,您会遇到问题。而且您没有处理样式定义等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      相关资源
      最近更新 更多