Java Code Examples for org.docx4j.jaxb.Context#getWmlObjectFactory()

The following examples show how to use org.docx4j.jaxb.Context#getWmlObjectFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AddingAnInlineImageToTable.java    From docx4j-template with Apache License 2.0 7 votes vote down vote up
/**
 *  首先我们创建包和对象工厂, 因此在类的随处我们都可以使用它们. 然后我们创建一个表格并添加
 *  边框. 接下来我们创建一个表格行并在第一个域添加一些文本.
 *  对于第二个域, 我们用与前面一样的图片创建一个段落并添加进去. 最后把行添加到表格中, 并将
 *  表格添加到包中, 然后保存这个包.
 */
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"));
}
 
Example 2
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
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"));
}
 
Example 3
Source File: TableWithMergedCells.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 *  创建一个带边框的表格并添加四个带内容的行, 然后将表格添加到文档并保存
 */
public static void main (String[] args) throws Docx4JException {
    wordMLPackage = WordprocessingMLPackage.createPackage();
    factory = Context.getWmlObjectFactory();
 
    Tbl table = factory.createTbl();
    addBorders(table);
 
    addTableRowWithMergedCells("Heading 1", "Heading 1.1",
        "Field 1", table);
    addTableRowWithMergedCells(null, "Heading 1.2", "Field 2", table);
 
    addTableRowWithMergedCells("Heading 2", "Heading 2.1",
        "Field 3", table);
    addTableRowWithMergedCells(null, "Heading 2.2", "Field 4", table);
 
    wordMLPackage.getMainDocumentPart().addObject(table);
    wordMLPackage.save(new java.io.File(
        "src/main/files/HelloWord9.docx") );
}
 
Example 4
Source File: TableWithStyledContent.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
  *  跟前面的做的一样, 我们再一次创建了一个表格, 并添加了三个单元格, 其中有两个
  *  单元带有样式. 在新方法中我们传进表格行, 单元格内容, 是否为粗体及字体大小作
  *  为参数. 你需要注意, 因为the Office Open specification规范定义这个属性是半个
  *  点(half-point)大小, 因此字体大小需要是你想在Word中显示大小的两倍, 
 */
public static void main (String[] args) throws Docx4JException {
    wordMLPackage = WordprocessingMLPackage.createPackage();
    factory = Context.getWmlObjectFactory();
 
    Tbl table = factory.createTbl();
    Tr tableRow = factory.createTr();
 
    addRegularTableCell(tableRow, "Normal text");
    addStyledTableCell(tableRow, "Bold text", true, null);
    addStyledTableCell(tableRow, "Bold large text", true, "40");
 
    table.getContent().add(tableRow);
    addBorders(table);
 
    wordMLPackage.getMainDocumentPart().addObject(table);
    wordMLPackage.save(new java.io.File("src/main/files/HelloWord6.docx") );
}
 
Example 5
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
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"));  
}
 
Example 6
Source File: SettingColumnWidthForTable.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 *  创建一个带边框的表格并添加一行. 然后添加两个带内容的单元格并给定宽度.
 */
public static void main (String[] args) throws Docx4JException {
    wordMLPackage = WordprocessingMLPackage.createPackage();
    factory = Context.getWmlObjectFactory();
 
    Tbl table = factory.createTbl();
    addBorders(table);
 
    Tr tr = factory.createTr();
 
    addTableCellWithWidth(tr, "Field 1", 2500);
    addTableCellWithWidth(tr, "Field 2", 0);
 
    table.getContent().add(tr);
 
    wordMLPackage.getMainDocumentPart().addObject(table);
    wordMLPackage.save(new java.io.File("src/main/HelloWord133.docx") );
}
 
Example 7
Source File: AddingTableOfContent.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 *  首先我们创建对象工厂和包并从包中抽出文档部件. 然后我们添加目录表, 后面跟着一些带有分类
 *  标题样式的段落. 最后我们保存包.
 */
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"));
}
 
Example 8
Source File: FOPAreaTreeHelper.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
private static P createFillerP() {

    	org.docx4j.wml.ObjectFactory wmlObjectFactory = Context.getWmlObjectFactory();

    	P p = wmlObjectFactory.createP(); 
    	    // Create object for pPr
    	    PPr ppr = wmlObjectFactory.createPPr(); 
    	    p.setPPr(ppr); 
    	        // Create object for rPr
    	        ParaRPr pararpr = wmlObjectFactory.createParaRPr(); 

    	        // Create object for spacing
    	        PPrBase.Spacing pprbasespacing = wmlObjectFactory.createPPrBaseSpacing(); 
    	        ppr.setSpacing(pprbasespacing); 
    	            pprbasespacing.setBefore( BigInteger.valueOf( 800) ); 
    	            pprbasespacing.setAfter( BigInteger.valueOf( 800) ); 
    	    // Create object for r
    	    R r = wmlObjectFactory.createR(); 
    	    p.getContent().add( r); 
    	        // Create object for rPr
    	        RPr rpr = wmlObjectFactory.createRPr(); 
    	        r.setRPr(rpr); 
    	            // Create object for sz
    	            HpsMeasure hpsmeasure3 = wmlObjectFactory.createHpsMeasure(); 
    	            rpr.setSz(hpsmeasure3); 
    	                hpsmeasure3.setVal( BigInteger.valueOf( 96) ); 

    	        // Create object for t (wrapped in JAXBElement) 
    	        Text text = wmlObjectFactory.createText(); 
    	        JAXBElement<org.docx4j.wml.Text> textWrapped = wmlObjectFactory.createRT(text); 
    	        r.getContent().add( textWrapped); 
    	            text.setValue( "BODY CONTENT"); 

    	return p;
    }
 
Example 9
Source File: Docx4j_创建表格_S5_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
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"));  
}
 
Example 10
Source File: TableWithBorders.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public static void main (String[] args) throws Docx4JException {
    wordMLPackage = WordprocessingMLPackage.createPackage();
    factory = Context.getWmlObjectFactory();
 
    Tbl table = createTableWithContent();
 
    addBorders(table);
 
    wordMLPackage.getMainDocumentPart().addObject(table);
    wordMLPackage.save(new java.io.File(
        "src/main/files/HelloWord5.docx") );
}
 
Example 11
Source File: AddingAPageBreak.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public static void main (String[] args) throws Docx4JException {
    wordMLPackage = WordprocessingMLPackage.createPackage();
    factory = Context.getWmlObjectFactory();
 
    wordMLPackage.getMainDocumentPart().addParagraphOfText("Hello Word!");
 
    addPageBreak();
 
    wordMLPackage.getMainDocumentPart().addParagraphOfText("This is page 2!");
    wordMLPackage.save(new java.io.File("src/main/files/HelloWord11.docx") );
}
 
Example 12
Source File: Docx4j_创建批注_S3_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
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"));  
}
 
Example 13
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
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"));
}
 
Example 14
Source File: DocxElementWmlRender.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public DocxElementWmlRender(WordprocessingMLPackage wmlPackage){
	this.wmlPackage = wmlPackage;
	this.factory = Context.getWmlObjectFactory();  
}
 
Example 15
Source File: Docx4jExample.java    From tutorials with MIT License 4 votes vote down vote up
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);
}
 
Example 16
Source File: AddingATable.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Docx4JException {
	wordMLPackage = WordprocessingMLPackage.createPackage();
	factory = Context.getWmlObjectFactory();

	Tbl table = factory.createTbl();
	Tr tableRow = factory.createTr();

	addTableCell(tableRow, "Field 1");
	addTableCell(tableRow, "Field 2");

	table.getContent().add(tableRow);

	wordMLPackage.getMainDocumentPart().addObject(table);

	wordMLPackage.save(new java.io.File("src/main/files/HelloWord4.docx"));
}
 
Example 17
Source File: AddingPageNrToFooter.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
/**
 *  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") );
}
 
Example 18
Source File: WordprocessingMLPackageRender.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public WordprocessingMLPackageRender(WordprocessingMLPackage wmlPackage){
	this.wmlPackage = wmlPackage;
	this.factory = Context.getWmlObjectFactory();  
	this.pRender = new ParagraphWmlRender(this.wmlPackage);
	this.tbRender = new DocxElementWmlRender(this.wmlPackage);  
}
 
Example 19
Source File: DocxBuilder.java    From TranskribusCore with GNU General Public License v3.0 4 votes vote down vote up
private void addComplexField(P p, String instrText, String instrText2) {

	    org.docx4j.wml.ObjectFactory wmlObjectFactory = Context.getWmlObjectFactory();

	    // Create object for r
	    R r = wmlObjectFactory.createR(); 
	    p.getContent().add( r); 
        // Create object for fldChar (wrapped in JAXBElement) 
        FldChar fldchar = wmlObjectFactory.createFldChar(); 
        JAXBElement<org.docx4j.wml.FldChar> fldcharWrapped = wmlObjectFactory.createRFldChar(fldchar); 
        r.getContent().add( fldcharWrapped); 
            fldchar.setFldCharType(org.docx4j.wml.STFldCharType.BEGIN);
        // Create object for instrText (wrapped in JAXBElement) 
        Text text = wmlObjectFactory.createText(); 
        JAXBElement<org.docx4j.wml.Text> textWrapped = wmlObjectFactory.createRInstrText(text); 
        r.getContent().add( textWrapped); 
        text.setValue( instrText); 
        text.setSpace( "preserve");    
        
	    R r2 = wmlObjectFactory.createR(); 
	    p.getContent().add( r2); 
	    
        if (!instrText2.equals("")){
    	    
        	r2.getContent().add(wmlObjectFactory.createRTab());
        	
            text = wmlObjectFactory.createText(); 
            JAXBElement<org.docx4j.wml.Text> textWrapped2 = wmlObjectFactory.createRInstrText(text); 
            r2.getContent().add( textWrapped2); 
            text.setValue( instrText2); 
            text.setSpace( "preserve");
        	
        }

        
//        R r = factory.createR();
//        r.setRsidRPr("TOC entry text");
//        R.Tab tab = new R.Tab();
//        r.getRunContent().add(tab);             
//        p.getParagraphContent().add(r);
        
//		org.docx4j.wml.Text idx = wmlObjectFactory.createText();
//		JAXBElement<org.docx4j.wml.Text> textWrapped2 = wmlObjectFactory.createRInstrText(idx); 
//        r.getContent().add( textWrapped2); 
//        idx.setValue( idxText); 
//        idx.setSpace( "preserve"); 

        // Create object for fldChar (wrapped in JAXBElement) 
        fldchar = wmlObjectFactory.createFldChar(); 
        fldcharWrapped = wmlObjectFactory.createRFldChar(fldchar); 
        r2.getContent().add( fldcharWrapped); 
            fldchar.setFldCharType(org.docx4j.wml.STFldCharType.END);

	}
 
Example 20
Source File: ParagraphWmlRender.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public ParagraphWmlRender(WordprocessingMLPackage wmlPackage){
	this.wmlPackage = wmlPackage;
	this.factory = Context.getWmlObjectFactory();  
}