I have a Microsoft Word .docx document uploaded to Sharepoint. In my java code, I have downloaded this document into a byte[]. Ok. Now, what I want is to process this byte[] to obtain an XWPFDocument and be able to replace some variables into the document.
Please, could anybody help me?
Thanks!!
You can read as XWPFDocument from byte[] by using InputStream(ByteArrayInputStream) specified in the constructor of XWPFDocument and you can get paragraphs and runs from XWPFDocument. After that you can edit like below.
byte[] byteData = ....
// read as XWPFDocument from byte[]
XWPFDocument doc = new XWPFDocument(new ByteArrayInputStream(byteData));
int numberToPrint = 0;
// you can edit paragraphs
for (XWPFParagraph para : doc.getParagraphs()) {
List<XWPFRun> runs = para.getRuns();
numberToPrint++;
for (XWPFRun run : runs) {
// read text
String text = run.getText(0);
// edit text and update it
run.setText(numberToPrint + " " + text, 0);
}
}
// save it and you can get the updated .docx
FileOutputStream fos = new FileOutputStream(new File("updated.docx"));
doc.write(fos);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With