Good afternoon,
I am Danila,
I have a problem with HPWF and Word.

I read a file, adding some text in it (using insertBefore(String)), then I
save this file.
When I open this file with Word, it show this message:

"Word was unable to read this document. It may be corrupt."
but if I open this document with Open Office, there aren't problem.

the source code
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;
            String protocollazione="Documento Protocollato:\r\n numero:
12345\r\n data:12-01-2009\r \n\f";




         try {


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


             range = new Range(0,
                               document.characterLength(),
                               document);


             range.insertBefore(protocollazione);

             fileOutputStream = new
                  FileOutputStream("C:\\word1.doc");
             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) {
                                  }
             }

             if(buffOutputStream != null) {
                 try {
                     buffOutputStream.flush();
                     buffOutputStream.close();
                 }
                 catch(IOException ioEx) {
                                    }
             }
         }
     }

If I use the method (insertAfter(String) ) i haven't this problem with Word
in fact this file is correct,

but if I open it with Open Office this String is not visible.


THE SOURCE CODE IS DIFFERENT
FOR : range.insertAfter(protocollazione);
Help me please
Danila.

Reply via email to