Search in sources :

Example 1 with AlternativeFormatInputPart

use of org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart in project docx4j-template by vindell.

the class HtmlToDOCDemo method convertToWmlObject.

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;
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) AlternativeFormatInputPart(org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart) Document(org.jsoup.nodes.Document) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) Body(org.docx4j.wml.Body) File(java.io.File)

Example 2 with AlternativeFormatInputPart

use of org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart in project docx4j-template by vindell.

the class WMLPackageUtils method insertDocx.

// 插入文档
private static void insertDocx(MainDocumentPart main, byte[] bytes, int chunkId) {
    try {
        AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/part" + chunkId + ".docx"));
        afiPart.setContentType(new ContentType(CONTENT_TYPE));
        afiPart.setBinaryData(bytes);
        Relationship altChunkRel = main.addTargetPart(afiPart);
        CTAltChunk chunk = Context.getWmlObjectFactory().createCTAltChunk();
        chunk.setId(altChunkRel.getId());
        main.addObject(chunk);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : PartName(org.docx4j.openpackaging.parts.PartName) ContentType(org.docx4j.openpackaging.contenttype.ContentType) Relationship(org.docx4j.relationships.Relationship) CTAltChunk(org.docx4j.wml.CTAltChunk) AlternativeFormatInputPart(org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException)

Example 3 with AlternativeFormatInputPart

use of org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart in project docx4j-template by vindell.

the class Docx4jUtils method insertDocx.

// 插入文档
private void insertDocx(MainDocumentPart main, byte[] bytes, int chunkId) {
    try {
        AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/part" + chunkId + ".docx"));
        // afiPart.setContentType(new ContentType(CONTENT_TYPE));
        afiPart.setContentType(new ContentType(ContentTypes.APPLICATION_XML));
        afiPart.setBinaryData(bytes);
        Relationship altChunkRel = main.addTargetPart(afiPart);
        CTAltChunk chunk = Context.getWmlObjectFactory().createCTAltChunk();
        chunk.setId(altChunkRel.getId());
        main.addObject(chunk);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : PartName(org.docx4j.openpackaging.parts.PartName) ContentType(org.docx4j.openpackaging.contenttype.ContentType) Relationship(org.docx4j.relationships.Relationship) CTAltChunk(org.docx4j.wml.CTAltChunk) AlternativeFormatInputPart(org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart) IOException(java.io.IOException) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException)

Aggregations

AlternativeFormatInputPart (org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart)3 IOException (java.io.IOException)2 ContentType (org.docx4j.openpackaging.contenttype.ContentType)2 Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)2 PartName (org.docx4j.openpackaging.parts.PartName)2 Relationship (org.docx4j.relationships.Relationship)2 CTAltChunk (org.docx4j.wml.CTAltChunk)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 JAXBException (javax.xml.bind.JAXBException)1 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)1 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)1 Body (org.docx4j.wml.Body)1 Document (org.jsoup.nodes.Document)1