如何使用POI将HWPFDocument转换为输入流

时间:2014-12-18 11:02:16

标签: java

我必须更改doc word中的文本并转换为pdf。 我已经更改了文本,但IDK,如何将HWPFDocument转换为pdf。

在调用 XWPFDocument document = new XWPFDocument(is); 我得到异常 io.IOException:Stream Closed

String inputFilename = "/root/GeneratorUmow/web/WEB-INF/umowy/kkb/wniosekkkb.doc";
POIFSFileSystem fs = null;
is = new FileInputStream(inputFilename);
fs = new POIFSFileSystem(is);
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
range.replaceText("nazwaFirmy", "KAKAOWY SZATAN");
//conversion na pdf
XWPFDocument document = new XWPFDocument(is);
PdfOptions options = PdfOptions.create().fontEncoding("windows-1250");
OutputStream out = new FileOutputStream(new File("kakaowyszal.pdf"));
PdfConverter.getInstance().convert(document, out, options);

我将非常感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

我知道这个问题很老但是有一种简单的方法可以将XWPFDocument“转换”为SnputStream。万一有人需要它......

ByteArrayOutputStream b = new ByteArrayOutputStream();
doc.write(b); // doc should be a XWPFDocument 
InputStream inputStream = new ByteArrayInputStream(b.toByteArray());