Java Code Examples for org.docx4j.Docx4J#load()

The following examples show how to use org.docx4j.Docx4J#load() . 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: WatermarkPicture.java    From kbase-doc with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception
    {
    	
		// The image to add
		//imageFile = new File(System.getProperty("user.dir") + "/src/test/resources/images/greentick.png" );  
		
    	// Save it to
		//DOCX_OUT = System.getProperty("user.dir") + "/OUT_WaterMarkPicture.docx";
    	
    	imageFile = new File("E:\\ConvertTester\\images\\jshrss-logo.png");
    	DOCX_OUT = "E:\\ConvertTester\\docx\\NVR5X-I人脸比对配置-ekozhan.docx";

    	WatermarkPicture sample = new WatermarkPicture();
    	

        File f = new File(DOCX_OUT);
//    	wordMLPackage = WordprocessingMLPackage.createPackage();
    	
//        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(f);
        WordprocessingMLPackage wordMLPackage = Docx4J.load(f);
        sample.addWaterMark(wordMLPackage);
//    	sample.addBackground(wordMLPackage);
    }
 
Example 2
Source File: WordprocessingMLDocxSaxTemplate.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * 变量替换方式实现(只能解决固定模板的word生成)
 * @param template :模板内容
 * @param variables :变量
 * @return {@link WordprocessingMLPackage} 对象
 * @throws Exception :异常对象
 */
@Override
public WordprocessingMLPackage process(InputStream template, Map<String, Object> variables) throws Exception {
	// Document loading (required)
	WordprocessingMLPackage wordMLPackage;
	if (template == null) {
		// 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 InputStream");
		wordMLPackage = Docx4J.load(template);
	}
       if (null != variables && !variables.isEmpty()) {
       	// 替换变量并输出Word文档 
       	MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
       	// 将${}里的内容结构层次替换为一层
       	VariablePrepare.prepare(wordMLPackage);
       	WMLPackageUtils.cleanDocumentPart(documentPart);
       	// 替换变量
       	documentPart.pipe( new VariableReplaceSAXHandler( this.getPlaceholderStart() , this.getPlaceholderEnd(), variables) );
        }
       // 返回WordprocessingMLPackage对象
	return FontMapperHolder.useFontMapper(wordMLPackage);
}
 
Example 3
Source File: WordprocessingMLDocxStAXTemplate.java    From docx4j-template with Apache License 2.0 6 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);
       	// 替换变量
       	documentPart.pipe( new VariableReplaceSaTXHandler( this.getPlaceholderStart() , this.getPlaceholderEnd(), variables) );
        }
       // 返回WordprocessingMLPackage对象
	return FontMapperHolder.useFontMapper(wordMLPackage);
}
 
Example 4
Source File: WordprocessingMLDocxStAXTemplate.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * 变量替换方式实现(只能解决固定模板的word生成)
 * @param template :模板内容
 * @param variables :变量
 * @return {@link WordprocessingMLPackage} 对象
 * @throws Exception :异常对象
 */
@Override
public WordprocessingMLPackage process(InputStream template, Map<String, Object> variables) throws Exception {
	// Document loading (required)
	WordprocessingMLPackage wordMLPackage;
	if (template == null) {
		// 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 InputStream");
		wordMLPackage = Docx4J.load(template);
	}
       if (null != variables && !variables.isEmpty()) {
       	// 替换变量并输出Word文档 
       	MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
       	// 将${}里的内容结构层次替换为一层
       	VariablePrepare.prepare(wordMLPackage);
       	WMLPackageUtils.cleanDocumentPart(documentPart);
       	// 替换变量
       	documentPart.pipe( new VariableReplaceSaTXHandler( this.getPlaceholderStart() , this.getPlaceholderEnd(), variables) );
        }
       // 返回WordprocessingMLPackage对象
	return FontMapperHolder.useFontMapper(wordMLPackage);
}
 
Example 5
Source File: WordprocessingMLDocxSaxTemplate.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);
       	// 替换变量
       	documentPart.pipe( new VariableReplaceSAXHandler( this.getPlaceholderStart() , this.getPlaceholderEnd(), variables) );
        }
       // 返回WordprocessingMLPackage对象
	return FontMapperHolder.useFontMapper(wordMLPackage);
}
 
Example 6
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 7
Source File: WordprocessingMLDocxTemplate.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * 变量替换方式实现(只能解决固定模板的word生成)
 * @param template :模板内容
 * @param variables :变量
 * @return {@link WordprocessingMLPackage} 对象
 * @throws Exception :异常对象
 */
@Override
public WordprocessingMLPackage process(InputStream template, Map<String, Object> variables) throws Exception {
	// Document loading (required)
	WordprocessingMLPackage wordMLPackage;
	if (template == null) {
		// 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 InputStream");
		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 8
Source File: WordprocessingMLTemplate.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * @param template :模板文件
 * @param variables :变量
 * @return {@link WordprocessingMLPackage} 对象
 * @throws Exception :异常对象
 */
default 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);
	}
	return wordMLPackage;
}
 
Example 9
Source File: WordprocessingMLTemplate.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * @param template :模板文件流
 * @param variables :变量
 * @return {@link WordprocessingMLPackage} 对象
 * @throws Exception :异常对象
 */
default WordprocessingMLPackage process(InputStream template, Map<String, Object> variables) throws Exception{
	// Document loading (required)
	WordprocessingMLPackage wordMLPackage;
	if (template == null) {
		// 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 InputStream");
		wordMLPackage = Docx4J.load(template);
	}
	return wordMLPackage;
}