Can I ask you to try something very simple please?

Could you use HWPF to open one of your user's Word files and then simply 
re-save it using a different name. The code is likely to be something like;

File inputFile = new File("C:\\temp\\Test.doc");
FileInputStream fileInputStream = new 
     FileInputStream(inputFile);
BufferedInputStream buffInputStream = new 
     BufferedInputStream(fileInputStream);
HWPFDocument document = new HWPFDocument(buffInputStream);

File outputFile = new File("C:\\temp\\Copy Of Test.doc");
FileOutputStream fileOutputStream = new 
     FileOutputStream(outputFile);
BufferedOutputStream buffOutputStream = new 
     BufferedOutputStream(fileOutputStream);
document.write(buffOutputStream);

After that, try to open the copy using MS Word and see if you experience 
problems. I am concerned that your files may contain something - I am thinking 
of scientific notation in this case - that HWPF cannot cope with and hope that 
this simple test will answer that question.

If this code is able to create a working copy of one of your files then we can 
begin to look a little deeper. In the interim, can I ask where you have to 
place the information that your application should add to the document? Does it 
- for example - have to be written into a table and does this table already 
exist in the document or will you have to create and insert it, into the 
header/footer, etc?

If the copy this code produces does not work then I fear you will have to look 
at alternative solutions. There are commercial tools available but you will 
have to pay for one of these of course. I am not certain, but I would think 
that you could create a Word macro with the capability to accomplish some of 
what you require although it may not be able to obtain the data that you have 
to write into the document. Finally, you could consider COM - there are tools 
such as JACOB and the SWT toolkit that allow you to control Word through Java 
code. In fact you execute VBA code that allows you to do anything to a Word 
document that a macro - or even a user - could. However this route has 
drawbacks and is neither neat nor particularly efficient.

--- On Wed, 12/3/08, Calimero <[EMAIL PROTECTED]> wrote:
From: Calimero <[EMAIL PROTECTED]>
Subject: Re: Bug inserting text in Word file
To: [email protected]
Date: Wednesday, December 3, 2008, 1:19 AM

Hi Anthony,

Thx for your example,

word files my application has to modify are scientifics abstracts synopsis
which could contain graphics and tables. The fact is i 've to insert some
aditionnal information like names, abstract's title, etc. My code is ok
when
content is only text but updates fail when word file holds graphics, images
or tables. The most incredible is that MS Word CAN'T open it but OpenOffice
should open it with damage content.

So, i'm lost !!! Hope it has solution... I'm thinking in creating an
abstract template with form at the beginning or the document. Hope, filling
fields form could maintain structure and so on avoid fail / bad alteration
of file content.

If any one has tested it... Share information please !




Anthony Andrews wrote:
> 
> 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]
> 
> 
> 
> 
> 
> 

-- 
View this message in context:
http://www.nabble.com/HELP-PLEASE-%3A-Bug-inserting-text-in-Word-file-tp20789913p20809373.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]




      

Reply via email to