2016-09-13 45 views
0

我想要编辑.doc(word)文档的标题。下面的代码,我已经写了:使用java在.doc中添加图像和编辑标题

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(); 
     } 
    } 
} 

但其没有工作,下面的代码执行后,它是不能够打开新的文件显示错误。我还需要在文档中提供的框中添加图片。任何机构有任何想法如何做到这一点?

下面是链接我也尝试:让同样的错误

Replacing variables in a word document template with java

enter image description here

+1

您使用的是哪个版本的Apache POI?如果不是最新的,你可以试试最新的? – centic

+0

Apache POI 3.13 –

回答

2

简短的回答是可能的,不幸的是:它不工作。

长的答案是:

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

(我开发了一个定制HWPF库,其固定的这一切在几年前,所以我知道详情的客户端。)

替代

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

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

+0

嗨,谢谢你的回应。现在我正在使用docx并能够替换文本。对于图像我跟着[这](http://stackoverflow.com/questions/19244822/replace-a-placeholder-with-image-in-word)和[this](http://stackoverflow.com/questions/17745466/insert-picture-in-word-document/17768732#17768732),但我的要求有点不同 - 它在第二页上有一个框,我必须插入图像。任何想法? –

+0

也许你有一个文本框。我现在无法查看详细信息,但可以在XWPFDocument或类似的类中搜索“文本框”。我假设会有一个文本框或文本框的子文档列表,你必须找到适当的框。 –