Yestreday, the ground was too frozen for us to plant trees so I spent the day in the workshop (un-heated I must add). In between sharpening axes, and billhooks, replacing spade handles and the like I had the chance to spend time at my desk playing around with some code.
Anyway, I think I may have sorted out the problem for you. The code I have written will search through a document one paragraph at a time and replace search strings with text. It assumes a few things; * The search and replacement text will be contained in a HashMap - each key/value pair being a search term and it's associated replacement text. * That each search term can appear zero, one or more times in each paragraph in any order. * That the document may contain text, pictures, tables, etc. * That tables should also be serached and updated if necessary. * That the serach term and replacement text can be aggrefated into that data structure - the HashMap. If not, it should be trivial task to alter the logic. If you want the code, let me know. It is still a little untidy and I would lke to create at least one supporting class but that is not strictly necessary. I will be working away freom the office today so forgive me please if I do not reply immediately. --- On Fri, 12/5/08, Anthony Andrews <[EMAIL PROTECTED]> wrote: From: Anthony Andrews <[EMAIL PROTECTED]> Subject: Re: POI - HWPFDocument To: "POI Users List" <[email protected]> Date: Friday, December 5, 2008, 5:09 AM Have a look at the Range class and you will see a method called replaceText(). This method can be used to replace one piece of text - called the placeholder - with another - called the replacement text much in the manner of a mail merge I guess. If you look closely at the javadoc, Paragraph is a subclass of Range so the method should be available on your object 'p' meaning the changes you will have to make are (hopefully) very simple. I am far from being an expert so I do not know how you can use it to search through a document to replace numerous occurrences of a placeholder but suspect there is a way because it is possible to define the offset - the point within the document at which the method begins searching for the text to replace. --- 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]
