Hi

For the beginning, you need to understand how text ist stored in OOXML Word documents. A paragraph consists of "runs", which contain one or more "t" - segments. The text is splitted among these text-elements. As soon as you bring in some formatting issues (bold, italic, different font, etc.) this gets far more complex.

The paragraph-method "getParagraphText" collects the unformatted text from all contained runs- and t-segments and returns it as an unformatted string. The text itself is not modifiable this way.

In order to modify the text, you would need to dig down into the runs and t-segments in order to remove existing snippets and replace them with your stuff.

Unfortunately, the POI-XWPF-API does not hide the XML below all too well right now. This means, that you need to take care of the XML partially by yourself. I'd prefer to enhance the POI API to a level, where you can deal with the "XWPF"-classes only, not taking care for the XML below. But that is a major task, and at the moment I do not have enough time to start such a huge project.

To modify the text of your document, you will have to take care for the XML below every XWPFRun by yourself. In order to access the XML directly, iterate all XWPFRun objects below the paragraph, and for every run call "getCTR". This will return the XMLBeans-object that corresponds to the Run-element.

Please be aware that dealing with OOXML by using POI still requires a basic knowledge of the document structure. Regardless the level of abstraction that an API like POI can provide, it cannot hide the structure completely.

Kind regards,
Stefan Stern

Am 08.09.2011 22:19, schrieb samirrolemberg:
Replacing the contents of a DOCX file.

I am using version 3.7 of Apache POI and would like to know how to replace
and save a file modified using the API.

I used the substitution model with a DOC file with HSSF, but the API had a
bug and prevented me from using it altogether.

So I decided to leave for the implementation of a code that uses XWPF.

By the time I got the following:

A DOCX file containing a header and a footer and a "body".

In the header, footer and body are one species of tags should I extract the
file and replaces it by a relevant data dynamically in my project.

I've managed to display (via console) the text in the header and footer.

I tried to modify the content using "XWPFRun", but without success.

does anyone know what to do? follows the code of what I'm trying to do:

*                       XWPFDocument docx = new XWPFDocument(new 
FileInputStream(new
File("Document.docx")));
                        for (XWPFHeader h : docx.getHeaderList()) {
                                for (XWPFParagraph p : h.getListParagraph()) {
                                        XWPFRun r = p.createRun();
                                                String a = 
r.getParagraph().getParagraphText();
                                                a="aaaaaaaaaaaa";
                                                p.addRun(r);
                                                
docx.enforceCommentsProtection();
                                                System.out.println(p.getText());
                                }
                        }
                        for (XWPFFooter f : docx.getFooterList()) {
                        }*

the paragraph it does not change the text.

or in some cases it adds the text after each line of the paragraph.

how I do it?

--
View this message in context: 
http://apache-poi.1045710.n5.nabble.com/Replacing-the-contents-of-a-DOCX-file-tp4783849p4783849.html
Sent from the POI - Dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]



--
MIND8 GmbH&  Co. KG · Gropiusplatz 10 · D-70563 Stuttgart
Phone +49(0)711 89536-207 · Fax +49(0)711 89536-199 · 
mailto:[email protected]
www.mind8.com · www.friedhelm-loh-group.com

MIND8 –
Wir automatisieren das Engineering.

MIND8 GmbH&  Co. KG, Sitz Stuttgart, HRA 724150, Amtsgericht Stuttgart
Persönlich haftend: MIND8 Verwaltungs-GmbH, Sitz: Haiger, HRB 5847, Amtsgericht 
Wetzlar
Geschäftsführung: Maximilian Brandl (Vorsitzender), Dr.-Ing. Jörg Lewek, 
Dr.-Ing. Marco Litto
Diese E-Mail ist vertraulich. Eine Weitergabe der darin verkörperten 
Informationen ist ohne Zustimmung des Versenders unzulässig.***
This email contains confidential information. You are not authorised to copy 
the contents without the consent of the sender.***


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to