当前位置: 首页>>代码示例>>Java>>正文


Java WordprocessingMLPackage.getMainDocumentPart方法代码示例

本文整理汇总了Java中org.docx4j.openpackaging.packages.WordprocessingMLPackage.getMainDocumentPart方法的典型用法代码示例。如果您正苦于以下问题:Java WordprocessingMLPackage.getMainDocumentPart方法的具体用法?Java WordprocessingMLPackage.getMainDocumentPart怎么用?Java WordprocessingMLPackage.getMainDocumentPart使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.docx4j.openpackaging.packages.WordprocessingMLPackage的用法示例。


在下文中一共展示了WordprocessingMLPackage.getMainDocumentPart方法的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"));
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:21,代码来源:Docx4J_简单例子.java

示例2: 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);
	}
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:18,代码来源:WordprocessingMLTemplateWriter.java

示例3: 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);
	}
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:20,代码来源:WordprocessingMLTemplateWriter.java

示例4: getAllTbl

import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入方法依赖的package包/类
/**
 * @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;
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:19,代码来源:Docx4j_工具类_S3_Test.java

示例5: 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);  
    //文字页眉  
    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"));  
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:24,代码来源:Docx4J_简单例子2.java

示例6: testDocx4jSetPageSize

import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入方法依赖的package包/类
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);  
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:53,代码来源:Docx4j_Helper.java

示例7: handle

import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入方法依赖的package包/类
public static WordprocessingMLPackage handle(WordprocessingMLPackage wmlPackage, Document doc,boolean fragment,boolean altChunk) throws IOException, Docx4JException {
	//设置转换模式
	doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml).escapeMode(Entities.EscapeMode.xhtml);  //转为 xhtml 格式
	
	if(altChunk){
		//Document对象
		MainDocumentPart document = wmlPackage.getMainDocumentPart();
		//获取Jsoup参数
		String charsetName = Docx4jProperties.getProperty(Docx4jConstants.DOCX4J_JSOUP_PARSE_CHARSETNAME, Docx4jConstants.DEFAULT_CHARSETNAME );
		//设置转换模式
		doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml).escapeMode(Entities.EscapeMode.xhtml);  //转为 xhtml 格式
		//创建html导入对象
		//XHTMLImporterImpl xhtmlImporter = new XHTMLImporterImpl(wordMLPackage);
		document.addAltChunk(AltChunkType.Xhtml, (fragment ? doc.body().html() : doc.html()) .getBytes(Charset.forName(charsetName)));
		//document.addAltChunk(type, bytes, attachmentPoint)
		//document.addAltChunk(type, is)
		//document.addAltChunk(type, is, attachmentPoint)
		WordprocessingMLPackage tempPackage = document.convertAltChunks();
		
		//返回处理后的WordprocessingMLPackage对象
		return tempPackage;
	}
	
	//创建html导入对象
	XHTMLImporterImpl xhtmlImporter = new XHTMLImporterImpl(wmlPackage);
	//将xhtml转换为wmlPackage可用的对象
	List<Object> list = xhtmlImporter.convert((fragment ? doc.body().html() : doc.html()), doc.baseUri());
	//导入转换后的内容对象
	wmlPackage.getMainDocumentPart().getContent().addAll(list);
	//返回原WordprocessingMLPackage对象
	return wmlPackage;
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:33,代码来源:XHTMLImporterUtils.java

示例8: extract

import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入方法依赖的package包/类
public void extract(WordprocessingMLPackage wmlPackage,Writer out) throws Exception {
	MainDocumentPart documentPart = wmlPackage.getMainDocumentPart();		
	org.docx4j.wml.Document wmlDocumentEl = documentPart.getContents();
	TextUtils.extractText(wmlDocumentEl, out);
	//out.flush();
	//out.close();
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:8,代码来源:WordprocessingMLPackageExtractor.java

示例9: setDocumentBackGround

import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入方法依赖的package包/类
/**
 * @Description: 设置页面背景色
 */
public void setDocumentBackGround(WordprocessingMLPackage wordPackage,
        ObjectFactory factory, String color) throws Exception {
    MainDocumentPart mdp = wordPackage.getMainDocumentPart();
    CTBackground bkground = mdp.getContents().getBackground();
    if (StringUtils.isNotBlank(color)) {
        if (bkground == null) {
            bkground = factory.createCTBackground();
            bkground.setColor(color);
        }
        mdp.getContents().setBackground(bkground);
    }
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:16,代码来源:Docx4j_工具类_S3_Test.java

示例10: name

import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入方法依赖的package包/类
public static void name() {
  
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));  
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();  
    String titleStr = "测试插入段落";  
    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);  
    documentPart.getContent().add(5, p);  
      
    String tblPrStr = "<w:tblPr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:tblW w:w=\"8522\" w:type=\"dxa\"/><w:tblBorders><w:top w:val=\"single\"  w:sz=\"4\" w:space=\"0\"/><w:left w:val=\"single\"  w:sz=\"4\" w:space=\"0\"/><w:bottom w:val=\"single\"  w:sz=\"4\" w:space=\"0\"/><w:right w:val=\"single\"  w:sz=\"4\" w:space=\"0\"/><w:insideH w:val=\"single\"  w:sz=\"4\" w:space=\"0\"/></w:tblBorders></w:tblPr>";  
    Tbl tbl = Docx4j_Helper.factory.createTbl();  
    TblPr tblPr = (TblPr) XmlUtils.unmarshalString(tblPrStr);  
    tbl.setTblPr(tblPr);  
    Tr tr = Docx4j_Helper.factory.createTr();  
    Tc tc = Docx4j_Helper.factory.createTc();  
    tr.getContent().add(tc);  
      
    tc = Docx4j_Helper.factory.createTc();  
    tr.getContent().add(tc);  
      
    tc = Docx4j_Helper.factory.createTc();  
    tr.getContent().add(tc);  
      
    tc = Docx4j_Helper.factory.createTc();  
    tr.getContent().add(tc);  
      
    tc = Docx4j_Helper.factory.createTc();  
    tr.getContent().add(tc);  
    tbl.getContent().add(tr);  
      
    tr = Docx4j_Helper.factory.createTr();  
    tc = Docx4j_Helper.factory.createTc();  
    tr.getContent().add(tc);  
      
    tc = Docx4j_Helper.factory.createTc();  
    tr.getContent().add(tc);  
      
    tc = Docx4j_Helper.factory.createTc();  
    tr.getContent().add(tc);  
      
    tc = Docx4j_Helper.factory.createTc();  
    tr.getContent().add(tc);  
      
    tc = Docx4j_Helper.factory.createTc();  
    tr.getContent().add(tc);  
    tbl.getContent().add(tr);  
    documentPart.getContent().add(9, tbl);  
      
    Docx4j_Helper.saveWordPackage(wordMLPackage, outputfilepath);  
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:53,代码来源:Docx4j_Helper.java

示例11: main

import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
	Docx4J_例子2 t = new Docx4J_例子2();
	WordprocessingMLPackage wordMLPackage = t
			.createWordprocessingMLPackage();
	MainDocumentPart mp = wordMLPackage.getMainDocumentPart();
	ObjectFactory factory = Context.getWmlObjectFactory();

	Relationship relationship = t.createHeaderPart(wordMLPackage, mp,
			factory, false, "3");
	relationship = t.createTextHeaderPart(wordMLPackage, mp, factory,
			"我是页眉,独乐乐不如众乐乐", true, "3", JcEnumeration.CENTER);
	t.addParagraphTest(wordMLPackage, mp, factory);
	t.addPageBreak(wordMLPackage, factory, STBrType.PAGE);
	t.createHeaderReference(wordMLPackage, mp, factory, relationship);
	t.createNormalTableTest(wordMLPackage, mp, factory);
	t.addPageBreak(wordMLPackage, factory, STBrType.TEXT_WRAPPING);
	t.createTableTest(wordMLPackage, mp, factory);
	t.addPageBreak(wordMLPackage, factory, STBrType.TEXT_WRAPPING);
	P paragraph=factory.createP();
	CTBorder topBorder=new CTBorder() ;
	topBorder.setSpace(new BigInteger("1"));
	topBorder.setSz(new BigInteger("2"));
	topBorder.setVal(STBorder.WAVE);
	t.createParagraghLine(wordMLPackage, mp, factory, paragraph, topBorder, topBorder, topBorder, topBorder);
	mp.addObject(paragraph);
	t.createHyperlink(wordMLPackage, mp, factory,paragraph,
			"mailto:[email protected]?subject=docx4j测试", "联系我","微软雅黑","24",JcEnumeration.CENTER);
	
	// 页脚
	// relationship = t.createFooterPart(wordMLPackage, mp, factory,
	// false,"3");
	// relationship = t.createTextFooterPart(wordMLPackage, mp,
	// factory,"我是页脚", true, "3", JcEnumeration.CENTER);
	relationship = t.createFooterPageNumPart(wordMLPackage, mp, factory,
			false, "3", JcEnumeration.CENTER);
	t.createFooterReference(wordMLPackage, mp, factory, relationship);
	t.saveWordPackage(wordMLPackage, new File(
			"f:/saveFile/temp/s7_simple.docx"));
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:40,代码来源:Docx4J_例子2.java

示例12: addPageBreak

import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入方法依赖的package包/类
public void addPageBreak(WordprocessingMLPackage wordMLPackage,
		ObjectFactory factory, STBrType sTBrType) {
	MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
	Br breakObj = new Br();
	breakObj.setType(sTBrType);
	P paragraph = factory.createP();
	paragraph.getContent().add(breakObj);
	documentPart.addObject(paragraph);
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:10,代码来源:Docx4J_例子2.java

示例13: main

import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入方法依赖的package包/类
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"));  
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:11,代码来源:Docx4j_创建批注_S3_Test.java

示例14: addPageBreak

import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入方法依赖的package包/类
public void addPageBreak(WordprocessingMLPackage wordMLPackage,  
        ObjectFactory factory) {  
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();  
    Br breakObj = new Br();  
    breakObj.setType(STBrType.PAGE);  
    P paragraph = factory.createP();  
    paragraph.getContent().add(breakObj);  
    documentPart.addObject(paragraph);  
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:10,代码来源:Docx4J_简单例子2.java

示例15: main

import org.docx4j.openpackaging.packages.WordprocessingMLPackage; //导入方法依赖的package包/类
/**
 *  首先我们创建对象工厂和包并从包中抽出文档部件. 然后我们添加目录表, 后面跟着一些带有分类
 *  标题样式的段落. 最后我们保存包.
 */
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"));
}
 
开发者ID:vindell,项目名称:docx4j-template,代码行数:19,代码来源:AddingTableOfContent.java


注:本文中的org.docx4j.openpackaging.packages.WordprocessingMLPackage.getMainDocumentPart方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。