簡體   English   中英

如何使用 POI 將 HWPFDocument 轉換為輸入流

[英]How to convert HWPFDocument to inputstream using POI

我必須更改 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);

我將非常感謝您的幫助。

我知道這個問題很老,但是有一種簡單的方法可以將 XWPFDocument 轉換為 InputStream。 以防萬一有人需要它...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM