Java Code Examples for org.docx4j.XmlUtils#marshaltoString()

The following examples show how to use org.docx4j.XmlUtils#marshaltoString() . 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: WordprocessingMLTemplateWriter.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
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);
	}
}
 
Example 2
Source File: WordprocessingMLTemplateWriter.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
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);
	}
}
 
Example 3
Source File: HtmlToDOCDemo.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
private static List<Object> convertToWmlObject(
		WordprocessingMLPackage wordMLPackage, String content)
		throws Docx4JException, JAXBException {
	MainDocumentPart document = wordMLPackage.getMainDocumentPart();
	//获取Jsoup参数
	String charsetName = Docx4jProperties.getProperty(Docx4jConstants.DOCX4J_CONVERT_OUT_WMLTEMPLATE_CHARSETNAME, Docx4jConstants.DEFAULT_CHARSETNAME );
	
	List<Object> wmlObjList = null;
	String templateString = XmlUtils.marshaltoString(document.getContents().getBody());
	System.out.println(templateString);
	Body templateBody = document.getContents().getBody();
	try {
		document.getContents().setBody(XmlUtils.deepCopy(templateBody));
		document.getContent().clear();
		Document doc = Jsoup.parse(content);
		doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml).escapeMode(Entities.EscapeMode.xhtml);
		//XHTMLImporterImpl xhtmlImporter = new XHTMLImporterImpl(wordMLPackage);
		
		AlternativeFormatInputPart  part = document.addAltChunk(AltChunkType.Xhtml,doc.html().getBytes(Charset.forName(charsetName)));
		
		WordprocessingMLPackage tempPackage = document.convertAltChunks();
		File file = new File("d://temp.docx");
		tempPackage.save(file);
		wmlObjList = document.getContent();
		//part.getOwningRelationshipPart().getSourceP().get
		//wmlObjList = xhtmlImporter.convert(doc.html(), doc.baseUri());
	} finally {
		document.getContents().setBody(templateBody);
	}
	return wmlObjList;
}
 
Example 4
Source File: Docx4j_替换模板.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/** 
 * 获取单元格字符串 
 */  
private String[] getTcMarshalStr(Tr tr) {  
    List<Object> tcObjList = tr.getContent();  
    String[] marshaArr = new String[7];  
    // 跳过层次  
    for (int i = 1, len = tcObjList.size(); i < len; i++) {  
        marshaArr[i - 1] = XmlUtils.marshaltoString(tcObjList.get(i), true, false);  
    }  
    return marshaArr;  
}
 
Example 5
Source File: WordprocessingMLTemplateWriter.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public String writeToString(WordprocessingMLPackage wmlPackage) throws IOException, Docx4JException {
	MainDocumentPart document = wmlPackage.getMainDocumentPart();		
	return XmlUtils.marshaltoString(wmlPackage);
}
 
Example 6
Source File: Docx4j_读取内容控件_S4_Test.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public void printSdtPrContent(SdtPr sdtPr) {  
    StringBuffer sb = new StringBuffer();  
    List<Object> rprList = sdtPr.getRPrOrAliasOrLock();  
    boolean flag=false;  
    for (Object obj : rprList) {  
        if (obj instanceof JAXBElement) {  
            String eName = ((JAXBElement) obj).getName().getLocalPart();  
            // System.out.println("---------=" + eName);  
            // 布尔类型特殊处理  
            if ("temporary".equals(eName)) {  
                sb.append(" 替换后是否删除内容控件:").append("是");  
            } else if ("text".equals(eName)) {  
                // 纯文本是否允许回车特殊处理  
                // CTSdtText判断是否回车代码不准确  
                // if (this.multiLine == null) {  
                // return true;  
                // }  
                flag=true;  
                String textXml = XmlUtils.marshaltoString(obj, true, true);  
                if (textXml.indexOf("w:multiLine") != -1) {  
                    sb.append(" 是否允许回车:").append("是");  
                }  
            }  
            obj = XmlUtils.unwrap(obj);  
            if (obj instanceof Alias) {  
                Alias alias = (Alias) obj;  
                if (alias != null) {  
                    sb.append(" 标题:").append(alias.getVal());  
                }  
            } else if (obj instanceof CTLock) {  
                CTLock lock = (CTLock) obj;  
                if (lock != null) {  
                    if (lock.getVal().value().toUpperCase().equals("CONTENTLOCKED")) {  
                        sb.append(" 锁定方式:").append("无法编辑内容");  
                    } else if (lock.getVal().value().toUpperCase().equals("SDTLOCKED")) {  
                        sb.append(" 锁定方式:").append("无法删除内容控件");  
                    }else if (lock.getVal().value().toUpperCase().equals("SDTCONTENTLOCKED")) {  
                        sb.append(" 锁定方式:").append("无法删除内容控件,无法编辑内容");  
                    } else {  
                        sb.append(" 锁定方式:").append(lock.getVal());  
                    }  
                }  
            }else if(obj instanceof RPr){  
                RPr rpr = (RPr) obj;  
                if(rpr!=null){  
                    RStyle rprStyle = rpr.getRStyle();  
                    if(rprStyle!=null){  
                        sb.append(" 样式名称:").append(rprStyle.getVal());  
                    }  
                }  
            }  
        } else if (obj instanceof Tag) {  
            Tag tag = (Tag) obj;  
            if (tag != null) {  
                sb.append(" tag标记:").append(tag.getVal());  
            }  
        } else if (obj instanceof Id) {  
            Id id = (Id) obj;  
            if (id != null) {  
                sb.append(" id:").append(id.getVal());  
            }  
        }  
    }  
    if(flag){  
        sb.append(" 内容控件类型:").append("纯文本");  
    }else{  
        sb.append(" 内容控件类型:").append("格式文本");  
    }  
    System.out.println(sb.toString());  
}