You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Anirudh Singh Gaur <gr...@gmail.com> on 2012/07/29 09:53:50 UTC
Formatting and Justify Paragraphs in Apache POI
I'm trying to generate a .docx file and need help in some things: -
1. How to get "Justify" alignment for paragraphs (there are options like
BOTH, DISTRIBUTED etc).
2. I need some bold words in between the normal words in a paragraph. How
to achieve this? (I'm trying to create two different "XWPFRun" but its not
working, they are not appearing in the sequence in which I want..)
Re: Formatting and Justify Paragraphs in Apache POI
Posted by Mark Beardsley <ma...@tiscali.co.uk>.
It is easier than you may think. Have a look at this;
import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
/**
*
* @author ubuntu
*/
public class DocxTest {
private static final String paraOneText = "Design patterns have become a
"
+ "staple of object oriented design and programming by providing
"
+ "elegant, easy-to-reuse and maintainable solutions to commonly
"
+ "encountered programming challenges. However, many busy Java "
+ "programmers have yet to learn about design patterns and "
+ "incorporate this powerful technology into their work.";
private static final String p2PartOneText = "Java Design Patterns is "
+ "exactly the tutorial resource you need. Gentle and clearly "
+ "written, it helps you ";
private static final String p2PartTwoText = "understand";
private static final String p2PartThreeText = " the nature and purpose
of "
+ "design patterns. It also serves as a practical guide to using
"
+ "design patterns to create sophisticated, robust Java
programs.";
public DocxTest(String filename) throws IOException {
File file = null;
FileOutputStream fos = null;
XWPFDocument document = null;
XWPFParagraph para = null;
XWPFRun run = null;
try {
// Create the first paragraph and set it's text.
document = new XWPFDocument();
para = document.createParagraph();
// Set the alignement of the paragraph. The ParagraphAlignment
class
// defines a number of values - as an Enumeration - that allow
you to
// set the alignment to, amongst others, left, right, center and
// distributed.
para.setAlignment(ParagraphAlignment.CENTER);
// Show a blank line after the paragrph. The value of 100 was
chosen
// arbirtarily.
para.setSpacingAfter(100);
// Set the text of the paragrph by creating a single run. Just a
single
// run is needed here as the format of all of the text in the
// paragrph is the same.
run = para.createRun();
run.setText(DocxTest.paraOneText);
// Paragraph two is slightly different; it will have a single
word
// set to bold, italicised.
para = document.createParagraph();
// Create the first run and set it's text.
run = para.createRun();
run.setText(DocxTest.p2PartOneText);
// Create the second run, set it's text and make sure it is
shown
// in a bold, italicised font.
run = para.createRun();
run.setText(DocxTest.p2PartTwoText);
run.setBold(true);
run.setItalic(true);
// Create the final run for the second paragraph. This will not
// be emboldened or italicised.
run = para.createRun();
run.setText(DocxTest.p2PartThreeText);
// Write the document away.
file = new File(filename);
fos = new FileOutputStream(file);
document.write(fos);
}
finally {
if(fos != null) {
fos.close();
fos = null;
}
}
}
}
You can see that there is a setAlignment() method for the paragraph and all
you need do is call this and pass the appropriate value from the
ParagraphAlignment enumeration.
Each time you wish to see a different font or treatment in a paragraph,
create a run for it ans set the various attributes of the run to get the
appearance you are after.
Yours
Mark B
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Formatting-and-Justify-Paragraphs-in-Apache-POI-tp5710562p5710578.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org