Page 1 of 1

P without any spacing

PostPosted: Sat Aug 11, 2012 11:33 pm
by sandra
I'm trying to add a Paragraph to my Document:

Code: Select all
WordDocument doc = new WordDocument();
WordParagraph p1 = new WordParagraph("Sample Text");
WordParagraph p2 = new WordParagraph("Sample Text2");
doc.add(p1);
doc.add(p2);


But there is always an empty line below the content of the paragraph (this looks like spacing or margin or something). Like this:

Code: Select all
Sample Text

Sample Text2



But in the normal case I don't want to have this empty space below the paragraph. I'm still trying to set spacing = 0, but where is the problem?

The class WordParagraph :

Code: Select all
public class WordParagraph {

public WordParagraph(){ 
    docx4jParagraph = factory.createP();
    run = factory.createR();
    docx4jParagraph.getContent().add(run);
    pPr = factory.createPPr();
   
    //spacing 0
    PPrBase.Spacing space = new PPrBase.Spacing();
    space.setAfter(BigInteger.ZERO);
    space.setBefore(BigInteger.ZERO);
    pPr.setSpacing(space);

    PStyle style = factory.createPPrBasePStyle();
    style.setVal(styleID);
    pPr.setPStyle(style);   
   
    jc = factory.createJc();
    jc.setVal(JcEnumeration.LEFT);
    pPr.setJc(jc);
   
    docx4jParagraph.setPPr(pPr);
  }



public void add(String text, String styleID, WordParagraph.TYPE type){   
    if (text != null) {
      this.styleID = styleID;
      org.docx4j.wml.Text t = factory.createText();
      t.setValue(text);
      P p = new P();     
      R tmpRun = factory.createR(); 
      tmpRun.getContent().add(t);
     
      org.docx4j.wml.RPr rpr = factory.createRPr();
      switch(type){
        case BOLD:           
          setBold(rpr);
          break;
        case ITALIC:
          setItalic(rpr);
          break;
        case UNDERLINED:
          setUnderlined(rpr);
          break;
        case ITALICUNDERLINED:
          setItalic(rpr);
          setUnderlined(rpr);
          break;
        case BOLDUNDERLINED:
          setBold(rpr);
          setUnderlined(rpr);
          break;
      }
      tmpRun.setRPr(rpr);
      CTSignedTwipsMeasure ctsm = new CTSignedTwipsMeasure();
      ctsm.setVal(BigInteger.ZERO);
      rpr.setSpacing(ctsm);
     
      p.getContent().add(tmpRun);     
     
   
      PStyle style = factory.createPPrBasePStyle();
      style.setVal(styleID);
      PPr properties = factory.createPPr();
      properties.setPStyle(style);     
     
      PPrBase.Spacing space = new PPrBase.Spacing();
      space.setAfter(BigInteger.ZERO);
      space.setBefore(BigInteger.ZERO);
      properties.setSpacing(space);       
   
      properties.setJc(jc);
     
      p.setPPr(properties);
     
      add(p);
    }   

public P getP(){   
    return docx4jParagraph;
  }
 
  }


The class WordDocument:

Code: Select all

public class WordDocument {
 
  private static final Logger log = Logger.getLogger(WordDocument.class);
  public enum ALIGNMENT {LEFT, RIGHT, CENTER};
  private String templateFile = "./src/sv/vorlage.docx"; 
  private String dateiname = "";   
  private String headerTitel = ""; 
  private MainDocumentPart mainDocumentPart = null;
  private ObjectFactory objectFactory = null;
  private static WordprocessingMLPackage wordMLPackage = null;
  private PgMar pgMar;
  private Body body; 
 

public WordDocument(String[] fileinfos){
    if (log.isInfoEnabled())
      log.info("WordDocument - ENTER");
   
    //liest aus dem uebergebenen Array die fileinfos
    getFileinfos(fileinfos);
   
    try {   
     
      objectFactory = new ObjectFactory();
      wordMLPackage = WordprocessingMLPackage.createPackage();
      mainDocumentPart = wordMLPackage.getMainDocumentPart();     
      body = mainDocumentPart.getJaxbElement().getBody();
     
      //Default Seitenabstaende setzen
      PageDimensions page = new PageDimensions();
      pgMar = page.getPgMar();                          //default 1,44 cm Abstand
      SectPr sectPr = objectFactory.createSectPr();     //Section Properties
      body.setSectPr(sectPr);                           
      sectPr.setPgMar(pgMar);                           //Page Margin setzen
           
      //fuege die Formatvorlagen und Header / Footer dem Dokument hinzu
      loadPartsFromTemplate();
     
    } catch (Exception e) {
      log.error("WordDocument - Exception", e);
    }
  }

public boolean add(WordParagraph wP){
    if(wP == null) return false;
    mainDocumentPart.addObject(wP.getP());   
    return true;
  }


Re: P without any spacing

PostPosted: Sun Aug 12, 2012 7:20 pm
by sandra
nodody any idea?

Re: P without any spacing

PostPosted: Tue Aug 14, 2012 8:37 am
by jason
What does your resulting XML look like? (Tip: XmlUtils marshaltoString).

Generally, as you'll see if you experiment in Word, if you want no spacing between paragaphs X1 and X2, you need both space-after for X1 to be 0, and space-before X2 to be 0.

Re: P without any spacing

PostPosted: Wed Aug 15, 2012 8:38 pm
by sandra
In XML it looks like:

Code: Select all
<w:p>
<w:pPr>
<w:pStyle w:val="Standard"/>
    <w:spacing w:before="0" w:after="0"/>
    <w:jc w:val="left"/></w:pPr><w:r><w:p>
<w:pPr>
<w:pStyle w:val="Standard"/>
   <w:spacing w:before="0" w:after="0"/>
   <w:jc w:val="left"/></w:pPr><w:r>
<w:rPr>
  <w:spacing w:val="0"/>
</w:rPr>
<w:t>Sample Text1</w:t>
</w:r>
</w:p>
</w:r>
</w:p>
<w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="Standard"/>
    <w:spacing w:before="0" w:after="0"/>
    <w:jc w:val="left"/></w:pPr><w:r><w:p>
<w:pPr>
<w:pStyle w:val="Standard"/>
   <w:spacing w:before="0" w:after="0"/>
   <w:jc w:val="left"/></w:pPr><w:r>
<w:rPr>
  <w:spacing w:val="0"/>
</w:rPr>
<w:t>Sample Text2</w:t>
</w:r>
</w:p>
</w:r>
</w:p>
<w:p>


Spacing before and after is set to 0. But there still is a emty line between both Paragraphs.

Re: P without any spacing

PostPosted: Wed Aug 15, 2012 9:05 pm
by jason
Are you sure?

That XML looks very broken.

Re: P without any spacing

PostPosted: Wed Aug 15, 2012 9:25 pm
by sandra
Second try:

Code: Select all
<w:body>
<w:p>
<w:pPr>
<w:pStyle w:val="Standard"/>
<w:spacing w:before="0" w:after="0"/>
<w:jc w:val="left"/>
</w:pPr>
<w:r>
<w:p>
<w:pPr>
<w:pStyle w:val="Standard"/>
<w:spacing w:before="0" w:after="0"/>
<w:jc w:val="left"/>
</w:pPr>
<w:r>
<w:rPr>
<w:spacing w:val="0"/>
</w:rPr>
<w:t>Sample Text1</w:t>
</w:r>
</w:p>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="Standard"/>
<w:spacing w:before="0" w:after="0"/>
<w:jc w:val="left"/>
</w:pPr>
<w:r><w:p>
<w:pPr>
<w:pStyle w:val="Standard"/>
<w:spacing w:before="0" w:after="0"/>
<w:jc w:val="left"/>
</w:pPr>
<w:r>
<w:rPr>
<w:spacing w:val="0"/>
</w:rPr>
<w:t>Sample Text2</w:t>
</w:r>
</w:p>
</w:r>
</w:p>
<w:sectPr>
<w:footerReference w:type="default" r:id="rId3"/>
<w:headerReference w:type="default" r:id="rId4"/>
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440"/>
</w:sectPr>
</w:body>


guess there were some lines missing, because my browser didn't copied the line breaks the first time.

Re: P without any spacing

PostPosted: Wed Aug 15, 2012 10:16 pm
by jason
You have a w:p nested in your w:r:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
      <w:r>
        <w:p>
          <w:pPr>
            <w:pStyle w:val="Standard"/>
            <w:spacing w:before="0" w:after="0"/>
            <w:jc w:val="left"/>
          </w:pPr>
          <w:r>
            <w:rPr>
              <w:spacing w:val="0"/>
            </w:rPr>
            <w:t>Sample Text1</w:t>
          </w:r>
        </w:p>
      </w:r>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Somewhat surprisingly, Word 2010 opens this, but renders it with an extra paragraph, which explains the behaviour you are seeing.

Re: P without any spacing

PostPosted: Fri Aug 17, 2012 4:33 am
by sandra
Yes I have a nested <p> in the <r>. But this is the only way I see to allow adding multiple content to my paragraph. Is there any other element byside a paragraph which can handle text, pictures and tables and text with different fonts? Something like a "chapter"? Because if I only use one <p> and <r> and don't nest other paragraphs inside my paragraph I only can use one text style (like bold, italic, font-size and so on).

But what I want is to create a chapter of text (which of course can have different styles).
Example:

This is the content of my paragraph!
This is much more text, and because it is so important I should be underlined.
And this should look nice and italic.

Is there a way to create the text construct you see above with only one single w:p and w:r?

Re: P without any spacing

PostPosted: Fri Aug 17, 2012 9:20 am
by jason
Don't try to nest paragraphs! It is not a supported usage in docx4j, nor a suggested usage in any of the Microsoft documentation.

A w:p is a paragraph, and you shouldn't try to use it like a chapter. (sectPr is the closest you get to that, or perhaps a content control, or sub document, depedending on your purpose)

sandra wrote:Because if I only use one <p> and <r> and don't nest other paragraphs inside my paragraph I only can use one text style (like bold, italic, font-size and so on).


You can have different styles of text in a single paragraph, using multiple runs.

Tables are block level elements, and don't belong in paragraphs.

Again, create the content you are after in Word (or even OpenOffice), then unzip it and look to see how it is done.

Re: P without any spacing

PostPosted: Sun Aug 19, 2012 8:35 pm
by sandra
Ok. Changed that. Now using different runs for different type of text (bold, italic ...). Works fine for this.

just, by the way: Is it also possible to have different "Styles" in a Paragraph? Is there a option to tell the R which " StyleID " (such as heading1, 2...) it should use, or belongs this kind of property only to P ?

Re: P without any spacing

PostPosted: Mon Aug 20, 2012 9:18 am
by jason
You can have run level styles, but these can only contain character formatting (not things like paragraph spacing).

There is a strange beast called a Style Separator which may do what you want though. See for example http://www.kisys.com/Tips/Style_separator.htm

Not sure what you are trying to do, but http://wordfaqs.mvps.org/RunInSidehead.htm may be relevant.