Page 1 of 1

change to bold font for only one cell

PostPosted: Wed Jun 15, 2016 3:56 am
by borja

Im trying to edit a table so i get some cells with words in bold. Im creating the rows and the cells and adding them to the table, not editing existing cells.
I looked for this in doc but i couldnt find it.

Code: Select all
tc.getEGBlockLevelElts().add(WordMLPackage.getMainDocumentPart().createParagraphOfText("my bold text"))

This is the part of my code where i add the text to the cell. I suppose i have to create the paragraph with the bold format before added to cell.

Any help i really appreciate.


Re: change to bold font for only one cell

PostPosted: Wed Jun 15, 2016 8:48 pm
by jason
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
         * Create a paragraph containing the string simpleText,
         * without adding it to the document.  If passed null, the result
         * is an empty P.
         * @param simpleText
         * @return

        public org.docx4j.wml.P createParagraphOfText(String simpleText) {
                org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
                org.docx4j.wml.P  para = factory.createP();

                if (simpleText!=null) {
                        org.docx4j.wml.Text  t = factory.createText();
                        org.docx4j.wml.R  run = factory.createR();
                        run.getContent().add(t); // ContentAccessor            
                        para.getContent().add(run); // ContentAccessor
                return para;
Parsed in 0.015 seconds, using GeSHi

To make it bold, you need to add w:rPr (run properties) to the run, and set w:b in the run properties.

Its probably easier not to use createParagraphOfText. (I never do).

If you upload a sample docx to the docx4j webapp (see menu above) or download the docx4j Word Helper AddIn, it will generate suitable code from your docx sample for you.