0

I have been trying to edit different types of documents using Apache POI. The script should handle both extensions .doc and .docx. I could successfully edit the .docx file using XWPF api and the required text was added at the end of the docx file.

For editing .doc files(which include header, footer and a few paragraphs), following script is used, which use HWPFDocument.

 FileInputStream fis = new FileInputStream(args[0]);

 POIFSFileSystem fs = new POIFSFileSystem(fis);
 HWPFDocument doc = new HWPFDocument(fs);

 Range range = doc.getRange();
 CharacterRun run = range.insertAfter("FROM SEHWAGGG A FOUUURRRRRR");
 run.setBold(true);
 run.setItalic(true);

The script works fine with normal documents which does not have header and footer. But seems that the issue appears with complex documents. It insert text, but in between the paragraphs (and at the beginning using insertBefore()). There are no text replacements required, just have to put the text at the end of the document. I searched similar scripts but most of them handle text replacement.

How can I add the text at the end, after all paragraphs?

3
  • hmm..okay, Actually the documents I work with include header and footer and have 4 or 5 pages. Let me check with different documents. Commented Nov 20, 2015 at 11:42
  • I'm not totaly sure if I got your question right. This is the document before: imgur.com/FH9SVkx And this is the modification your code applies: imgur.com/Nt7XroL Is it what you expected? Commented Nov 20, 2015 at 11:46
  • You are right, but the problem appears with complex documents having header, footer and a few paragraphs. A way to identify the end of the document is needed in this case, then insert the text. Commented Nov 20, 2015 at 11:51

2 Answers 2

1

I've tested It with the following document:

enter image description here

At first (with your original code) it completely destroyed the document:

enter image description here

By changing the following line, the insert works fine for me:

// Old
Range range = doc.getEndnoteRange();
// New
Range range = doc.getEndnoteRange();

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

I'm afraid you are out of luck with HWPF with the current state of the project.

I created a custom HWPF library for one of our clients, but the changes are not public. The changes were huge, so you can't spend - say - a week and assume that things will be fixed. You might get away with the current public HWPF when only some text needs to be replaced without changing the string length ("abc" -> "123" or "a " -> "1234").

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.