I am far from being an expert with HWPF as I have only looked at it and written
a few simple programs (HSSF is the API I use most). However, just a few moments
ago, I created a very simple Word document (.doc file) containing a single
paragraph of text and successfully used this class to modify it;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
/**
*
* @author win user
*/
public class InsertText {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
HWPFDocument document = null;
Range range = null;
BufferedInputStream buffInputStream = null;
BufferedOutputStream buffOutputStream = null;
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
File inputFile = null;
try {
// Open the document
inputFile = new File("C:\\temp\\Test.doc");
fileInputStream = new
FileInputStream(inputFile);
buffInputStream = new
BufferedInputStream(fileInputStream);
document = new HWPFDocument(buffInputStream);
// Get an instance of the Range class. As
// the document is so simple
// the Range will cover the entire contents
// of the .doc file.
range = new Range(0,
document.characterLength(),
document);
// Insert text at the start of the document
// and at the end
range.insertBefore(
"Insert this at the very start. ");
range.insertAfter(
"....and this right at the end.");
// Save the document away.
fileOutputStream = new
FileOutputStream(inputFile);
buffOutputStream = new
BufferedOutputStream(fileOutputStream);
document.write(buffOutputStream);
}
catch(IOException ioEx) {
System.out.println("Caught an: " +
ioEx.getClass().getName());
System.out.println("Message: " +
ioEx.getMessage());
System.out.println("StackTrace follows:");
ioEx.printStackTrace(System.out);
}
finally {
if(buffInputStream != null) {
try {
buffInputStream.close();
}
catch(IOException ioEx) {
// I G N O R E //
}
}
if(buffOutputStream != null) {
try {
buffOutputStream.flush();
buffOutputStream.close();
}
catch(IOException ioEx) {
// I G N O R E //
}
}
}
}
}
and you may wish to have a go at repeating the experiment for yourself.
Remember to create a very simple file and try modifying that first. You will
need to alter this line of code;
inputFile = new File("C:\\temp\\Test.doc");
to point to your file of course - sorry if that sounds obvious, I try not to
make assumptions about peoples knowledge and am not being patronising.
As to modifying files your users upload, I would like to know;
What it is that you are actually attempting to do. Are you inserting a single
line or paragraph of text, a picture, a table, etc? Or are you triying
something more complex, for example modifying a table that already exists in
the document? How do you determine where the inerstion should be made - is it
at the start, the end, after a specific paragraph, etc?
In broad terms, what do your users documents contain - and I understand that
some of this information may be commercially sensitive so please do not think I
want you to be specific - just say something like 'a paragraph of text followed
by a table'? Is it just text or are there more complex elements such as
pictures, tables, etc?
Hopefully, we should be able to work out a solution for you.
--- On Tue, 12/2/08, Calimero <[EMAIL PROTECTED]> wrote:
From: Calimero <[EMAIL PROTECTED]>
Subject: Re: Bug inserting text in Word file
To: [email protected]
Date: Tuesday, December 2, 2008, 7:38 AM
Well,
the thing is that users upload word files and application needs to add some
information at beginning of the document).
Does it means that i don't have any alternative ? In the way i put a form
in
word file to fill in it and so on maybe maintain information structure, can
it be done ? and how ?
Thx so much !
Anthony Andrews wrote:
>
> I am assuming that you have created a file using Word then opened it and
> re-saved it using POI and are wondering why the file sizes are different.
>
> If that is the case then you should not be surprised because it is quite
> lilely that the structure of the files will be slightly different. The two
> applications whilst creating files that both can read may go about the
> process slightly differently - Word for example may exclude certain
> information because it can make assumptions about issing/excluded
> information when reading that same file. POI cannot because - at least
> until recently - the full details of the file structure were not public
> and outside of Microsoft no one really knows how Word parses the files it
> creates.
>
> --- On Tue, 12/2/08, Calimero <[EMAIL PROTECTED]> wrote:
> From: Calimero <[EMAIL PROTECTED]>
> Subject: Re: Bug inserting text in Word file
> To: [email protected]
> Date: Tuesday, December 2, 2008, 3:27 AM
>
> Well,
>
> it can delete cells of table, doesn't open in MSOffice. I noted that
> inserting text number of bytes have changed.
>
> Any idea ?
>
>
> Ulf Dittmer-2 wrote:
>>
>> What do you mean by "document is altered"? Inserting
>> text is altering the document, isn't it?
>>
>> Also, you aren't really ignoring the exception, right?
>>
>> Ulf
>>
>> --- Calimero <[EMAIL PROTECTED]> wrote:
>>
>>>
>>> Hi,
>>>
>>> i try to insert text in Word file like that.
>>> Document is altered when it
>>> contains tables / images or graphics :
>>>
>>> try {
>>> HWPFDocument doc = new HWPFDocument(is);
>>>
>>> Range range = doc.getRange();
>>> range.insertBefore("Hello world");
>>> doc.write(outputStream);
>>> } catch(Exception e){}
>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>>
>
> --
> View this message in context:
>
http://www.nabble.com/HELP-PLEASE-%3A-Bug-inserting-text-in-Word-file-tp20789913p20790326.html
> Sent from the POI - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
>
>
>
--
View this message in context:
http://www.nabble.com/HELP-PLEASE-%3A-Bug-inserting-text-in-Word-file-tp20789913p20794180.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]