docx4j尝试将模板应用于文档

ars1skjm  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(340)

我试图将模板的页眉、页脚和样式从dotx附加到docx文档,但是docx4j只应用样式而忽略页眉和页脚。我做错什么了吗?有办法吗?

ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            JAXBContext jc = JAXBContext.newInstance(bindClass);
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            StringWriter writer = new StringWriter();
            marshaller.marshal(bindingInstance, writer);
            String inputXML = writer.toString();
            System.out.println(inputXML);
            // Load input_template

            WordprocessingMLPackage wordMLPackage = Docx4J.load(getDocx("doc.docx"));

            wordMLPackage.attachTemplate(getDotx("template.dotx").getAbsolutePath());

            Docx4J.bind(wordMLPackage, inputXML, Docx4J.FLAG_BIND_BIND_XML | Docx4J.FLAG_BIND_INSERT_XML | Docx4J.FLAG_BIND_REMOVE_XML);

            Docx4J.save(wordMLPackage, bos, Docx4J.FLAG_BIND_BIND_XML | Docx4J.FLAG_BIND_INSERT_XML | Docx4J.FLAG_BIND_REMOVE_XML);

            System.out.println("DocumentPrintHandler.bindData() ==>" + wordMLPackage.getDocPropsExtendedPart().getContents().getPages()); //$NON-NLS-1$
        } catch (JAXBException e) {
            Logging.i("DocumentPrintHandler.bindData() (JAXBException)", e); //$NON-NLS-1$
            return null;
        } catch (Docx4JException e) {
            Logging.i("DocumentPrintHandler.bindData() (Docx4JException)", e); //$NON-NLS-1$
            return null;
        } catch (Exception e) {
            Logging.i("DocumentPrintHandler.bindData() (Exception)", e); //$NON-NLS-1$
            return null;
        }
        return bos.toByteArray();
    }```

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题