Page 1 of 1

How to remove space after paragraph?

PostPosted: Thu Feb 13, 2014 4:38 am
by STMaraj
So i've been trying to understand how to use docx4j but have had a lot of trouble getting the basics to work. I eventually found the online web app to generate the code. It created the document but theres always spaces after each paragraph. How do I get rid of them? I have docx4j 3.0.0

My main method calls this:
Code: Select all
   private static WordprocessingMLPackage createTestDocument(WordprocessingMLPackage wordMLPackage) {

      try {
         wordMLPackage = new WordprocessingMLPackage();

         Document newDocument = createIt();

         MainDocumentPart wordDocumentPart = new MainDocumentPart();
         wordDocumentPart.setContents(newDocument);

         wordMLPackage.addTargetPart(wordDocumentPart);

      } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }

      return wordMLPackage;
   }


This is the code that the web app generated:
Code: Select all
public static Document createIt() {

      org.docx4j.wml.ObjectFactory wmlObjectFactory = new org.docx4j.wml.ObjectFactory();

      Document document = wmlObjectFactory.createDocument();
      // Create object for body
      Body body = wmlObjectFactory.createBody();
      document.setBody(body);
      // Create object for sectPr
      SectPr sectpr = wmlObjectFactory.createSectPr();
      body.setSectPr(sectpr);
      // Create object for pgSz
      SectPr.PgSz sectprpgsz = wmlObjectFactory.createSectPrPgSz();
      sectpr.setPgSz(sectprpgsz);
      sectprpgsz.setH(BigInteger.valueOf(15840));
      sectprpgsz.setW(BigInteger.valueOf(12240));
      // Create object for pgMar
      SectPr.PgMar sectprpgmar = wmlObjectFactory.createSectPrPgMar();
      sectpr.setPgMar(sectprpgmar);
      sectprpgmar.setFooter(BigInteger.valueOf(720));
      sectprpgmar.setLeft(BigInteger.valueOf(1440));
      sectprpgmar.setRight(BigInteger.valueOf(1440));
      sectprpgmar.setGutter(BigInteger.valueOf(0));
      sectprpgmar.setTop(BigInteger.valueOf(1440));
      sectprpgmar.setBottom(BigInteger.valueOf(1440));
      sectprpgmar.setHeader(BigInteger.valueOf(720));
      // Create object for cols
      CTColumns columns = wmlObjectFactory.createCTColumns();
      sectpr.setCols(columns);
      columns.setSpace(BigInteger.valueOf(720));
      // Create object for docGrid
      CTDocGrid docgrid = wmlObjectFactory.createCTDocGrid();
      sectpr.setDocGrid(docgrid);
      docgrid.setLinePitch(BigInteger.valueOf(360));
      // Create object for p
      P p = wmlObjectFactory.createP();
      body.getContent().add(p);
      // Create object for pPr
      PPr ppr = wmlObjectFactory.createPPr();
      p.setPPr(ppr);
      // Create object for rPr
      ParaRPr pararpr = wmlObjectFactory.createParaRPr();
      ppr.setRPr(pararpr);
      // Create object for b
      BooleanDefaultTrue booleandefaulttrue = wmlObjectFactory.createBooleanDefaultTrue();
      pararpr.setB(booleandefaulttrue);
      // Create object for ind
      PPrBase.Ind pprbaseind = wmlObjectFactory.createPPrBaseInd();
      ppr.setInd(pprbaseind);
      pprbaseind.setLeft(BigInteger.valueOf(0));
      // Create object for r
      R r = wmlObjectFactory.createR();
      p.getContent().add(r);
      // Create object for rPr
      RPr rpr = wmlObjectFactory.createRPr();
      r.setRPr(rpr);
      // Create object for b
      BooleanDefaultTrue booleandefaulttrue2 = wmlObjectFactory.createBooleanDefaultTrue();
      rpr.setB(booleandefaulttrue2);
      // Create object for t (wrapped in JAXBElement)
      Text text = wmlObjectFactory.createText();
      JAXBElement<org.docx4j.wml.Text> textWrapped = wmlObjectFactory.createRT(text);
      r.getContent().add(textWrapped);
      text.setValue("Heading");
      // Create object for p
      P p2 = wmlObjectFactory.createP();
      body.getContent().add(p2);
      // Create object for pPr
      PPr ppr2 = wmlObjectFactory.createPPr();
      p2.setPPr(ppr2);
      // Create object for ind
      PPrBase.Ind pprbaseind2 = wmlObjectFactory.createPPrBaseInd();
      ppr2.setInd(pprbaseind2);
      pprbaseind2.setLeft(BigInteger.valueOf(0));
      // Create object for r
      R r2 = wmlObjectFactory.createR();
      p2.getContent().add(r2);
      // Create object for tab (wrapped in JAXBElement)
      R.Tab rtab = wmlObjectFactory.createRTab();
      JAXBElement<org.docx4j.wml.R.Tab> rtabWrapped = wmlObjectFactory.createRTab(rtab);
      r2.getContent().add(rtabWrapped);
      // Create object for t (wrapped in JAXBElement)
      Text text2 = wmlObjectFactory.createText();
      JAXBElement<org.docx4j.wml.Text> textWrapped2 = wmlObjectFactory.createRT(text2);
      r2.getContent().add(textWrapped2);
      text2.setValue("Description:");
      // Create object for p
      P p3 = wmlObjectFactory.createP();
      body.getContent().add(p3);
      // Create object for pPr
      PPr ppr3 = wmlObjectFactory.createPPr();
      p3.setPPr(ppr3);
      // Create object for ind
      PPrBase.Ind pprbaseind3 = wmlObjectFactory.createPPrBaseInd();
      ppr3.setInd(pprbaseind3);
      pprbaseind3.setLeft(BigInteger.valueOf(0));
      // Create object for r
      R r3 = wmlObjectFactory.createR();
      p3.getContent().add(r3);
      // Create object for tab (wrapped in JAXBElement)
      R.Tab rtab2 = wmlObjectFactory.createRTab();
      JAXBElement<org.docx4j.wml.R.Tab> rtabWrapped2 = wmlObjectFactory.createRTab(rtab2);
      r3.getContent().add(rtabWrapped2);
      // Create object for t (wrapped in JAXBElement)
      Text text3 = wmlObjectFactory.createText();
      JAXBElement<org.docx4j.wml.Text> textWrapped3 = wmlObjectFactory.createRT(text3);
      r3.getContent().add(textWrapped3);
      text3.setValue("Value:");

      return document;
   }

Re: How to remove space after paragraph?

PostPosted: Thu Feb 13, 2014 6:19 am
by jason
Using the webapp:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
    // Create object for spacing
    PPrBase.Spacing pprbasespacing = wmlObjectFactory.createPPrBaseSpacing();
    ppr.setSpacing(pprbasespacing);
        pprbasespacing.setAfter( BigInteger.valueOf( 0) );
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4

Re: How to remove space after paragraph?

PostPosted: Sat May 24, 2014 5:09 am
by kebeaner
Didn't want to make a new post but if youre using the wordprocessingMLPackage.createPackage();
how do you remove "space after paragraph"

Re: How to remove space after paragraph?

PostPosted: Sat May 24, 2014 7:23 am
by jason
createPackage() does:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                Part stylesPart = new org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart();
                try {
                        ((org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart) stylesPart)
                                        .unmarshalDefaultStyles();
                       
                        // Add the styles part to the main document part relationships
                        // (creating it if necessary)
                        wordDocumentPart.addTargetPart(stylesPart);
                        :
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


unmarshalDefaultStyles() reads "org/docx4j/openpackaging/parts/WordprocessingML/styles.xml"

That in turn contains:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
    <w:pPrDefault>
      <w:pPr>
                <w:spacing w:after="200" w:line="276" w:lineRule="auto" />
      </w:pPr>
    </w:pPrDefault>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


So you have three options:

Option 1: programmatically delete or change that w:spacing value

Option 2: edit "org/docx4j/openpackaging/parts/WordprocessingML/styles.xml"; I think I may patch docx4j later today to read that value from docx4j.properties, so you could more easily override the contents

Option 3: when adding a paragraph set w:pPr/w:spacing, or set a style which does that

Re: How to remove space after paragraph?

PostPosted: Wed May 28, 2014 10:02 pm
by jason
Re option 2, https://github.com/plutext/docx4j/commi ... 4cc444be26 provides the ability to easily point at your own styles.xml