use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project docx4j-template by vindell.
the class Docx4j_Helper method testDocx4jSetPageSize.
public void testDocx4jSetPageSize() throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
String titleStr = "静夜思 李白";
String str = "床前明月光,疑似地上霜。";
String str2 = "举头望明月,低头思故乡。";
P p = Docx4j_Helper.factory.createP();
String rprStr = "<w:rPr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rFonts w:hint=\"eastAsia\" w:ascii=\"Times New Roman\" w:hAnsi=\"Times New Roman\" w:eastAsia=\"宋体\"/><w:b/><w:color w:val=\"333333\"/><w:sz w:val=\"32\"/><w:szCs w:val=\"32\"/></w:rPr>";
RPr rpr = (RPr) XmlUtils.unmarshalString(rprStr);
setParagraphContent(p, rpr, titleStr);
mdp.addObject(p);
p = Docx4j_Helper.factory.createP();
setParagraphContent(p, rpr, str);
mdp.addObject(p);
p = Docx4j_Helper.factory.createP();
PPr pPr = Docx4j_Helper.factory.createPPr();
// 设置文字方向
SectPr sectPr = Docx4j_Helper.factory.createSectPr();
TextDirection textDirect = Docx4j_Helper.factory.createTextDirection();
// 文字方向:垂直方向从右往左
textDirect.setVal("tbRl");
sectPr.setTextDirection(textDirect);
Type sectType = Docx4j_Helper.factory.createSectPrType();
// 下一页
sectType.setVal("nextPage");
sectPr.setType(sectType);
// 设置页面大小
PgSz pgSz = Docx4j_Helper.factory.createSectPrPgSz();
pgSz.setW(new BigInteger("8335"));
pgSz.setH(new BigInteger("11850"));
sectPr.setPgSz(pgSz);
pPr.setSectPr(sectPr);
p.setPPr(pPr);
setParagraphContent(p, rpr, str2);
mdp.addObject(p);
p = createParagraphWithHAlign();
setParagraphContent(p, rpr, titleStr);
mdp.addObject(p);
p = createParagraphWithHAlign();
setParagraphContent(p, rpr, str);
mdp.addObject(p);
p = createParagraphWithHAlign();
setParagraphContent(p, rpr, str2);
mdp.addObject(p);
// Docx4j_Helper.saveWordPackage(wordMLPackage, outputfilepath);
}
use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project docx4j-template by vindell.
the class Docx4j_删除所有批注_S3_Test method removeAllComment.
// 这里2个路径可以一致
public void removeAllComment(String filePath, String savePath) throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(filePath));
// 清空comments.xml内容
Parts parts = wordMLPackage.getParts();
HashMap<PartName, Part> partMap = parts.getParts();
CommentsPart commentPart = (CommentsPart) partMap.get(new PartName("/word/comments.xml"));
Comments comments = commentPart.getContents();
List<Comment> commentList = comments.getComment();
for (int i = 0, len = commentList.size(); i < len; i++) {
commentList.remove(0);
}
// 清空document.xml文件中批注
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getContents();
Body body = wmlDocumentEl.getBody();
CommentFinder cf = new CommentFinder();
new TraversalUtil(body, cf);
for (Child commentElement : cf.commentElements) {
System.out.println(commentElement.getClass().getName());
Object parent = commentElement.getParent();
List<Object> theList = ((ContentAccessor) parent).getContent();
boolean removeResult = remove(theList, commentElement);
System.out.println(removeResult);
}
wordMLPackage.save(new FileOutputStream(savePath));
}
use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project docx4j-template by vindell.
the class Docx4j_工具类_S3_Test method getAllTbl.
/**
* @Description:得到所有表格
*/
public List<Tbl> getAllTbl(WordprocessingMLPackage wordMLPackage) {
MainDocumentPart mainDocPart = wordMLPackage.getMainDocumentPart();
List<Object> objList = getAllElementFromObject(mainDocPart, Tbl.class);
if (objList == null) {
return null;
}
List<Tbl> tblList = new ArrayList<Tbl>();
for (Object obj : objList) {
if (obj instanceof Tbl) {
Tbl tbl = (Tbl) obj;
tblList.add(tbl);
}
}
return tblList;
}
use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project docx4j-template by vindell.
the class AddingPageNrToFooter method main.
/**
* First we create the package and the factory. Then we create the footer.
* Finally we add two pages with text to the document and save it.
*/
public static void main(String[] args) throws Exception {
wordMLPackage = WordprocessingMLPackage.createPackage();
factory = Context.getWmlObjectFactory();
Relationship relationship = createFooterPart();
createFooterReference(relationship);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
documentPart.addParagraphOfText("Hello World!");
addPageBreak(documentPart);
documentPart.addParagraphOfText("This is page 2!");
wordMLPackage.save(new File("src/main/files/HelloWord15.docx"));
}
use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project docx4j-template by vindell.
the class AddingTableOfContent method main.
/**
* 首先我们创建对象工厂和包并从包中抽出文档部件. 然后我们添加目录表, 后面跟着一些带有分类
* 标题样式的段落. 最后我们保存包.
*/
public static void main(String[] args) throws Docx4JException {
factory = Context.getWmlObjectFactory();
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
addTableOfContent(documentPart);
documentPart.addStyledParagraphOfText("Heading1", "Hello 1");
documentPart.addStyledParagraphOfText("Heading2", "Hello 2");
documentPart.addStyledParagraphOfText("Heading3", "Hello 3");
documentPart.addStyledParagraphOfText("Heading1", "Hello 1");
wordMLPackage.save(new File("src/main/files/HelloWord10.docx"));
}
Aggregations