本文整理汇总了Java中org.docx4j.openpackaging.packages.WordprocessingMLPackage类的典型用法代码示例。如果您正苦于以下问题:Java WordprocessingMLPackage类的具体用法?Java WordprocessingMLPackage怎么用?Java WordprocessingMLPackage使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WordprocessingMLPackage类属于org.docx4j.openpackaging.packages包,在下文中一共展示了WordprocessingMLPackage类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入依赖的package包/类
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);
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/s_simple.docx"));
}
示例2: main
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入依赖的package包/类
/**
* 首先我们创建包和对象工厂, 因此在类的随处我们都可以使用它们. 然后我们创建一个表格并添加
* 边框. 接下来我们创建一个表格行并在第一个域添加一些文本.
* 对于第二个域, 我们用与前面一样的图片创建一个段落并添加进去. 最后把行添加到表格中, 并将
* 表格添加到包中, 然后保存这个包.
*/
public static void main (String[] args) throws Exception {
wordMLPackage = WordprocessingMLPackage.createPackage();
factory = Context.getWmlObjectFactory();
Tbl table = factory.createTbl();
addBorders(table);
Tr tr = factory.createTr();
P paragraphOfText = wordMLPackage.getMainDocumentPart().createParagraphOfText("Field 1");
addTableCell(tr, paragraphOfText);
File file = new File("src/main/resources/iProfsLogo.png");
P paragraphWithImage = addInlineImageToParagraph(createInlineImage(file));
addTableCell(tr, paragraphWithImage);
table.getContent().add(tr);
wordMLPackage.getMainDocumentPart().addObject(table);
wordMLPackage.save(new java.io.File("src/main/files/HelloWord8.docx"));
}
示例3: setDocumentSize
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入依赖的package包/类
/**
* @Description: 设置页面大小及纸张方向 landscape横向
*/
public void setDocumentSize(WordprocessingMLPackage wordPackage,
ObjectFactory factory, String width, String height,
STPageOrientation stValue) {
SectPr sectPr = getDocSectPr(wordPackage);
PgSz pgSz = sectPr.getPgSz();
if (pgSz == null) {
pgSz = factory.createSectPrPgSz();
sectPr.setPgSz(pgSz);
}
if (StringUtils.isNotBlank(width)) {
pgSz.setW(new BigInteger(width));
}
if (StringUtils.isNotBlank(height)) {
pgSz.setH(new BigInteger(height));
}
if (stValue != null) {
pgSz.setOrient(stValue);
}
}
示例4: main
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入依赖的package包/类
/**
* First we create the package, then we alter the style sheet and add some
* styled paragraphs. Finally we save the package.
*/
public static void main (String[] args) throws Docx4JException {
wordMLPackage = WordprocessingMLPackage.createPackage();
alterStyleSheet();
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title",
"Hello World! This title is now in Arial.");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Subtitle",
"Subtitle, this subtitle is now Arial too");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Heading1",
"As is Heading1");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Heading2",
"Heading2 is now Arial, no longer bold and has an underline " +
"and fontsize 12");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Heading3",
"Heading3 is now Arial");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Normal",
"And normal text has changed to Arial and fontsize 10");
wordMLPackage.save(new java.io.File("src/main/files/HelloWord12.docx") );
}
示例5: newImage
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入依赖的package包/类
public P newImage(WordprocessingMLPackage wordMLPackage,
ObjectFactory factory, byte[] bytes, String filenameHint,
String altText, int id1, int id2, long cx) throws Exception {
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
.createImagePart(wordMLPackage, bytes);
Inline inline = imagePart.createImageInline(filenameHint, altText, id1,
id2, cx, false);
// Now add the inline in w:p/w:r/w:drawing
P p = factory.createP();
R run = factory.createR();
p.getContent().add(run);
Drawing drawing = factory.createDrawing();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
return p;
}
示例6: createFooterReference
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入依赖的package包/类
public void createFooterReference(
WordprocessingMLPackage wordprocessingMLPackage,
MainDocumentPart t, ObjectFactory factory, Relationship relationship)
throws InvalidFormatException {
List<SectionWrapper> sections = wordprocessingMLPackage
.getDocumentModel().getSections();
SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
// There is always a section wrapper, but it might not contain a sectPr
if (sectPr == null) {
sectPr = factory.createSectPr();
t.addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
FooterReference footerReference = factory.createFooterReference();
footerReference.setId(relationship.getId());
footerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(footerReference);
}
示例7: getComments
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入依赖的package包/类
public int getComments(WordprocessingMLPackage wordMLPackage){
Parts parts = wordMLPackage.getParts();
HashMap<PartName, Part> partMap = parts.getParts();
CommentsPart commentPart = (CommentsPart) partMap.get(new CommentsPart().getPartName());
Comments comments = commentPart.getContents();
List<Comment> commentList = comments.getComment();
for (Comment comment : commentList) {
StringBuffer sb = new StringBuffer();
sb.append(" ID: ").append(comment.getId());
sb.append(" 作者:").append(comment.getAuthor());
sb.append(" 时间: ").append(comment.getDate().toGregorianCalendar().getTime());
sb.append(" 内容:").append(comment.getContent());
sb.append(" 文中内容:").append(docCmtMap.get(comment.getId().toString()));
System.out.println(sb.toString());
}
}
示例8: replaceTable
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入依赖的package包/类
public static void replaceTable(String[] placeholders, List<Map<String, String>> textToAdd,
WordprocessingMLPackage template) throws Docx4JException, JAXBException {
List<Tbl> tables = getTargetElements(template.getMainDocumentPart(), Tbl.class);
// 1. find the table
Tbl tempTable = getTable(tables, placeholders[0]);
List<Tr> rows = getTargetElements(tempTable, Tr.class);
// first row is header, second row is content
if (rows.size() == 2) {
// this is our template row
Tr templateRow = (Tr) rows.get(1);
for (Map<String, String> replacements : textToAdd) {
// 2 and 3 are done in this method
addRowToTable(tempTable, templateRow, replacements);
}
// 4. remove the template row
tempTable.getContent().remove(templateRow);
}
}
示例9: createFooterReference
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入依赖的package包/类
/**
* 创建页脚引用关系
*
* @param word
* @param relationship
* @throws InvalidFormatException
*/
public static void createFooterReference(
WordprocessingMLPackage word,
Relationship relationship )
throws InvalidFormatException {
List<SectionWrapper> sections = word.getDocumentModel().getSections();
SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
// There is always a section wrapper, but it might not contain a sectPr
if (sectPr==null ) {
sectPr = factory.createSectPr();
word.getMainDocumentPart().addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
FooterReference footerReference = factory.createFooterReference();
footerReference.setId(relationship.getId());
footerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(footerReference);
}
示例10: getFtr
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入依赖的package包/类
public Ftr getFtr(WordprocessingMLPackage wordprocessingMLPackage,
ObjectFactory factory, Part sourcePart, boolean isUnderLine,
String underLineSz) throws Exception {
Ftr ftr = factory.createFtr();
File file = new File("f:/saveFile/tmp/xxt.jpg");
java.io.InputStream is = new java.io.FileInputStream(file);
ftr.getContent().add(
newImage(wordprocessingMLPackage, factory, sourcePart,
BufferUtil.getBytesFromInputStream(is), "filename",
"这是页脚", 1, 2, isUnderLine, underLineSz,
JcEnumeration.CENTER));
if (isUnderLine) {
ftr.getContent().add(
createHeaderBlankP(wordprocessingMLPackage, factory,
underLineSz, JcEnumeration.CENTER));
}
return ftr;
}
示例11: writeToStream
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入依赖的package包/类
public static void writeToStream(WordprocessingMLPackage wmlPackage,OutputStream output) throws IOException, Docx4JException {
Assert.notNull(wmlPackage, " wmlPackage is not specified!");
Assert.notNull(output, " output is not specified!");
InputStream input = null;
try {
//Document对象
MainDocumentPart document = wmlPackage.getMainDocumentPart();
//Document XML
String documentXML = XmlUtils.marshaltoString(wmlPackage);
//转成字节输入流
input = IOUtils.toBufferedInputStream(new ByteArrayInputStream(documentXML.getBytes()));
//输出模板
IOUtils.copy(input, output);
} finally {
IOUtils.closeQuietly(input);
}
}
示例12: writeToWriter
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入依赖的package包/类
public void writeToWriter(WordprocessingMLPackage wmlPackage,Writer output) throws IOException, Docx4JException {
Assert.notNull(wmlPackage, " wmlPackage is not specified!");
Assert.notNull(output, " output is not specified!");
InputStream input = null;
try {
//Document对象
MainDocumentPart document = wmlPackage.getMainDocumentPart();
//Document XML
String documentXML = XmlUtils.marshaltoString(wmlPackage.getPackage());
//转成字节输入流
input = IOUtils.toBufferedInputStream(new ByteArrayInputStream(documentXML.getBytes()));
//获取模板输出编码格式
String charsetName = Docx4jProperties.getProperty(Docx4jConstants.DOCX4J_CONVERT_OUT_WMLTEMPLATE_CHARSETNAME, Docx4jConstants.DEFAULT_CHARSETNAME );
//输出模板
IOUtils.copy(input, output, Charset.forName(charsetName));
} finally {
IOUtils.closeQuietly(input);
}
}
示例13: main
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
// 加载模板
InputStream template = HtmlToDOCDemo.class.getResourceAsStream("kcjj.xml");
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(template);
PhysicalFontUtils.setSimSunFont(wordMLPackage);
// 加载Html数据
String jxdg = IOUtils.toString(HtmlToDOCDemo.class.getResourceAsStream("data.txt"));
Map<String, String> richTextMap = new HashMap<String, String>();
richTextMap.put("jxdg", jxdg);
replaceRichText(wordMLPackage, richTextMap);
File file = new File("d://result4.docx");
wordMLPackage.save(file);
System.out.println("success");
}
示例14: createHeaderReference
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入依赖的package包/类
/**
* 创建页眉引用关系
*
* @param word
* @param relationship
* @throws InvalidFormatException
*/
public static void createHeaderReference(
WordprocessingMLPackage word,
Relationship relationship )
throws InvalidFormatException {
List<SectionWrapper> sections = word.getDocumentModel().getSections();
SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
// There is always a section wrapper, but it might not contain a sectPr
if (sectPr==null ) {
sectPr = factory.createSectPr();
word.getMainDocumentPart().addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
HeaderReference headerReference = factory.createHeaderReference();
headerReference.setId(relationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);
}
示例15: createHeaderReference
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入依赖的package包/类
public void createHeaderReference(
WordprocessingMLPackage wordprocessingMLPackage,
MainDocumentPart t, ObjectFactory factory, Relationship relationship)
throws InvalidFormatException {
List<SectionWrapper> sections = wordprocessingMLPackage
.getDocumentModel().getSections();
SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
// There is always a section wrapper, but it might not contain a sectPr
if (sectPr == null) {
sectPr = factory.createSectPr();
t.addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
HeaderReference headerReference = factory.createHeaderReference();
headerReference.setId(relationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);
}