Java Code Examples for org.docx4j.openpackaging.packages.WordprocessingMLPackage#createPackage()

The following examples show how to use org.docx4j.openpackaging.packages.WordprocessingMLPackage#createPackage() . 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: PStyle12PtInTableNormalOverrideTrueTest.java    From docx4j-export-FO with Apache License 2.0 6 votes vote down vote up
@Test 
	public void testTblStyle_AllSilent() throws Exception {
		
		// Result is to use the implicit 10pt		
	
		WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
		wordMLPackage.getMainDocumentPart().setContents(
				(Document)XmlUtils.unmarshalString(mdpXml_tblStyle) );
		wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().setContents(
				(Styles)XmlUtils.unmarshalString(styles_no_font_sz) );

		setSetting(wordMLPackage, OVERRIDE);  // table style should get overridden
		
		wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
		
		// NB PropertyResolver puts 10pt in DocDefaults, if nothing is specified!
		
		ParagraphStylesInTableFix.process(wordMLPackage);
		
		Style s = getStyle(wordMLPackage, getStyleName());
		this.assertSz(s, 20);
//		Assert.assertTrue(s.getRPr().getSz().getVal().intValue()==20); 
	}
 
Example 3
Source File: PStyle12PtInTableNormalOverrideTrueTest.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
@Test 
	public void testTblStyle_BasedOnNormal() throws Exception {
		
		// Compat setting says Paragraph style overrides table style		
	
		WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
		wordMLPackage.getMainDocumentPart().setContents(
				(Document)XmlUtils.unmarshalString(mdpXml_tblStyle) );
		wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().setContents(
				(Styles)XmlUtils.unmarshalString(styles_in_basedOn_Normal) );
		
		// Use our style!
		List<Object> xpathResults = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath("//w:p", true);
		PPr ppr = Context.getWmlObjectFactory().createPPr();
		((P)xpathResults.get(0)).setPPr(ppr);
		PStyle ps = Context.getWmlObjectFactory().createPPrBasePStyle();
		ps.setVal("testStyle");
		ppr.setPStyle(ps);
		
		setSetting(wordMLPackage, OVERRIDE);  // table style should get overridden
	
		wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
		
		ParagraphStylesInTableFix.process(wordMLPackage);
		
//		// Revert style and save: 
//		ppr.setPStyle(ps); // doesn't work - wrong ref!
//		wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
		
		Style ours = null;
		for (Style s : wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().getContents().getStyle()) {
			if ("testStyle-TableNormal-BR".equals(s.getStyleId())) {
				ours = s;
				break;
			}
		}
		
//		Style s = getStyle(wordMLPackage, STYLE_NAME);
		Assert.assertTrue(ours.getRPr().getSz().getVal().intValue()==EXPECTED_RESULT); 
	}
 
Example 4
Source File: PStyle11PtInTableOverrideTrueTest.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
@Test 
	public void testTblStyle_AllSilent() throws Exception {
	
		WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
		wordMLPackage.getMainDocumentPart().setContents(
				(Document)XmlUtils.unmarshalString(mdpXml_tblStyle) );
		wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().setContents(
				(Styles)XmlUtils.unmarshalString(styles_no_font_sz) );
		
//		// NB createVirtualStylesForDocDefaults() puts 10pt there, if nothing is specified!
//		// So we need to delete that!
//		wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().createVirtualStylesForDocDefaults();
//		Style dd = wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().getStyleById("DocDefaults");
//		dd.getRPr().setSz(null);
//		dd.getRPr().setSzCs(null);
		
		setSetting(wordMLPackage, OVERRIDE);  // table style should get overridden
	
		wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
		
		ParagraphStylesInTableFix.process(wordMLPackage);
		
		Style s = getStyle(wordMLPackage, getStyleName());
		//Assert.assertTrue(s.getRPr().getSz().getVal().intValue()==40); 		
		
		if (s==null) {
			log.warn("missing style " + getStyleName());
			Assert.fail("missing style " + getStyleName());
		} else {		
			this.assertSz(s, 40);
		}		
		
		
	}
 
Example 5
Source File: WordprocessingMLDocxTemplate.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * @param template :模板文件
 * @param variables :变量
 * @return {@link WordprocessingMLPackage} 对象
 * @throws Exception :异常对象
 */
@Override
public WordprocessingMLPackage process(File template, Map<String, Object> variables) throws Exception{
	// Document loading (required)
	WordprocessingMLPackage wordMLPackage;
	if (template == null || !template.exists() || !template.isFile() ) {
		// Create a docx
		System.out.println("No imput path passed, creating dummy document");
		wordMLPackage = WordprocessingMLPackage.createPackage();
		SampleDocument.createContent(wordMLPackage.getMainDocumentPart());	
	} else {
		System.out.println("Loading file from " + template.getAbsolutePath());
		wordMLPackage = Docx4J.load(template);
	}
	if (null != variables && !variables.isEmpty()) {
       	// 替换变量并输出Word文档 
       	MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();  
       	// 将${}里的内容结构层次替换为一层
       	VariablePrepare.prepare(wordMLPackage);
       	WMLPackageUtils.cleanDocumentPart(documentPart);
           // 获取静态变量集合
           HashMap<String, String> staticMap = getStaticData(variables);
           // 替换普通变量  
           documentPart.variableReplace(staticMap);  
        }
       // 返回WordprocessingMLPackage对象
	return FontMapperHolder.useFontMapper(wordMLPackage);
}
 
Example 6
Source File: CreateBlockTest.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws SAXException, IOException, TransformerException, InvalidFormatException {
	
	/* Here we want to test white space handling in 
	 * 	protected static DocumentFragment createBlock(WordprocessingMLPackage wmlPackage, RunFontSelector runFontSelector, NodeIterator childResults,
		boolean sdt, PPr pPrDirect, PPr pPr, RPr rPr, RPr rPrParagraphMark) 
		
		
	 */
	
	// We need NodeIterator, simulating the results from XSLT
	File f = new File("src/test/java/org/docx4j/convert/out/fo/nodes.fo");
	DocumentBuilder db = XmlUtils.getNewDocumentBuilder();
	Document doc = db.parse(f);
	NodeIterator childResults = XPathAPI.selectNodeIterator(doc, "/*");

	//System.out.println(childResults.nextNode().getNodeName());
	
	// Other params don't matter
	FOConversionContext context = null; 
	String pStyleVal = "styleX";
	boolean sdt = false;
	PPr pPrDirect = new PPr();
	PPr pPr = new PPr(); 
	RPr rPr = new RPr();
	RPr rPrParagraphMark  = new RPr();
	
	WordprocessingMLPackage wmlPackage = WordprocessingMLPackage.createPackage();
	RunFontSelector runFontSelector = FOConversionContext.createRunFontSelector(wmlPackage);
	
	DocumentFragment df = XsltFOFunctions.createBlock( wmlPackage, runFontSelector,  pStyleVal,  childResults,
			 sdt,  pPrDirect,  pPr,  rPr,  rPrParagraphMark);
	
	System.out.println(XmlUtils.w3CDomNodeToString(df));
	
}
 
Example 7
Source File: PStyle11PtInTableOverrideFalseTest.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
@Test 
	@Ignore
	public void testTblStyle_BasedOn_Normal11() throws Exception {
		
		WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
		wordMLPackage.getMainDocumentPart().setContents(
				(Document)XmlUtils.unmarshalString(mdpXml_tblStyle) );
		wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().setContents(
				(Styles)XmlUtils.unmarshalString(styles_basedOn_Normal) );
		
		// Use our style!
		List<Object> xpathResults = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath("//w:p", true);
		PPr ppr = Context.getWmlObjectFactory().createPPr();
		((P)xpathResults.get(0)).setPPr(ppr);
		PStyle ps = Context.getWmlObjectFactory().createPPrBasePStyle();
		ps.setVal("testStyle");
		ppr.setPStyle(ps);
		
		setSetting(wordMLPackage, OVERRIDE); 

		wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
		
		ParagraphStylesInTableFix.process(wordMLPackage);
		
//		// Revert style and save: 
//		ppr.setPStyle(ps); // doesn't work - wrong ref!
//		wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
		
		Style ours = null;
		for (Style s : wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().getContents().getStyle()) {
			if ("testStyle-TableGrid-BR".equals(s.getStyleId())) {
				ours = s;
				break;
			}
		}
		
//		Style s = getStyle(wordMLPackage, STYLE_NAME);
		Assert.assertTrue(ours.getRPr().getSz().getVal().intValue()==22); 
	}
 
Example 8
Source File: PStyle12PtInTableGridOverrideTrueTest.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
@Test 
	public void testTblStyle_BasedOn_Normal12() throws Exception {
		
		// Compat setting says Paragraph style overrides table style		
	
		WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
		wordMLPackage.getMainDocumentPart().setContents(
				(Document)XmlUtils.unmarshalString(mdpXml_tblStyle) );
		wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().setContents(
				(Styles)XmlUtils.unmarshalString(styles_basedOn_Normal) );
		
		// Use our style!
		List<Object> xpathResults = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath("//w:p", true);
		PPr ppr = Context.getWmlObjectFactory().createPPr();
		((P)xpathResults.get(0)).setPPr(ppr);
		PStyle ps = Context.getWmlObjectFactory().createPPrBasePStyle();
		ps.setVal("testStyle");
		ppr.setPStyle(ps);
		
		setSetting(wordMLPackage, OVERRIDE); 

		wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
		
		ParagraphStylesInTableFix.process(wordMLPackage);
		
//		// Revert style and save: 
//		ppr.setPStyle(ps); // doesn't work - wrong ref!
//		wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
		
		Style ours = null;
		for (Style s : wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().getContents().getStyle()) {
			if ("testStyle-TableGrid-BR".equals(s.getStyleId())) {
				ours = s;
				break;
			}
		}
		
//		Style s = getStyle(wordMLPackage, STYLE_NAME);
		Assert.assertTrue(ours.getRPr().getSz().getVal().intValue()==EXPECTED_RESULT); 
	}
 
Example 9
Source File: PStyle11PtInTableOverrideTrueTest.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
@Test 
	public void testTblStyle_BasedOn_Normal12() throws Exception {
		
		// Compat setting says Paragraph style overrides table style		
	
		WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
		wordMLPackage.getMainDocumentPart().setContents(
				(Document)XmlUtils.unmarshalString(mdpXml_tblStyle) );
		wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().setContents(
				(Styles)XmlUtils.unmarshalString(styles_basedOn_Normal) );
		
		// Use our style!
		List<Object> xpathResults = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath("//w:p", true);
		PPr ppr = Context.getWmlObjectFactory().createPPr();
		((P)xpathResults.get(0)).setPPr(ppr);
		PStyle ps = Context.getWmlObjectFactory().createPPrBasePStyle();
		ps.setVal("testStyle");
		ppr.setPStyle(ps);
		
		setSetting(wordMLPackage, OVERRIDE); 

		wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
		
		ParagraphStylesInTableFix.process(wordMLPackage);
		
//		// Revert style and save: 
//		ppr.setPStyle(ps); // doesn't work - wrong ref!
//		wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
		
		Style ours = null;
		for (Style s : wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().getContents().getStyle()) {
			if ("testStyle-TableGrid-BR".equals(s.getStyleId())) {
				ours = s;
				break;
			}
		}
		
//		Style s = getStyle(wordMLPackage, STYLE_NAME);
		Assert.assertTrue(ours.getRPr().getSz().getVal().intValue()==EXPECTED_RESULT); 
	}
 
Example 10
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 11
Source File: WordprocessingMLPackageBuilder.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public WordprocessingMLPackage buildWhithXhtmlFragment(String xhtml,boolean altChunk) throws Exception {
/*	
 * 	根据docx4j.properties配置文件中:
 * 	docx4j.PageSize = A4
 * 	docx4j.PageOrientationLandscape = true
 * 	创建默认的WordProcessingML package
 */
		WordprocessingMLPackage wmlPackage = WordprocessingMLPackage.createPackage();
//返回WordprocessingMLPackage对象
return buildWhithXhtmlFragment(wmlPackage, xhtml, altChunk);
  }
 
Example 12
Source File: PStyle12PtInTableGridOverrideFalseTest.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
@Test 
	public void testTblStyle_BasedOn_Normal12() throws Exception {
		
		// A style basedOn Normal is ignored where the font size comes from Normal
	
		WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
		wordMLPackage.getMainDocumentPart().setContents(
				(Document)XmlUtils.unmarshalString(mdpXml_tblStyle) );
		wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().setContents(
				(Styles)XmlUtils.unmarshalString(styles_basedOn_Normal) );
		
		// Use our style!
		List<Object> xpathResults = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath("//w:p", true);
		PPr ppr = Context.getWmlObjectFactory().createPPr();
		((P)xpathResults.get(0)).setPPr(ppr);
		PStyle ps = Context.getWmlObjectFactory().createPPrBasePStyle();
		ps.setVal("testStyle");
		ppr.setPStyle(ps);
		
		setSetting(wordMLPackage, OVERRIDE); 

		wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
		
		ParagraphStylesInTableFix.process(wordMLPackage);
		
//		// Revert style and save: 
//		ppr.setPStyle(ps); // doesn't work - wrong ref!
//		wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
		
		Style ours = null;
		for (Style s : wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().getContents().getStyle()) {
			if ("testStyle-TableGrid-BR".equals(s.getStyleId())) {
				ours = s;
				break;
			}
		}
		
//		Style s = getStyle(wordMLPackage, STYLE_NAME);
		Assert.assertTrue(ours.getRPr().getSz().getVal().intValue()==EXPECTED_RESULT); 
	}
 
Example 13
Source File: WordprocessingMLPackageBuilder.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public WordprocessingMLPackage buildWhithXhtml(String html, PageSizePaper pageSize, boolean landscape, boolean altChunk) throws Exception {
//创建指定纸张大小和方向的WordprocessingMLPackage对象
      WordprocessingMLPackage wmlPackage = WordprocessingMLPackage.createPackage(pageSize, landscape); //A4纸,
      //返回WordprocessingMLPackage对象
      return buildWhithXhtml(wmlPackage, html, altChunk);
  }
 
Example 14
Source File: WordprocessingMLPackageBuilder.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public WordprocessingMLPackage buildWhithURL(String url, DataMap dataMap, PageSizePaper pageSize, boolean landscape, boolean altChunk) throws Exception {
//创建指定纸张大小和方向的WordprocessingMLPackage对象
      WordprocessingMLPackage wmlPackage = WordprocessingMLPackage.createPackage(pageSize, landscape); //A4纸,
      //返回WordprocessingMLPackage对象
      return buildWhithURL(wmlPackage, url, dataMap, altChunk);
  }
 
Example 15
Source File: PStyle12PtInTableNormalOverrideFalseTest.java    From docx4j-export-FO with Apache License 2.0 4 votes vote down vote up
@Test 
	public void testTblStyle_BasedOn_Normal12() throws Exception {
		
		// A style basedOn Normal is ignored where the font size comes from Normal
	
		WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
		wordMLPackage.getMainDocumentPart().setContents(
				(Document)XmlUtils.unmarshalString(mdpXml_tblStyle) );
		wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().setContents(
				(Styles)XmlUtils.unmarshalString(styles_basedOn_Normal) );
		
		// Use our style!
		List<Object> xpathResults = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath("//w:p", true);
		PPr ppr = Context.getWmlObjectFactory().createPPr();
		((P)xpathResults.get(0)).setPPr(ppr);
		PStyle ps = Context.getWmlObjectFactory().createPPrBasePStyle();
		ps.setVal("testStyle");
		ppr.setPStyle(ps);
		
		setSetting(wordMLPackage, OVERRIDE); 

		wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
		
		ParagraphStylesInTableFix.process(wordMLPackage);
		
//		// Revert style and save: 
//		ppr.setPStyle(ps); // doesn't work - wrong ref!
//		wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
		
		Style ours = null;
		for (Style s : wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().getContents().getStyle()) {
			if ("testStyle-TableNormal-BR".equals(s.getStyleId())) {
				ours = s;
				break;
			}
		}
		
//		Style s = getStyle(wordMLPackage, STYLE_NAME);
		assertSz(ours, EXPECTED_RESULT);

	}
 
Example 16
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public WordprocessingMLPackage createWordprocessingMLPackage()
		throws Exception {
	return WordprocessingMLPackage.createPackage();
}
 
Example 17
Source File: WordprocessingMLPackageBuilder.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public WordprocessingMLPackage buildWhithXhtml(File htmlFile, PageSizePaper pageSize, boolean landscape ,boolean altChunk) throws Exception {
//创建指定纸张大小和方向的WordprocessingMLPackage对象
      WordprocessingMLPackage wmlPackage = WordprocessingMLPackage.createPackage(pageSize, landscape); //A4纸,
      //返回WordprocessingMLPackage对象
      return buildWhithXhtml(wmlPackage, htmlFile , altChunk);
  }
 
Example 18
Source File: Docx4j_Helper.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
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);  
}
 
Example 19
Source File: BackgroundImage.java    From kbase-doc with Apache License 2.0 4 votes vote down vote up
public void addBackground() throws Exception
{
	
	image = this.getImage();
	
	System.out.println("ekozhan" + image.length);

	wordMLPackage = WordprocessingMLPackage.createPackage();
    
	BinaryPartAbstractImage imagePartBG = BinaryPartAbstractImage.createImagePart(wordMLPackage, image);

	wordMLPackage.getMainDocumentPart().getJaxbElement().setBackground(
			createBackground(
					imagePartBG.getRelLast().getId())); 

	wordMLPackage.getMainDocumentPart().addParagraphOfText("Ekozhan");
	
    File f = new File(DOCX_OUT);
    wordMLPackage.save(f);

}
 
Example 20
Source File: PStyleTableAbstract.java    From docx4j-export-FO with Apache License 2.0 4 votes vote down vote up
protected WordprocessingMLPackage test(String documentXml, String styleXml,  int expectedResult) throws Exception {

		WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
		wordMLPackage.getMainDocumentPart().setContents(
				(Document)XmlUtils.unmarshalString(documentXml) );
		wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().setContents(
				(Styles)XmlUtils.unmarshalString(styleXml) );
		
		setSetting(wordMLPackage, OVERRIDE); 
		if (OVERRIDE) {
			log.info("table style should get overridden by Normal");
		} else {
			log.info("table style should NOT get overridden by Normal");			
		}

		/* Where Normal is basedOn our DocDefaults style,  
		 * Word *does* override the table style! 
		 * 
		 * It doesn't if there is just a normal DocDefaults element
		 * which sets the font size.  And that is what we should be testing here.
		 * 
		 * So save the docx
		 * 
		 * Note that createVirtualStylesForDocDefaults() puts 10pt there, if nothing is specified,
		 * so we save the docx here before that method has run.
		 * 
		 * TODO 2016 01 18 revisit this, since createVirtualStylesForDocDefaults has gone now
		 * (its only in StyleTree)
		 */
		wordMLPackage.save(new File(System.getProperty("user.dir") + "/OUT_PStyleInTableTest.docx"));
//		this.saveDocx(wordMLPackage, null);
		
		ParagraphStylesInTableFix.process(wordMLPackage);
				
//		// Now remove the style, and save the docx, to check in Word
//		Style s = getStyle(wordMLPackage, STYLE_NAME);
//		wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().getContents().getStyle().remove(s);
//		
//		getStyle(wordMLPackage, STYLE_NAME).setBasedOn(null);
//		
//		this.saveDocx(wordMLPackage, null);
		
		Style s = getStyle(wordMLPackage, getStyleName());

		if (s==null) {
			log.warn("missing style " + getStyleName());
			Assert.fail("missing style " + getStyleName());
		} else {		
			assertSz(s, expectedResult);
		}
		
		return wordMLPackage;
		
	}