aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes Editing pdf/word content (text replacement) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Editing pdf/word content (text replacement)" Watch "Editing pdf/word content (text replacement)" New topic
Author

Editing pdf/word content (text replacement)

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 358
Hi,
I am trying to work on a project that would allow me to do text replacement in pdf & word file.
I spend some time using iText to edit pdf(doing text replacement), but it does not work efficiently. The formatting is lost, words overlap etc.

I am now trying to do similar thing in word document. I am trying to use poi project from poi.apache.org. I am not able to do text replacement or editing existing word document.

Any suggestion / API help will be appreciated.


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42918
    
  68
PDFs are not meant for editing, certainly not for altering the textual contents. Adobe's Acrobat Pro can do that, but I'm not aware of any free Java libraries for that.

My last post in this topic suggests a solution for DOC files.

It'll probably be easier if you were to do this using RTF files. This discussion should also be of interest: http://www.coderanch.com/t/278413/Streams/java/apache-POI-HWPF-search-replace
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 358
I am trying to use the code posed on the following URLs

http://test.javaranch.com/ulf/ParagraphText.java and http://test.javaranch.com/ulf/InsertText.java

When i run the program , i am generating output file : Copy.doc. When i try to open the file it give me the following error: word was unable to read this document. It may be corrupt

Please suggest what needs to be done.

Regards.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42918
    
  68
I'm surprised you even got the code to compile; that means you're using an old version of POI. When I tried it just now I had to replace "usesUnicode" by "isUnicode" to get it compile against POI 3.7. Then I was able to create and open the output file using both LibreOffice and Mellel just fine (I don't have Word around). And, also important, the replacement was done correctly :-)

Try upgrading to the latest POI version; each new version has lots of bug fixes.
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 358
I am using poi3.7. I also made the change replace "usesUnicode" by "isUnicode".

Following is my code

InsertText


ParagraphText


Not sure if i have to make some other change to make this thing work correctly.
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 358
I am working on poi 3.7 and running the test case testRangeInsertion from file TestRangeInsertion.java. I added the following lines of code


and the final function is as follows:



I am not able to open the output file generated from the above test case. Any suggestions. This problem is similar to the above problem. I am not able to understand why the newly created file is corrupted/not opening using MS word.
Mohit Uppal
Greenhorn

Joined: Jun 22, 2009
Posts: 3
Hello Naresh Chaurasia,

I am able to read and edit document and also the code you posted is making new document file but that document file
is not able to open in MS Word 2007. Did you find any solution to problem ??

please reply... Its urgent...

Thanks

Mohit Uppal
Greenhorn

Joined: Jun 22, 2009
Posts: 3
Hello Ulf Dittmer,

As you have said that you are able to create and open the output file using both LibreOffice and Mellel just fine
and you don't have MS Word around. So if you can open the file with MS word and check if you are able to do that.
Because i am not able to open it with Office 2007.

I have used POI 3.7 and POI 3.8 Beta 3, same result with both APIs

Thanks and Please reply

Urgent....

Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Mohit Uppal wrote:Urgent....

Please EaseUp. There's no such thing as "urgent" around here.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Editing pdf/word content (text replacement)