0

Is there any way to change any property of a paragraph after I added to the MainDocumentPart ?

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
mdp.addStyledParagraphOfText("NormalIndent", "The following PHP error occurs: ");

In the last row i want preserve the whitespace using setSpace("preserve"), but how?

Thanks, roncsak

roncsak
  • 618
  • 1
  • 8
  • 21

1 Answers1

2

Disclosure: I work on docx4j

Get the paragraph, you just added; get the first run from the paragraph; get the first Text object from the run, the use setSpace("preserve")

The ContentAccessor interface can be used to get the paragraph, the run, and the Text object ie invoke getContent(), which returns a List

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
  • I don't understand. Why should I get the Paragraph again when I already got in line 3? I'm using eclipse and it wont offers the setSpace() method either way i'm trying. I understand the XML tree but not the ways to get existed ones. – roncsak Aug 16 '12 at 11:43
  • Well, your line 3 isn't getting anything. It could if you said P p = mdp.addStyledParagraphOfText(.... Once you have the org.docx4j.wml.Text object, Eclipse will oblige. – JasonPlutext Aug 16 '12 at 22:58
  • Well, i finally could do it today but its too sluggish. The setSpace method only applies to Text, isn't it ? Then why the unecessary steps to get the right object manually? The other thing is that if i have more Run or more text, I have to apply the setSpace() separately. (Or not?) An example of setting space preserve: `((Text) ((R) p.getContent().get(0)).getContent().get(0)).setSpace("preserve");` – roncsak Aug 21 '12 at 07:05
  • It takes a while to load the JAXB Context when you load your first document. After that, it shouldn't be "sluggish". Yes, setSpace only applies to the Text object, so in the absence of a higher level helper method, you have to get that object. – JasonPlutext Aug 21 '12 at 07:13