java - 如何使用 Apache POI 在 Word 中添加图像标题

标签 java apache apache-poi

我现在正在使用 Apache POI 生成一个 Word 文档,这是我所需要的,我需要将图像标题附加到该文档,我该如何做到这一点。这就是我所做的。

  public class SimpleDocument {

public static void main(String[] args) throws Exception {
    XWPFDocument doc = new XWPFDocument();

    XWPFParagraph p1 = doc.createParagraph();
    p1.setAlignment(ParagraphAlignment.CENTER);
    p1.setBorderBottom(Borders.DOUBLE);
    p1.setBorderTop(Borders.DOUBLE);

    p1.setBorderRight(Borders.DOUBLE);
    p1.setBorderLeft(Borders.DOUBLE);
    p1.setBorderBetween(Borders.SINGLE);

    p1.setVerticalAlignment(TextAlignment.TOP);

    XWPFRun r1 = p1.createRun();
    r1.setBold(true);
    r1.setText("The quick brown fox");
    r1.setBold(true);
    r1.setFontFamily("Courier");
    r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
    r1.setTextPosition(100);

    XWPFParagraph p2 = doc.createParagraph();
    p2.setAlignment(ParagraphAlignment.RIGHT);

    //BORDERS
    p2.setBorderBottom(Borders.DOUBLE);
    p2.setBorderTop(Borders.DOUBLE);
    p2.setBorderRight(Borders.DOUBLE);
    p2.setBorderLeft(Borders.DOUBLE);
    p2.setBorderBetween(Borders.SINGLE);

    XWPFRun r2 = p2.createRun();
    r2.setText("jumped over the lazy dog");
    r2.setStrike(true);
    r2.setFontSize(20);

    XWPFRun r3 = p2.createRun();
    r3.setText("and went away");
    r3.setStrike(true);
    r3.setFontSize(20);
    r3.setSubscript(VerticalAlign.SUPERSCRIPT);


    XWPFParagraph p3 = doc.createParagraph();
    p3.setWordWrap(true);
    p3.setPageBreak(true);

    //p3.setAlignment(ParagraphAlignment.DISTRIBUTE);
    p3.setAlignment(ParagraphAlignment.BOTH);
    p3.setSpacingLineRule(LineSpacingRule.EXACT);

    p3.setIndentationFirstLine(600);


    XWPFRun r4 = p3.createRun();
    r4.setTextPosition(20);
    r4.setText("To be, or not to be: that is the question: "
            + "Whether 'tis nobler in the mind to suffer "
            + "The slings and arrows of outrageous fortune, "
            + "Or to take arms against a sea of troubles, "
            + "And by opposing end them? To die: to sleep; ");
    r4.addBreak(BreakType.PAGE);
    r4.setText("No more; and by a sleep to say we end "
            + "The heart-ache and the thousand natural shocks "
            + "That flesh is heir to, 'tis a consummation "
            + "Devoutly to be wish'd. To die, to sleep; "
            + "To sleep: perchance to dream: ay, there's the rub; "
            + ".......");
    r4.setItalic(true);
//This would imply that this break shall be treated as a simple line break, and break the line    after that word:

    XWPFRun r5 = p3.createRun();
    r5.setTextPosition(-10);
    r5.setText("For in that sleep of death what dreams may come");
    r5.addCarriageReturn();
    r5.setText("When we have shuffled off this mortal coil,"
            + "Must give us pause: there's the respect"
            + "That makes calamity of so long life;");
    r5.addBreak();
    r5.setText("For who would bear the whips and scorns of time,"
            + "The oppressor's wrong, the proud man's contumely,");

    r5.addBreak(BreakClear.ALL);
    r5.setText("The pangs of despised love, the law's delay,"
            + "The insolence of office and the spurns" + ".......");

    FileOutputStream out = new FileOutputStream("F://simple.docx");
    doc.write(out);
    out.close();
    System.out.println("Doc Created");
    }
}

我想在正在生成的三个页面中附加一个图像。但是无法正确使用页眉页脚策略。请有人帮忙

最佳答案

我是这样做的...... 我不知道在新版本中是否可以从头开始将图像添加到标题中,但我知道"template"工作得非常完美。我在 Word 中创建了模板文档,按照我需要的方式调整了标题,并将文档的其余部分留空。创建XWPFDocument时,不要调用默认构造函数,而是:

XWPFDocument doc = new XWPFDocument(new FileInputStream("pathTo/template.docx"));

不仅仅是用您的内容填充新的 XWPFDocument 实例,如果您需要像我一样更新不同导出的 header 内容,请实现与此类似的方法:

public void updateHeader() throws InvalidFormatException, IOException {
        // load the header policy from template and update the paragraph text
        XWPFHeaderFooterPolicy headerFooterPolicy = document
                .getHeaderFooterPolicy();
        XWPFHeader defaultHeader = headerFooterPolicy.getDefaultHeader();
        defaultHeader.getParagraphs().get(0).getRuns().get(0)
                .setText("New Text", 0);
        // this is only to put some space between the content in the header and the real content
        defaultHeader.getParagraphs()
                .get(defaultHeader.getParagraphs().size() - 1)
                .setSpacingAfter(300);
    }

这从 3.10 开始有效,如果您偶然发现一些 java 安全问题,请尝试当前的每夜版本,其中许多安全问题已得到解决。对我来说,它甚至可以在 Google App Engine 上运行。

关于java - 如何使用 Apache POI 在 Word 中添加图像标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27523777/

相关文章:

java - 无法解析 org.apache.poi.poifs.crypt.dsig.SignatureInfo 类?

java - 将 POI 中的 Excel 单元格分组,并将第一行设置为 "main"

java - 为什么我不能在 Android 上使用 Override 注释?

java - Ajax 无法与 h :inputSecret 一起使用

java - 使用camel流式传输文件

java - 是否可以有一个 Eclipse 项目,其中一个包作为 Android 应用程序执行,第二个包作为 Java SE 应用程序执行?

java - Apache Tomcat 8.5.9 中未设置 X-Frame-Options header

python - 如何让 Pyramid 应用程序在 apache + mod_wsgi 上运行

java - 使用 Apache Poi 从固定位置的模板复制表

php - 如何在 PHP 中检查不完整的 POST 请求