Sorry to say that it is a bit more complex than I at first thought but still 
really quite straightforward.

I have played around with a bit of code and come up with a solution that works 
a little like this;

inputFile = new File("C:\\temp\\Test.doc");
fileInputStream = new FileInputStream(inputFile);
buffInputStream = new BufferedInputStream(fileInputStream);
HWPF Document document = new HWPFDocument(buffInputStream);
            
Range range = document.getRange();
            
int numParagraphs = range.numParagraphs();
            
for(int i = 0; i < numParagraphs; i++) {
     Paragraph para = range.getParagraph(i);
     String paraText = para.text();
     for(String key : keys) {
         paraText = swapPlaceholderForText(key,
             replacementText.get(key), paraText);
     }
     para.replaceText(para.text(), paraText, 0);
}

The first four lines simply open the Word file and there are corresponding 
lines in my program to save the file after processing.

Next, I recover a reference to the Range object that describes the complete 
document and then ask that how many paragraphs there are. That value which is 
stored in the int variable numParagraphs is then used to control the for loop 
where the work is actually accomplished.

Before getting to that loop, I should mention that I have stored the 
placeholders and associated replacement text in a HashMap which uses the 
placeholder as a key. Next, I have recovered a reference to the List of keys 
and stored that into a variable called keys.

So, in the for loop, I am recovering a reference to each Paragraph and storing 
it in the variable called para. Next, I get the text from each paragraph in 
turn and store it into a local String variable called paraText. Aftre that, I 
am searching through this String for occurrences of the placeholders and 
replacing each with the associated text - that bit occurs within the inner for 
loop when a call is made to a local method I have called 
swapPlaceholderForText(). Finally, I call the replaceText() method on the 
Paragraph object.

The swapPlaceholderForText() method looks like this;

private String replaceText(String key, String value, String text) {
    int index = 0;
    while((index = text.indexOf(key)) >= 0) {
        text = text.substring(0, index) + value + text.substring(index + 
key.length());
    }
    return(text);
}   

Let me know if there are any things you cannot understand or if you want the 
complete program as I will need to tidy a few things up.


--- On Fri, 12/5/08, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
From: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
Subject: POI - HWPFDocument
To: [email protected]
Date: Friday, December 5, 2008, 12:52 AM



Hallo everyboby.



I have benn looking for the last days
in internet trying to find a solution for my problem, and I found nothing
that could help me...so I am asking you help... 



The problem:



I store in oracle table field (BLOB)
a word document.This document is a template whose content is text and also
few fields that  I'm trying to fill up at runtime using HWPFDocument...but
I don't find the way.

What I want to do is at field with name
"name" put e.g. "Myname", to address
put "MyAddress" and so on.....



Here few code lines and what it prints



Range r = wordDocument.getRange();

         
for (int x = 0; x < r.numSections(); x++){

           
  Section s = r.getSection(x);

           
  for (int y = 0; y < s.numParagraphs(); y++){

           
          Paragraph p = s.getParagraph(y);

           
          for (int z = 0; z <
p.numCharacterRuns(); z++){

           
               
  CharacterRun run = p.getCharacterRun(z);

           
               
  //character run text

           
               
  String text = run.text();

           
               
  System.out.println(text);

           
          }

           
  }

      }





"name"



John Smith









"address"



99 North Road









"postCode"



X1 2YZ



 



"city"



London





















"date"



Tuesday, 8 August 2006



















Dear 



"name"



John Smith





..........



Hope my english is understandable...



Thanks in advance.



Gilio Simione



P.S.



Here is the template stores in the database.







Este mensaje, y en su caso, cualquier fichero anexo al mismo, puede
contener informacion confidencial, siendo para uso exclusivo del 
destinatario, quedando prohibida su divulgacion copia o  distribucion a 
terceros sin la autorizacion expresa del remitente. Si Vd. ha recibido 
este mensaje erroneamente, se ruega lo  notifique al remitente y 
proceda a su borrado.
Gracias por su colaboracion.

This message (including any attachments) may contain confidential 
information. It is intended for use by the recipient only. Any 
dissemination, copying or distribution to third parties without the 
express consent of the sender is strictly prohibited. If you have 
received this message in error, please delete it immediately and 
notify the sender. 
Thank you for your collaboration.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


      

Reply via email to