使用java在.doc中添加图像和编辑标题

时间:2016-09-13 10:56:00

标签: java apache-poi hwpf

我想编辑.doc(word)文档的标题。我在下面的代码中写道:

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Section;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

public class WordReplaceText {
    public static final String SOURCE_FILE = "C:\\Users\\609650323\\Desktop\\Files\\Project\\GFAST\\surveyPack.doc";
    public static final String OUTPUT_FILE = "C:\\Users\\609650323\\Desktop\\Files\\Project\\GFAST\\surveyPack2.doc";

    public static void main(String[] args) throws Exception {
        WordReplaceText instance = new WordReplaceText();
        HWPFDocument doc = instance.openDocument(SOURCE_FILE);
        if (doc != null) {
            doc = instance.replaceText(doc,  "${A}", "AField");
            instance.saveDocument(doc, OUTPUT_FILE);

        }

    }

    private HWPFDocument replaceText(HWPFDocument doc, String findText, String replaceText) {
        Range r = doc.getRange();
        for (int i = 0; i < r.numSections(); ++i) {
            Section s = r.getSection(i);
            for (int j = 0; j < s.numParagraphs(); j++) {
                Paragraph p = s.getParagraph(j);
                for (int k = 0; k < p.numCharacterRuns(); k++) {
                    CharacterRun run = p.getCharacterRun(k);
                    String text = run.text();
                    if (text.contains(findText)) {
                        run.replaceText(findText, replaceText);
                    }
                }
            }
        }
        return doc;
    }

    private HWPFDocument openDocument(String file) throws Exception {
        URL res = getClass().getClassLoader().getResource(file);
        HWPFDocument document = null;
        if (res != null) {
            document = new HWPFDocument(new POIFSFileSystem(new File(res.getPath())));
        }else
            document = new HWPFDocument(new POIFSFileSystem(new File(SOURCE_FILE)));
        return document;
    }

    private void saveDocument(HWPFDocument doc, String file) {
        try {
            FileOutputStream out = new FileOutputStream(file);
            doc.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

但它不起作用,在执行下面的代码后,它无法打开显示错误的新文档。另外,我需要在文档中提供的框中添加pic。有没有人知道如何做到这一点?

以下是我也尝试过的链接:

Replacing variables in a word document template with java

获得相同的错误:

enter image description here

1 个答案:

答案 0 :(得分:2)

简短的答案很可能而且不幸的是:它不起作用。

答案很长:

HWPF处于不完整状态,并且不支持许多事情(上一次我看起来可能是一年前)。 .doc文件格式是一种复杂的二进制文件格式。存在许多表,其中条目指向文档中的某些位置。更改文档的一部分通常需要更新所有表。有用于文本运行,文本框,书签,形状,表(行和列)等的表格。如果你很幸运,你有一个非常简单的文档,大多数复杂的表都不存在。但是,当您有形状,图像,文本框等时,您可能会遇到HWPF中尚未/不正确支持的事物。然后输出通常是一个无效的Word文件,它被Word拒绝(如果你很幸运)或者它或多或少地崩溃了Word(可能需要重新启动计算机)。

(我为客户开发了一个自定义HWPF库,几年前修复了所有这些。因此我知道了详细信息。)

替代

您可能希望查看.docx格式而不是.doc。如果您可以安排获取.docx个文件,则可以使用处于更好状态的XWPF。

关于标题:据我记忆,标题不在主文档中。您需要查看标题子文档。 (我相信它是XWPFHeader?)