aspose file tools*
The moose likes Other Open Source Projects and the fly likes Apache POI -HWPF Word Document not appending using document.write(stream) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Apache POI -HWPF Word Document not appending using document.write(stream)" Watch "Apache POI -HWPF Word Document not appending using document.write(stream)" New topic
Author

Apache POI -HWPF Word Document not appending using document.write(stream)

lal dev
Greenhorn

Joined: Dec 08, 2008
Posts: 8
Hi,
I am working for Apache POI, HWPF Documents
Used to concatenate 2 word documents.

I am able to concatenate 2 word documents and display result in 3rd document.
But, as i am concatenating, the font type, size, bold are not coming in result document

When i read one word document, and update result document, everything is coming fine.
ie result document has all font size, bold format is correct

But, try to open existing word document in append mode and updating other documents,
the result document is resulting only with first document, not updating 2nd document.

Can you please suggest.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42628
    
  65
Welcome to JavaRanch.

How, exactly, are you appending the second document to the first?


Ping & DNS - my free Android networking tools app
lal dev
Greenhorn

Joined: Dec 08, 2008
Posts: 8
Hi,

I am reading new document as follows:

FileInputStream fileInputStream = new FileInputStream(new File(Infilename));
BufferedInputStream buffInputStream = new BufferedInputStream(fileInputStream);
HWPFDocument document = new HWPFDocument(buffInputStream);
range = new Range(0,document.characterLength(),document);
range.insertBefore(pos+". ");

File outfile = new File(Outfilename);
FileOutputStream fileOutputStream =new FileOutputStream(outfile,true);
BufferedOutputStream buffOutputStream = new BufferedOutputStream(fileOutputStream);
document.write(buffOutputStream);

i am adding some text to the range of new document and writing the new document.
I am opening the FileOutputStream in append mode, ie true..
so, i think it will append the existing file.

Can you please suggest...
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

"Lalitha Devi G" please check your private messages for an important administrative matter. You can see them by clicking the My Private Messages link above.


SCJP, SCWCD.
|Asking Good Questions|
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42628
    
  65
DOC is a structure document format. You can't append one document to another by writing two DOC file streams into a single file. You'll need to open the other file as an HWPF document as well, and then insert the text range of the first file into it.
lal dev
Greenhorn

Joined: Dec 08, 2008
Posts: 8
we can open HWPF Word document with input stream as input parameter.
so, i am opening HWPF word document as inputfile.

can you please explain "and then insert the text range of the first file into it".

can you please suggest..
lal dev
Greenhorn

Joined: Dec 08, 2008
Posts: 8
I got the point, i had Range say bigRange with all contents of all documents, but how can we set this range to a particular document...

can you please suggest..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42628
    
  65
First of all, please change your screen name according to the JavaRanch naming policy - a first name and a last name are required.

You need to open both files as HWPF documents, and then get a Range object of the document you want to append. Then you can insert that behind the Range object of the document you want to append to. You already have most of that code in the snippet you posted before.
lal dev
Greenhorn

Joined: Dec 08, 2008
Posts: 8
Hi,

I had a range with all concatenated word documents, but... not able to set this range to a HWPF Document.

Can you please suggest...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42628
    
  65
"gld", please read the policy on screen names: http://www.javaranch.com/name.jsp A valid screen name is not optional - accounts without it are generally closed. To avoid this, please fix yours before your next post.

I'm not sure how exactly to accomplish appending documents, but I'm sure it has something to with what's mentioned in "Changing text" in http://poi.apache.org/hwpf/quick-guide.html
lal dev
Greenhorn

Joined: Dec 08, 2008
Posts: 8
i am not able to create HWPF empty document. and not able to set range to a HWPF Document.

I am glad to see responses, thank you sir, but can you please suggest how to create a new HWPF Document and set range..

Can you please suggest....

Thanks in advance..

i have changed the name as "lal dev". my name is Lalitha Devi, so is this name fine... to continue... please suggest.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42628
    
  65
Your screen name is fine now.

An empty document can be created by "new HWPFDocument(new POIFSFileSystem())". How are you trying to append the files? Post the code, and we may be able to help.
lal dev
Greenhorn

Joined: Dec 08, 2008
Posts: 8
I am getting text of document as range and concatenating all ranges into Bigrange as follows:


Bigr.insertAfter(range.text());

range is one documents range..

but facing problem for DocumentSummary inforamtion:
Error getting property set with name DocumentSummaryInformation

here i am not able to set document information for individual documents...
Can you please suggest..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42628
    
  65
The line of code you posted doesn't do anything with DocumentSummaryInformation; where is that error message coming from? If you post code, seeing more than a single line would be helpful.
Naga Varadharaju
Greenhorn

Joined: Dec 15, 2008
Posts: 6
Lal can you please post the code for concatenating Ranges thanks.
gl rao
Greenhorn

Joined: Sep 10, 2009
Posts: 1
Hi lalitha devi,
Any success in merging 2 documents or replacing text with POI?
please post the code.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42628
    
  65
Some discussion of replacing text using HWPF can be found at http://mail-archives.apache.org/mod_mbox/poi-user/200812.mbox/browser in the discussion "POI - HWPFDocument". The code that goes with it is at http://test.javaranch.com/ulf/ParagraphText.java and http://test.javaranch.com/ulf/InsertText.java
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Welcome to Javaranch gl rao !


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
som sekhar
Greenhorn

Joined: Feb 18, 2010
Posts: 3
Hi
Can anybody post code for merging 2 documents with POI?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Hi som sekhar, welcome to the ranch !
If you have a question, please make your own thread. (UseOneThreadPerQuestion)


[My Blog]
All roads lead to JavaRanch
emrecan polatkan
Greenhorn

Joined: Feb 25, 2010
Posts: 1
hi! how to wtite jtable to ms word program i try this but i didn't help me.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hi there and welcome to Javaranch! You might be best off asking your question in a new thread .
 
Don't get me started about those stupid light bulbs.
 
subject: Apache POI -HWPF Word Document not appending using document.write(stream)