use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.
the class Docx4J_简单例子2 method main.
public static void main(String[] args) throws Exception {
Docx4J_简单例子 t = new Docx4J_简单例子();
WordprocessingMLPackage wordMLPackage = t.createWordprocessingMLPackage();
MainDocumentPart mp = wordMLPackage.getMainDocumentPart();
ObjectFactory factory = Context.getWmlObjectFactory();
// 图片页眉
// Relationship relationship =t.createHeaderPart(wordMLPackage, mp, factory);
// 文字页眉
Relationship relationship = t.createTextHeaderPart(wordMLPackage, mp, factory, "我是页眉,多创造,少抄袭", JcEnumeration.CENTER);
t.createHeaderReference(wordMLPackage, mp, factory, relationship);
t.addParagraphTest(wordMLPackage, mp, factory);
t.addPageBreak(wordMLPackage, factory);
t.createNormalTableTest(wordMLPackage, mp, factory);
// 页脚
relationship = t.createFooterPageNumPart(wordMLPackage, mp, factory);
t.createFooterReference(wordMLPackage, mp, factory, relationship);
t.saveWordPackage(wordMLPackage, new File("f:/saveFile/temp/s5_simple.docx"));
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.
the class Docx4j_创建批注_S3_Test method main.
public static void main(String[] args) throws Exception {
Docx4j_创建批注_S3_Test t = new Docx4j_创建批注_S3_Test();
WordprocessingMLPackage wordMLPackage = t.createWordprocessingMLPackage();
MainDocumentPart mp = wordMLPackage.getMainDocumentPart();
ObjectFactory factory = Context.getWmlObjectFactory();
t.testCreateComment(wordMLPackage, mp, factory);
t.saveWordPackage(wordMLPackage, new File("f:/saveFile/temp/sys_" + System.currentTimeMillis() + ".docx"));
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.
the class Docx4jStyle_S3 method testDocx4jCreateTable.
public void testDocx4jCreateTable() throws Exception {
boolean landscape = false;
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(PageSizePaper.A4, landscape);
ObjectFactory factory = Context.getWmlObjectFactory();
setPageMargins(wordMLPackage, factory);
String imgFilePath = "f:/saveFile/tmp/2sql日志.jpg";
Tbl table = createTableWithContent(wordMLPackage, factory, imgFilePath);
wordMLPackage.getMainDocumentPart().addObject(table);
wordMLPackage.save(new File("f:/saveFile/temp/sys_" + System.currentTimeMillis() + ".docx"));
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.
the class Docx4j_替换模板 method replaceTemplateDocx.
public void replaceTemplateDocx() throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
HashMap<String, String> staticMap = getStaticData();
// 替换普通变量
documentPart.variableReplace(staticMap);
Document document = (Document) documentPart.getContents();
Body body = document.getBody();
MyTblFinder tblFinder = new MyTblFinder();
new TraversalUtil(body, tblFinder);
// 替换表格模板第三行数据
Tbl firstTbl = tblFinder.getTbls().get(0);
List<Object> trObjList = firstTbl.getContent();
Tr tr = (Tr) trObjList.get(2);
replaceTrTotalData(tr, getTotalData());
int lvlIndex = 3;
// 替换表格模板第四行数据
int lvlTotalSize = 4;
tr = (Tr) trObjList.get(lvlIndex);
List<String[]> lvDataList = getLvDataList(lvlTotalSize);
replaceTrData(firstTbl, tr, lvDataList, lvlIndex);
// 重新获取表格数据
trObjList = firstTbl.getContent();
int sexTotalSize = 2;
// 替换表格模板第五行数据
tr = (Tr) trObjList.get(lvlIndex + lvlTotalSize);
List<String[]> sexDataList = getSexDataList(2);
replaceTrSexData(tr, sexDataList.get(0));
// 替换表格模板第六行数据
tr = (Tr) trObjList.get(4 + lvlTotalSize);
replaceTrSexData(tr, sexDataList.get(1));
// 替换表格模板第七行数据
tr = (Tr) trObjList.get(5 + lvlTotalSize);
int nationTotalSize = 56;
List<String[]> nationDataList = getNationDataList(nationTotalSize);
replaceTrData(firstTbl, tr, nationDataList, lvlIndex + lvlTotalSize + sexTotalSize);
// 合并层次单元格 层次位于第3行
mergeCellsVertically(firstTbl, 0, lvlIndex, lvlIndex + lvlTotalSize - 1);
// 合并民族单元格 层次+性别2行
mergeCellsVertically(firstTbl, 0, lvlIndex + lvlTotalSize + sexTotalSize, lvlIndex + lvlTotalSize + sexTotalSize + nationTotalSize - 1);
// 替换图表数据
String[] chartArr = getChartData();
replacePieChartData(wordMLPackage, chartArr);
// 保存结果
saveWordPackage(wordMLPackage, outputfilepath);
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.
the class SdtFinder method printSdtContent.
public void printSdtContent(String filePath) throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(filePath));
MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
Document wmlDocumentEl = (Document) mdp.getContents();
Body body = wmlDocumentEl.getBody();
SdtFinder sdtFinder = new SdtFinder();
new TraversalUtil(body, sdtFinder);
for (Child sdtChild : sdtFinder.sdtList) {
if (sdtChild instanceof SdtRun) {
SdtRun sdtRun = (SdtRun) sdtChild;
SdtPr sdtPr = sdtRun.getSdtPr();
printSdtPrContent(sdtPr);
// System.out.println(XmlUtils.marshaltoString(sdtPr, true,
// true));
SdtContent sdtContent = sdtRun.getSdtContent();
System.out.println("-----------p content=" + getContentAccessorContent(sdtContent));
} else if (sdtChild instanceof CTSdtCell) {
CTSdtCell sdtCell = (CTSdtCell) sdtChild;
SdtPr sdtPr = sdtCell.getSdtPr();
printSdtPrContent(sdtPr);
SdtContent sdtContent = sdtCell.getSdtContent();
System.out.println("-----------table content=" + getContentAccessorContent(sdtContent));
} else if (sdtChild instanceof SdtBlock) {
SdtBlock sdtBlock = (SdtBlock) sdtChild;
SdtPr sdtPr = sdtBlock.getSdtPr();
printSdtPrContent(sdtPr);
SdtContent sdtContent = sdtBlock.getSdtContent();
System.out.println("-----------sdtblock content=" + getContentAccessorContent(sdtContent));
}
}
}
Aggregations