poi-tl
poi-tl copied to clipboard
不同页眉页脚合并文档
private final Configure configure = Configure.newBuilder()
.addPlugin('\0', new CustomRenderPolicy())
.addPlugin('%', new CustomRenderPolicy()).addPlugin('@', new CustomRenderPolicy())
.addPlugin('#', new CustomRenderPolicy()).addPlugin('+', new CustomRenderPolicy())
.build();
public class CustomRenderPolicy extends AbstractRenderPolicy<SiteRenderData> {
@Override
public void doRender(RenderContext<SiteRenderData> context) throws Exception {
SiteRenderData renderData = context.getData();
ExportFieldEnum.FieldFormat fieldFormat = renderData.getFieldFormat();
Object value = renderData.getValue();
fieldFormat.getRenderPolicy().render(context.getEleTemplate(), value, context.getTemplate());
}
}
CustomRenderPolicy 中又调用renderPolicy.render方法,具体策略包装在SiteRenderData中。
public class CustomAttachmentRenderPolicy extends DocxRenderPolicy {
@Override
public void render(ElementTemplate eleTemplate, Object data, XWPFTemplate template) {
if (null == data) {
super.render(eleTemplate, null, template);
return;
}
String path = data.toString();
File file = new File(path);
DocxRenderData docxRenderData = new DocxRenderData(file);
super.render(eleTemplate, docxRenderData, template);
}
}
现在合并占位符是第一个,后续是文本或者表格类型。 当第一个合并文档DocxRenderPolicy 策略后,reload doc后,后续执行普通的文本渲染就会抛异常 xmlbeans-3.1.0
protected final void check_orphaned()
{
if (is_orphaned())
throw new XmlValueDisconnectedException();
}
当合并占位符是最后一个,就没问题。 当合并占位符是第一个时,合并完reload后,文档结构发生变化,导致后续填充数据出错? 需要帮助
当前合并算法的原因需要reload,所以要先调用其他策略,最后调用DocxRenderPolicy。
后续考虑改变合并算法,这样就可以满足你的场景。
可以参考一下源码当中渲染的代码,docx都是最后调用的。
看了源码,明白了,是最后调用的 一个模版和一个待合并的文档,都有水印logo,但merge合并后水印没有了。 单独填充模版没问题
水印是以模板为主的,模板有文字或者图片水印,合并后应该是有水印的。我没重现出你的问题。
@Test
public void testMerge() throws Exception {
Map<String, Object> datas = new HashMap<String, Object>();
datas.put("content", new DocxRenderData(new File("/Users/zhujiajun/Downloads/export-test/test/fileplan.docx")));
datas.put("projectName", "project for test");
XWPFTemplate template = XWPFTemplate.compile("/Users/zhujiajun/Downloads/export-test/test/999.docx").render(datas);
FileOutputStream out = new FileOutputStream("/Users/zhujiajun/Downloads/export-test/test/poi-tl-render.docx");
template.write(out);
out.flush();
out.close();
template.close();
}
导出后,第一页无水印和页眉页脚,请看下
你的水印其实是页眉页脚里面的图片,页眉页脚的合并可能会有点麻烦了。