Page 1 of 1

Paragraph alignment

PostPosted: Tue Oct 16, 2012 1:47 am
by lacinaKrol
Hi Everyone.
I have a problem, with text alignment, I want it to be aligned to the right side of the table cell.
How I can achieve that ? Is there any way to do it programatically in java ? I've found solution to deal with it by editing XML, but I am not familliar with it.

Thanks

Re: Paragraph alignment

PostPosted: Tue Oct 16, 2012 6:01 pm
by jason
Sure. The docx4j API effectively gives you a strongly typed way to edit the XML.

The workflow is, using docx4j: load the docx, find the table cell you wish to alter, create JAXB elements matching the XML you want, then save the docx.

Please see our Getting Started guide for further details.

If you need more help, please post the code you have tried, and the XML you are trying to create.

Re: Paragraph alignment

PostPosted: Wed Oct 17, 2012 12:05 am
by lacinaKrol
I'm generating document in my application, and during the creation i want to set alignments in cells, is that possible ? I have found nothing particularly useful about this in "Getting Started " document, or in the links submitted at main page.

Code: Select all
private void addStyling(Tc tableCell, String content, boolean bold, String fontSize) {
      P paragraph = factory.createP();   
      Text text = factory.createText();
      text.setValue(content);
      
      R run = factory.createR();
      run.getContent().add(text);
      paragraph.getContent().add(run);
      RPr runProperties = factory.createRPr();
      PPr otherProperties = factory.createPPr();
       PPrBase.Spacing space = new PPrBase.Spacing();
       otherProperties.setAdjustRightInd(new BooleanDefaultTrue());
       TextAlignment ta = new TextAlignment();
       ta.setVal("right");
       otherProperties.setTextAlignment(ta);
       space.setAfter(new BigInteger ("0"));
      otherProperties.setSpacing(space);
      paragraph.setPPr(otherProperties);
      if (bold) {
         addBoldStyle(runProperties);
      }
      
      if (fontSize != null && !fontSize.isEmpty()) {
         setFontSize(runProperties, fontSize);
      }
      run.setRPr(runProperties);
      tableCell.getContent().add(paragraph);
   }


I have tried something like this, but it isn't working.

Re: Paragraph alignment

PostPosted: Wed Oct 17, 2012 4:25 am
by jason
XmlUtils.marshaltoString tells me your code creates XML like:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
                    <w:p>
                        <w:pPr>
                            <w:adjustRightInd/>
                            <w:spacing w:after="0"/>
                            <w:textAlignment w:val="right"/>
                        </w:pPr>
                        <w:r>
                            <w:rPr/>
                            <w:t>hello</w:t>
                        </w:r>
                    </w:p>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


What you want in your w:pPr is <w:jc w:val="right"/>

The general approach is to use Word (or possibly LibreOffice/OpenOffice) to create the document content you are aiming for, the unzip it and look at the XML to see what you need to create.

After that, you can easily create the content using the object factory (as you have done), or by unmarshalling a string of XML.

Re: Paragraph alignment

PostPosted: Wed Oct 17, 2012 9:29 pm
by lacinaKrol
Thank you :-) it helped a lot