poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Fabián Avilés Martínez <famarti...@gmv.com>
Subject Modify word document
Date Mon, 23 Nov 2009 15:42:34 GMT
Hi all,
	I have a Word document, as a template: In this template there are some tokenized words, which
have to be modified and the result has to be saved into another file. The original file has
some properties, like header and footer, images, etc. The resulting file has to be the same,
but with the modified words. I am trying it with the code below, but it does not work.

public ByteArrayOutputStream processFile(final InputStream is, final Map<String, String>
replacementText)
        throws IOException {
        Set<String> keys = replacementText.keySet();
        try {
            POIFSFileSystem poifs = new POIFSFileSystem(is);
            HWPFDocument document = new HWPFDocument(poifs);
            Range range = document.getRange();

            for (int i = 0; i < range.numParagraphs(); i++) {
                String newTxt = range.getParagraph(i).text();
                String oldTxt = range.getParagraph(i).text();
                for (Iterator<String> it = keys.iterator(); it.hasNext();) {
                    String key = it.next();
                    if (newTxt.contains(key)) {
                        newTxt = replacePlaceholders(key, replacementText.get(key), newTxt);
                    }
                }
                if (!oldTxt.equals(newTxt)) {
                    range.getParagraph(i).replaceText(oldTxt, newTxt);
                }
            }

            // Save the document away.
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            document.write(bos);
            bos.flush();
            bos.close();
            return bos;
        } catch (IOException e) {
            logger.error("Error procesando el fichero WORD: " + e);
            throw new IOException("Error procesando el fichero WORD");
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }

Any help, please?

Thanks in advance, Fabi.



______________________
This message including any attachments may contain confidential 
information, according to our Information Security Management System,
 and intended solely for a specific individual to whom they are addressed.
 Any unauthorised copy, disclosure or distribution of this message
 is strictly forbidden. If you have received this transmission in error,
 please notify the sender immediately and delete it.

______________________
Este mensaje, y en su caso, cualquier fichero anexo al mismo,
 puede contener informacion clasificada por su emisor como confidencial
 en el marco de su Sistema de Gestion de Seguridad de la 
Informacion 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.

______________________


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


Mime
View raw message