Page 1 of 1

Text Alignment inside of a Table

PostPosted: Tue Jul 24, 2012 2:34 am
by rightAlign
I'm trying to figure out an efficient way to set the text alignment within the cells of my table to "right." I can't seem to find a simple way to do this.. I may have a solution but I feel like there is a much more elegant one that doesn't involve generating a large string and unmarshaling it each time.

Here is the code (some of it) that I pulled from the document.xml file which contained the table I created:

Code: Select all
String tableProperties = "<w:tblPr " + Namespaces.W_NAMESPACE_DECLARATION
        + ">" + "<w:tblStyle w:val=\"TableGrid\"/>"
        +"<w:tblW w:w=\"0\" w:type=\"auto\"/>"+"<w:tblBorders>"
        +"<w:top w:val=\"none\" w:sz=\"0\" w:space=\"0\" w:color=\"auto\" /> "
        +"<w:left w:val=\"none\" w:sz=\"0\" w:space=\"0\" w:color=\"auto\" /> "
        +"<w:bottom w:val=\"none\" w:sz=\"0\" w:space=\"0\" w:color=\"auto\" /> "
        +"<w:right w:val=\"none\" w:sz=\"0\" w:space=\"0\" w:color=\"auto\" /> "
        +"<w:insideH w:val=\"none\" w:sz=\"0\" w:space=\"0\" w:color=\"auto\" /> "
        +"<w:insideV w:val=\"none\" w:sz=\"0\" w:space=\"0\" w:color=\"auto\" /> "
        +"</w:tblBorders>"
        + "<w:tblLook w:val=\"04A0\"/>" + "</w:tblPr>";


This sets up the generic table but the alignment is default to the left which doesn't fix the problem. This part works as expected, though.

Here is the code that I believe would be necessary to set up the right alignment, by perhaps accessing each individual cell and setting the properties? :

Code: Select all
String tableContents = "stringThatWouldChange";
      String rowUnmarshaled = "<w:tc> "
      +" <w:tcPr>"
      +"     <w:tcW w:w=\"1504\" w:type=\"dxa\" /> "
      +"     <w:vAlign w:val=\"center\" /> "
      +"  </w:tcPr> "
      +" <w:p w:rsidR=\"00D53843\" w:rsidRDefault=\"009229D7\" w:rsidP=\"00A01E30\"> "
      +"    <w:pPr> "
      +"        <w:jc w:val=\"right\" />  "
      +"     </w:pPr>  "
      +"        <w:proofErr w:type=\"spellStart\" /> "
      +"          <w:r> "
      +"              <w:t>" + tableContents + "</w:t>   "
      +"           </w:r> "
      +"        <w:proofErr w:type=\"spellEnd\" /> "
      +"     </w:p>  "
      +" </w:tc>";
      
      TcPr rowProperties = null;
      rowProperties = (TcPr) XmlUtils.unmarshalString(rowUnmarshaled);


Here are my questions:
First, is there a very easy way that I could just add some extra XML in my first block of code to make all of the alignment in my table to the right?
If this is not the case, can I somehow access just the paragraph portion of each cell, changing the alignment to right with a few lines of code like this? :

Code: Select all
String pPr = "<w:pPr> "
        + "<w:jc w:val=\"right\"/> "
        + "</w:pPr>";


Finally, if my original suspicion was correct and I in fact DO have to access each individual cell and generate the properties individually, what is the method to access the properties? Am I dealing with TcPr?

Thanks in advance.

Re: Text Alignment inside of a Table

PostPosted: Tue Jul 24, 2012 8:31 pm
by jason
You set vertical alignment within a cell via w:tcPr; horizontal alignment is set at the individual paragraph level, as shown in your string pPr (or a style).

So you do need to access each paragraph within your table, setting the pPr property.

Probably easiest to use SingleTraversalUtilVisitorCallback to visit each w:p in the table. For an example of how to use, see the ImageConvertEmbeddedToLinked sample

Re: Text Alignment inside of a Table

PostPosted: Wed Jul 25, 2012 3:54 pm
by rightAlign
Jason, thank you for your quick response.

I looked through the ImageConvertEmbeddedToLinked sample code and it seemed to make sense for the most part. My problem now surrounds the TraversalUtilVisitor. I couldn't find very much documentation on the actual class itself, and while it's clear that CTBlip elements are very useful for changing image properties, I'm confused on where to start for affecting paragraph properties. I know I need to access the w:p portion and then each time I visit a paragraph in the table I want to set the w:val to "right" - I just don't know how to access each w:p of the table because I don't know what type of element it is (assuming i'm on the right track and it is actually an element).

In summary: I can see how the SingleTraversalUtilVisitorCallback will find me the specific elements I am looking for, and then allow me to change the corresponding XML properties that I need to change, but I don't know the names/class of the element (w:p?) that I am trying to modify. Hopefully i'm on the right track and only one step away; if I am way off course please let me know.

Thanks again

Re: Text Alignment inside of a Table

PostPosted: Wed Jul 25, 2012 6:09 pm
by jason
Yep, the class for w:p is org.docx4j.wml.P

So you want something like:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting

        public static class TraversalUtilParagraphVisitor extends TraversalUtilVisitor<P> {
               
                @Override
                public void apply(P p, Object parent, List<Object> siblings) {
                                // get or create the pPr here

 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


Invoke it with:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                SingleTraversalUtilVisitorCallback paragraphVisitor
                        = new SingleTraversalUtilVisitorCallback(
                                        new TraversalUtilParagraphVisitor ());
                paragraphVisitor.walkJAXBElements(yourTable);

 
Parsed in 0.013 seconds, using GeSHi 1.0.8.4


Hope this helps .. Jason

Re: Text Alignment inside of a Table

PostPosted: Thu Jul 26, 2012 7:14 am
by rightAlign
Jason,

Worked like a charm, although I took a slightly different approach (will post my working examples sometime tomorrow as a reference for others).

I have one last question and I'm pretty sure there's an easy fix, but Googling the problem hasn't seemed to help me. When I try and throw a JaxBException i'm getting a restriction:

Access restriction: The type JAXBException is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar

Did the recent Java package add some restriction to these elements? It occurs for all 4 javax.xml.bind libraries I attempt to import. Perhaps you've encountered this problem already and know a quick solution?

Thanks