You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by hasiasat <ha...@gmail.com> on 2007/01/19 09:33:37 UTC

Creating new word doc with POI

Hi,

Im working on creating new doc using POI. i found a sample code:

import java.util.*;
import java.io.*;
import java.lang.*;
import org.apache.poi.hwpf.*;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.poifs.filesystem.*;
 
public class test4 {
     
     private static String _blankFile = "blank.doc";
     
     
     private static void copyBlankHWPF (String name) {
          
          try {
               HWPFDocument doc = new HWPFDocument(new
FileInputStream(_blankFile));
               OutputStream out = new FileOutputStream(name);
               doc.write(out);
          } catch (Exception ex) {
               ex.printStackTrace();
          }
     }
     
     public static void main(String [] args){
          String generatedFile =
args[1].substring(0,args[1].length()-3)+"-generated.doc";
          try {
               copyBlankHWPF(generatedFile);
               HWPFDocument templateFile = new HWPFDocument(new
FileInputStream(args[0]));
               HWPFDocument blankFile = new HWPFDocument(new
FileInputStream(_blankFile));
               OutputStream out = new FileOutputStream(generatedFile);
               Range blankRange = blankFile.getRange();
               Range  templateRange= templateFile.getRange();
               String templateContent = templateRange.text();
               templateContent =
templateContent.replaceAll(string1,string2);
               blankRange.insertBefore(templateContent);
               blankFile.write(out);
          } catch (Exception ex) {
               ex.printStackTrace();
          }
     }
}


when i run the code it will throw an exception:

java.io.FileNotFoundException: blank.doc 



i created a blank.doc in my current directory but i got this error when i
run again the application:

Error getting property set with name ♣DocumentSummaryInformation
java.io.FileNotFoundException: no such entry: "♣DocumentSummaryInformation"
Error getting property set with name ♣SummaryInformation
java.io.FileNotFoundException: no such entry: "♣SummaryInformation"
Error getting property set with name ♣DocumentSummaryInformation
java.io.FileNotFoundException: no such entry: "♣DocumentSummaryInformation"
Error getting property set with name ♣SummaryInformation
java.io.FileNotFoundException: no such entry: "♣SummaryInformation"



i dont know what happen here, i found a site:
http://java.sys-con.com/read/45846.htm that state: The POI distribution
comes with one called "blank.doc."

But i coudn't found it in distribution folder.

Does anyone know how to make this code running? I am new to all of this.

Thanks in advance.
-- 
View this message in context: http://www.nabble.com/Creating-new-word-doc-with-POI-tf3038842.html#a8445730
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


Re: Creating new word doc with POI

Posted by Rainer Schwarze <rs...@admadic.de>.
Mohit (ashwani Kabra) wrote:
> Hello,
> 
> I'm also very new to POI . I'm also not getting able to create a word doc.
> I got able to read the existing file and I can copy the content of old file
> to new one.
> But i want to create a word doc and want to write my content what i'm
> getting from console.
> 
> Please can you help me?
> 
> Thanks.
> Mohit


Hi Mohit,

you should use an empty Word file as a "template" for POI. Creating a
Word file from scratch is not supported. When you open an empty Word
file, fill it with the content and save it using a different filename,
it is probably the easiest solution.

Best wishes, Rainer
-- 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Creating new word doc with POI

Posted by "Mohit (ashwani Kabra)" <fa...@yahoo.com>.
Hello,

I'm also very new to POI . I'm also not getting able to create a word doc.
I got able to read the existing file and I can copy the content of old file
to new one.
But i want to create a word doc and want to write my content what i'm
getting from console.

Please can you help me?

Thanks.
Mohit
-- 
View this message in context: http://www.nabble.com/Creating-new-word-doc-with-POI-tp8445730p14753648.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Creating new word doc with POI

Posted by Rainer Schwarze <rs...@admadic.de>.
At 11:40 19.01.2007, Rainer Klute wrote:
>hasiasat schrieb:
>> Error getting property set with name ♣DocumentSummaryInformation
>> java.io.FileNotFoundException: no such entry:
>> "♣DocumentSummaryInformation"
>> Error getting property set with name ♣SummaryInformation
>> java.io.FileNotFoundException: no such entry: "♣SummaryInformation"
>>   
>
>The property sets DocumentSummaryInformation and SummaryInformation do
>not belong to the Word document as such. You have to create them using
>POI's HPSF component, see <http://jakarta.apache.org/poi/hpsf/> for
>details. I don't know whether HWPF has any functionalities to
>encapsulate the HPSF stuff for you.

HWPFDocument is a subclass of POIDocument. That has readProperties() and
writeProperties(...). readProperties() is called already, but upon a write
the writeProperties is missing. To fix it add a line at the end of the
function HWPFDocument.write(...) as shown below:

  public void write(OutputStream out) throws IOException
  {
    ...
    // spit out the Word document.
    POIFSFileSystem pfs = new POIFSFileSystem();
    pfs.createDocument(new ByteArrayInputStream(mainBuf), "WordDocument");
    pfs.createDocument(new ByteArrayInputStream(tableBuf), "1Table");
    pfs.createDocument(new ByteArrayInputStream(dataBuf), "Data");
    // write the properties:
    writeProperties(pfs);       // <-- insert this
    pfs.writeFilesystem(out);
  }

That should do it.

Best wishes,
Rainer

-- 
Rainer Schwarze  


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


Re: Creating new word doc with POI

Posted by Rainer Klute <kl...@rainer-klute.de>.
hasiasat schrieb:
> Error getting property set with name ♣DocumentSummaryInformation
> java.io.FileNotFoundException: no such entry: "♣DocumentSummaryInformation"
> Error getting property set with name ♣SummaryInformation
> java.io.FileNotFoundException: no such entry: "♣SummaryInformation"
>   

The property sets DocumentSummaryInformation and SummaryInformation do
not belong to the Word document as such. You have to create them using
POI's HPSF component, see <http://jakarta.apache.org/poi/hpsf/> for
details. I don't know whether HWPF has any functionalities to
encapsulate the HPSF stuff for you.

Best regards
Rainer Klute

                           Rainer Klute IT-Consulting GmbH
  Dipl.-Inform.
  Rainer Klute             E-Mail:  klute@rainer-klute.de
  Körner Grund 24          Telefon: +49 172 2324824
D-44143 Dortmund           Telefax: +49 231 5349423

OpenPGP fingerprint: E4E4386515EE0BED5C162FBB5343461584B5A42E