use of org.docx4j.wml.RPr in project Java-Tutorial by gpcodervn.
the class Write_Format method main.
public static void main(String[] args) throws Docx4JException {
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
mainDocumentPart.addStyledParagraphOfText("Title", "Hello World!");
mainDocumentPart.addParagraphOfText("Welcome To Baeldung");
ObjectFactory factory = Context.getWmlObjectFactory();
P p = factory.createP();
R r = factory.createR();
Text t = factory.createText();
t.setValue("Welcome To Baeldung");
r.getContent().add(t);
p.getContent().add(r);
RPr rpr = factory.createRPr();
BooleanDefaultTrue b = new BooleanDefaultTrue();
rpr.setB(b);
rpr.setI(b);
rpr.setCaps(b);
Color green = factory.createColor();
green.setVal("green");
rpr.setColor(green);
r.setRPr(rpr);
mainDocumentPart.getContent().add(p);
File exportFile = new File("output/welcome2.docx");
wordPackage.save(exportFile);
System.out.println("Done!");
}
use of org.docx4j.wml.RPr in project tutorials by eugenp.
the class Docx4jExample method createDocumentPackage.
void createDocumentPackage(String outputPath, String imagePath) throws Exception {
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
mainDocumentPart.addStyledParagraphOfText("Title", "Hello World!");
mainDocumentPart.addParagraphOfText("Welcome To Baeldung!");
ObjectFactory factory = Context.getWmlObjectFactory();
P p = factory.createP();
R r = factory.createR();
Text t = factory.createText();
t.setValue("Welcome To Baeldung");
r.getContent().add(t);
p.getContent().add(r);
RPr rpr = factory.createRPr();
BooleanDefaultTrue b = new BooleanDefaultTrue();
rpr.setB(b);
rpr.setI(b);
rpr.setCaps(b);
Color red = factory.createColor();
red.setVal("green");
rpr.setColor(red);
r.setRPr(rpr);
mainDocumentPart.getContent().add(p);
File image = new File(imagePath);
byte[] fileContent = Files.readAllBytes(image.toPath());
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordPackage, fileContent);
Inline inline = imagePart.createImageInline("Baeldung Image", "Alt Text", 1, 2, false);
P Imageparagraph = addImageToParagraph(inline);
mainDocumentPart.getContent().add(Imageparagraph);
int writableWidthTwips = wordPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips();
int columnNumber = 3;
Tbl tbl = TblFactory.createTable(3, 3, writableWidthTwips / columnNumber);
List<Object> rows = tbl.getContent();
for (Object row : rows) {
Tr tr = (Tr) row;
List<Object> cells = tr.getContent();
for (Object cell : cells) {
Tc td = (Tc) cell;
td.getContent().add(p);
}
}
mainDocumentPart.getContent().add(tbl);
File exportFile = new File(outputPath);
wordPackage.save(exportFile);
}
use of org.docx4j.wml.RPr in project docx4j-template by vindell.
the class Docx4J_例子2 method createNormalTableTest.
public void createNormalTableTest(WordprocessingMLPackage wordMLPackage, MainDocumentPart t, ObjectFactory factory) throws Exception {
RPr titleRpr = getRPr(factory, "宋体", "000000", "22", STHint.EAST_ASIA, true, false, false, false);
RPr contentRpr = getRPr(factory, "宋体", "000000", "22", STHint.EAST_ASIA, false, false, false, false);
Tbl table = factory.createTbl();
CTBorder topBorder = new CTBorder();
topBorder.setColor("80C687");
topBorder.setVal(STBorder.DOUBLE);
topBorder.setSz(new BigInteger("2"));
CTBorder leftBorder = new CTBorder();
leftBorder.setVal(STBorder.NONE);
leftBorder.setSz(new BigInteger("0"));
CTBorder hBorder = new CTBorder();
hBorder.setVal(STBorder.SINGLE);
hBorder.setSz(new BigInteger("1"));
addBorders(table, topBorder, topBorder, leftBorder, leftBorder, hBorder, null);
// 百分比
double[] colWidthPercent = new double[] { 15, 20, 20, 20, 25 };
setTableGridCol(wordMLPackage, factory, table, 80, colWidthPercent);
Tr titleRow = factory.createTr();
setTableTrHeight(factory, titleRow, "500");
addTableCell(factory, wordMLPackage, titleRow, "序号", titleRpr, JcEnumeration.CENTER, true, "C6D9F1");
addTableCell(factory, wordMLPackage, titleRow, "姓甚", titleRpr, JcEnumeration.CENTER, true, "C6D9F1");
addTableCell(factory, wordMLPackage, titleRow, "名谁", titleRpr, JcEnumeration.CENTER, true, "C6D9F1");
addTableCell(factory, wordMLPackage, titleRow, "籍贯", titleRpr, JcEnumeration.CENTER, true, "C6D9F1");
addTableCell(factory, wordMLPackage, titleRow, "营生", titleRpr, JcEnumeration.CENTER, true, "C6D9F1");
table.getContent().add(titleRow);
for (int i = 0; i < 10; i++) {
Tr contentRow = factory.createTr();
addTableCell(factory, wordMLPackage, contentRow, i + "", contentRpr, JcEnumeration.CENTER, false, null);
addTableCell(factory, wordMLPackage, contentRow, "无名氏", contentRpr, JcEnumeration.CENTER, false, null);
addTableCell(factory, wordMLPackage, contentRow, "佚名", contentRpr, JcEnumeration.CENTER, false, null);
addTableCell(factory, wordMLPackage, contentRow, "武林", contentRpr, JcEnumeration.CENTER, false, null);
addTableCell(factory, wordMLPackage, contentRow, "吟诗赋曲", contentRpr, JcEnumeration.CENTER, false, null);
table.getContent().add(contentRow);
}
setTableAlign(factory, table, JcEnumeration.CENTER);
t.addObject(table);
}
use of org.docx4j.wml.RPr in project docx4j-template by vindell.
the class Docx4J_例子2 method addParagraphTest.
public void addParagraphTest(WordprocessingMLPackage wordMLPackage, MainDocumentPart t, ObjectFactory factory) throws Exception {
RPr titleRPr = getRPr(factory, "黑体", "000000", "30", STHint.EAST_ASIA, true, false, false, false);
RPr boldRPr = getRPr(factory, "宋体", "000000", "24", STHint.EAST_ASIA, true, false, false, false);
RPr fontRPr = getRPr(factory, "宋体", "000000", "22", STHint.EAST_ASIA, false, false, false, false);
P paragraph = factory.createP();
Text txt = factory.createText();
R run = factory.createR();
txt.setValue("七年级上册Unit2 This is just a test. sectionA测试卷答题卡");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(titleRPr);
paragraph.getContent().add(run);
t.addObject(paragraph);
paragraph = factory.createP();
setParagraphSpacing(factory, paragraph, JcEnumeration.CENTER, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
txt = factory.createText();
txt.setValue("班级:________ 姓名:________");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("一、单选题");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(boldRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", "200", "100", true, "240", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("1.下列有关仪器用途的说法错误的是( )");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("A.烧杯用于较多量试剂的反应容器");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("B.烧杯用于较多量试剂的反应容器");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("C.烧杯用于较多量试剂的反应容器");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("D.烧杯用于较多量试剂的反应容器");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("2.下列实验操作中,正确的是( ) ");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
// 段前8磅 段后0.5磅
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "160", "10", null, null, true, "240", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
File file = new File("f:/saveFile/temp/image1.png");
java.io.InputStream is = new java.io.FileInputStream(file);
createImageParagraph(wordMLPackage, factory, paragraph, "img_1", "A.", BufferUtil.getBytesFromInputStream(is), JcEnumeration.LEFT);
file = new File("f:/saveFile/temp/image2.png");
is = new java.io.FileInputStream(file);
createImageParagraph(wordMLPackage, factory, paragraph, "img_2", StringUtils.leftPad("B.", 20), BufferUtil.getBytesFromInputStream(is), JcEnumeration.LEFT);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
file = new File("f:/saveFile/temp/image3.png");
is = new java.io.FileInputStream(file);
createImageParagraph(wordMLPackage, factory, paragraph, "img_3", "C.", BufferUtil.getBytesFromInputStream(is), JcEnumeration.LEFT);
file = new File("f:/saveFile/temp/image4.png");
is = new java.io.FileInputStream(file);
createImageParagraph(wordMLPackage, factory, paragraph, "img_4", StringUtils.leftPad("D.", 20), BufferUtil.getBytesFromInputStream(is), JcEnumeration.LEFT);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
t.addObject(paragraph);
addPageBreak(wordMLPackage, factory, STBrType.PAGE);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("测试首行缩进");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
setParagraphInd(factory, paragraph, JcEnumeration.LEFT, true, "200", false, null);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("测试悬挂缩进");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
setParagraphInd(factory, paragraph, JcEnumeration.LEFT, false, null, true, "200");
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("测试段前2行单倍行距");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", "200", null, true, "240", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("测试段后1行单倍行距");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, "100", true, "240", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("测试单倍行距");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("测试段前2行段后2行单倍行距");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", "200", "200", true, "240", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("测试段前10磅段后10磅段单倍行距");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "200", "200", null, null, true, "240", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("测试段前10磅段后10磅段前2行段后2行单倍行距");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "200", "200", "200", "200", true, "240", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("测试段后12磅单倍行距");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "240", null, null, true, "240", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("测试单倍行距");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("测试行距300[实际为多倍行距1.25]");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, null, true, "300", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("测试1.5倍行距");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, null, true, "360", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("测试2倍行距");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, null, true, "480", STLineSpacingRule.AUTO);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("测试最小值 12磅");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, null, true, "240", STLineSpacingRule.AT_LEAST);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("测试固定值12磅");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, null, true, "240", STLineSpacingRule.EXACT);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("测试固定值13磅");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, null, true, "260", STLineSpacingRule.EXACT);
t.addObject(paragraph);
paragraph = factory.createP();
txt = factory.createText();
txt.setValue("测试多倍行距3倍");
run = factory.createR();
run.getContent().add(txt);
run.setRPr(fontRPr);
paragraph.getContent().add(run);
setParagraphSpacing(factory, paragraph, JcEnumeration.LEFT, true, "0", "0", null, null, true, "720", STLineSpacingRule.AUTO);
t.addObject(paragraph);
}
use of org.docx4j.wml.RPr in project docx4j-template by vindell.
the class Docx4J_简单例子 method createNormalTableTest.
public void createNormalTableTest(WordprocessingMLPackage wordMLPackage, MainDocumentPart t, ObjectFactory factory) throws Exception {
RPr titleRpr = getRPr(factory, "宋体", "000000", "22", STHint.EAST_ASIA, true, false, false, false);
RPr contentRpr = getRPr(factory, "宋体", "000000", "22", STHint.EAST_ASIA, false, false, false, false);
Tbl table = factory.createTbl();
addBorders(table, "2");
Tr titleRow = factory.createTr();
addTableCell(factory, wordMLPackage, titleRow, "序号", titleRpr, JcEnumeration.CENTER, true, "C6D9F1");
addTableCell(factory, wordMLPackage, titleRow, "姓甚", titleRpr, JcEnumeration.CENTER, true, "C6D9F1");
addTableCell(factory, wordMLPackage, titleRow, "名谁", titleRpr, JcEnumeration.CENTER, true, "C6D9F1");
addTableCell(factory, wordMLPackage, titleRow, "籍贯", titleRpr, JcEnumeration.CENTER, true, "C6D9F1");
addTableCell(factory, wordMLPackage, titleRow, "营生", titleRpr, JcEnumeration.CENTER, true, "C6D9F1");
table.getContent().add(titleRow);
for (int i = 0; i < 10; i++) {
Tr contentRow = factory.createTr();
addTableCell(factory, wordMLPackage, contentRow, i + "", contentRpr, JcEnumeration.CENTER, false, null);
addTableCell(factory, wordMLPackage, contentRow, "无名氏", contentRpr, JcEnumeration.CENTER, false, null);
addTableCell(factory, wordMLPackage, contentRow, "佚名", contentRpr, JcEnumeration.CENTER, false, null);
addTableCell(factory, wordMLPackage, contentRow, "武林", contentRpr, JcEnumeration.CENTER, false, null);
addTableCell(factory, wordMLPackage, contentRow, "吟诗赋曲", contentRpr, JcEnumeration.CENTER, false, null);
table.getContent().add(contentRow);
}
setTableAlign(factory, table, JcEnumeration.CENTER);
t.addObject(table);
}
Aggregations