Join
Sign in
Search Options
Search Everything
Search Formats
Home
Blog
Resources
Forums
About Open XML
More ...
Home
»
Forums
»
Formats
»
WordprocessingML
»
How to substitute text in docx.
How to substitute text in docx.
Formats
Discussions about working with different Open XML Formats
Get this RSS feed
Details
3
Replies
0
Subscribers
Posted
over 2 years ago
Options
Subscribe via RSS
Share this
WordprocessingML
How to substitute text in docx.
rated by 0 users
This post has
3 Replies |
0
Followers
MiKL1981
20
Posted by
MiKL1981
on
Fri, Jan 8 2010 6:26 AM
rated by 0 users
Post Points: 20
How to substitute text in docx.
Hi,
I would like to change documents docx in Groovy or JAVA language. I can replace image or graph, but i don't know how i have to do to substitute keyword ($$$textToSubstitute) by new text (String).
I manage actually no problem to browse each paragraph
(
xwpfDoc.getParagraphs(i)
),
however in the next step I cannot browse the Run (I cannot find a method such paragraph.getRuns ()).
This is a problem to replace the keyword with the replacement text in the right place, because I cannot use the
specific
Run containing the keyword.
I cannot find method in Apache POI (XWPF) to browse Run one by one. despite the fact that this method exist in (HWPF : Apache POI for .doc file)
...
int numCharRuns = paragraph.numCharacterRuns();
for (int j = 0; j < numCharRuns; j++) {
CharacterRun charRun = paragraph.getCharacterRun(j);
text = charRun.text();
if (text.contains(key)) {
int start = text.indexOf(key);
charRun.replaceText(key, value, start);
}
}
...
How can i do to browse each Run in a docx file, with the help of Apache POI APIs or other?
Thank you for everyhelps!
jasonh
375
Posted by
jasonh
on
Mon, Jan 11 2010 7:44 PM
rated by 0 users
Post Points: 20
Re: How to substitute text in docx.
You can use docx4j.
The docx4j equivalent of paragraph.getRuns() is paragraph.getParagraphContent()
There is also a method XmlUtils.unmarshallFromTemplate(String wmlTemplateString,
java.util.HashMap mappings)
Savan
10
Posted by
Savan
on
Tue, Feb 23 2010 4:21 AM
rated by 0 users
Post Points: 5
Re: How to substitute text in docx.
Hi,
I'm working with docx4j. I'm trying to change the main title of the document and I do this:
WordprocessingMLPackage wordMLPackage;
wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
//MARSHAL
String xml = XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true);
//CHANGE (Replace a string)
xml = xml.replaceAll("<w:t>Title of document</w:t>", "<w:t>New Title</w:t>");
//UNMARSHAL
wordMLPackage.getMainDocumentPart().addObject(org.docx4j.XmlUtils.unmarshalString(xml));
// Now save it
if (save) {
System.out.println("Saved.");
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(outPutFileName);
}
But... when I compile it, I get some exceptions like this:
org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:319)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:130)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:80)
at org.docx4j.samples.ChangeTitle.main(ChangeTitle.java:90)
Could someone help me, please?
Thank you!
Savan
10
Posted by
Savan
on
Wed, Feb 24 2010 6:14 AM
rated by 0 users
Post Points: 5
Re: How to substitute text in docx.
I read in plutext forums that this problem has a solution: download the last release of docx4j 2.3.0, and with that, my program works perfectly. But finally, when i didn't know it, I changed my program, and now I do a text replace traversing the document, using org.docx4j.wml.Text objects. There is an example of it in docx4j samples.
Thanks!
Page 1 of 1 (4 items)