jspriggs
jspriggs

Reputation: 403

Apache poi XWPF Paragraph line spacing

I am looking at trying to build a word document using Apache Poi in Java/Coldfusion. So far it's letting me do a lot of the formatting I want except for line spacing of a paragraph. Does anyone know how I would set a paragraph to be single line spaced? It keeps building documents that have Multiple spaced paragraphs, with 1.15 as the spacing.

Here's an example of the code:

document = loader.create("org.apache.poi.xwpf.usermodel.XWPFDocument");
ParagraphAlignment = loader.create("org.apache.poi.xwpf.usermodel.ParagraphAlignment" );
paragraphOne = document.createParagraph();
paragraphOne.setAlignment(ParagraphAlignment.CENTER);

paragraphOneRunOne = paragraphVar.createRun();
paragraphOneRunOne.setFontFamily('Cambria');
paragraphOneRunOne.setFontSize(12);
paragraphOneRunOne.setBold(false);
paragraphOneRunOne.setText(Here's one line of text in this paragraph);
paragraphOneRunOne.addBreak();

paragraphOneRunTwo = paragraphVar.createRun();
paragraphOneRunTwo.setFontFamily('Cambria');
paragraphOneRunTwo.setFontSize(12);
paragraphOneRunTwo.setBold(false);
paragraphOneRunTwo.setText(Here's a second line of text in this paragraph);
paragraphOneRunTwo.addBreak();

I've looked through examples and the api docs, and the only settings I can find for line spacing determine the line spacing before or after a paragraph, not within the paragraph for linespacing of the text.

Anyone encounter this before or have any exmaples of how to fix it?

Upvotes: 3

Views: 8724

Answers (4)

Benjamin OB
Benjamin OB

Reputation: 1

<w:spacing w:lineRule='atLeast' w:line='240' />

Fix

XWPFParagraph p
p.setSpacingBetween(1);
CTP ctP = p.getCTP();
CTPPr ctPr = ctP.isSetPPr() ? ctP.getPPr() : ctP.addNewPPr();
CTSpacing spacing = ctPr.isSetSpacing() ? ctPr.getSpacing() :     ctPr.addNewSpacing();
spacing.setLine(BigInteger.valueOf(200));
spacing.setBefore(BigInteger.valueOf(0));
spacing.setAfter(BigInteger.valueOf(0));
spacing.setLineRule(STLineSpacingRule.EXACT);
p.getCTP().getPPr().setSpacing(spacing);

Upvotes: 0

Abhishek Jaiswal
Abhishek Jaiswal

Reputation: 261

paragraph.setSpacingBetween(1);

It will create a line spacing of 1

Upvotes: 1

Peter Arboleda
Peter Arboleda

Reputation: 521

I recently have to use the line spacing with POI,and my approach was get the paragraph and get the ooxml-schema

XWPFParagraph paragraph;
xml = paragraph->getCTP()->ToString();

then the schema will be stored as a String in that xml variable and you have to retrieve the value from this with some string function

<w:spacing w:lineRule='atLeast' w:line='240' />

the w:line is the one you need

Hope this helps!

Upvotes: 1

bbhar
bbhar

Reputation: 609

I also faced the same problem and found a solution after going through the ooxml-schemas source and the internals of the docx file generated. Here is the code

public void setSingleLineSpacing(XWPFParagraph para) {
    CTPPr ppr = para.getCTP().getPPr();
    if (ppr == null) ppr = para.getCTP().addNewPPr();
    CTSpacing spacing = ppr.isSetSpacing()? ppr.getSpacing() : ppr.addNewSpacing();
    spacing.setAfter(BigInteger.valueOf(0));
    spacing.setBefore(BigInteger.valueOf(0));
    spacing.setLineRule(STLineSpacingRule.AUTO);
    spacing.setLine(BigInteger.valueOf(240));
}

Upvotes: 1

Related Questions