使用 java 在 .doc 中添加图像和编辑 headers
Adding images and editing headers in .doc using java
我想编辑 header .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();
}
}
}
但是它不起作用,执行下面的代码后,它无法打开显示错误的新文档。我还需要在文档中提供的框中添加图片。 body 有知道怎么做的吗?
以下是我也尝试过的链接:
Replacing variables in a word document template with java
出现同样的错误:
简短的回答可能是不幸的:它不起作用。
长答案是:
HWPF 处于不完整状态,很多东西不受支持(我上次查看可能是一年前)。 .doc 文件格式是一种复杂的二进制文件格式。许多表的条目指向文档中的特定位置。更改文档的一部分通常需要更新所有表格。有用于文本 运行 的表格、文本框、书签、形状、表格(行和列)等等。如果幸运的话,您有一个非常简单的文档,而大多数复杂的表格都不存在。但是,当您有形状、图像、文本框等时,您可能 运行 进入 HWPF 尚未/未正确支持的内容。然后输出通常是一个无效的 Word 文件,它被 Word 拒绝(如果你幸运的话)或者它或多或少地使 Word 崩溃(可能需要重新启动计算机)。
(我为客户开发了一个自定义的 HWPF 库,几年前修复了所有这些问题。因此我知道细节。)
备选方案
您可能希望查看 .docx
格式而不是 .doc
。如果你能安排得到.docx
个文件,你可以使用XWPF,状态会好很多。
关于headers:据我所知,headers不在主文档中。您需要查看 headers 子文档。 (我相信是 XWPFHeader
?)
我想编辑 header .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();
}
}
}
但是它不起作用,执行下面的代码后,它无法打开显示错误的新文档。我还需要在文档中提供的框中添加图片。 body 有知道怎么做的吗?
以下是我也尝试过的链接:
Replacing variables in a word document template with java
出现同样的错误:
简短的回答可能是不幸的:它不起作用。
长答案是:
HWPF 处于不完整状态,很多东西不受支持(我上次查看可能是一年前)。 .doc 文件格式是一种复杂的二进制文件格式。许多表的条目指向文档中的特定位置。更改文档的一部分通常需要更新所有表格。有用于文本 运行 的表格、文本框、书签、形状、表格(行和列)等等。如果幸运的话,您有一个非常简单的文档,而大多数复杂的表格都不存在。但是,当您有形状、图像、文本框等时,您可能 运行 进入 HWPF 尚未/未正确支持的内容。然后输出通常是一个无效的 Word 文件,它被 Word 拒绝(如果你幸运的话)或者它或多或少地使 Word 崩溃(可能需要重新启动计算机)。
(我为客户开发了一个自定义的 HWPF 库,几年前修复了所有这些问题。因此我知道细节。)
备选方案
您可能希望查看 .docx
格式而不是 .doc
。如果你能安排得到.docx
个文件,你可以使用XWPF,状态会好很多。
关于headers:据我所知,headers不在主文档中。您需要查看 headers 子文档。 (我相信是 XWPFHeader
?)