Introduction to poi-tl
poi-tl (poi template language) is a Word template engine that generates new documents based on Microsoft Word templates and data.
Document address:/poi-tl/1./
Because poi-tl does not support setting image surround mode for the time being, but you can customize the image rendering plug-in
Less nonsense, just upload the code
First, customize an image rendering plugin
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import .; import .; import .; import ; import ; import ; import ; /** * Custom image rendering */ public class MyPictureRenderPolicy extends AbstractRenderPolicy<Object> { @Override protected boolean validate(Object data) { if (null == data) { return false; } else if (data instanceof PictureRenderData) { return null != ((PictureRenderData) data).getPictureSupplier(); } else { return true; } } @Override public void doRender(RenderContext<Object> context) throws Exception { ((), wrapper(())); } @Override protected void afterRender(RenderContext<Object> context) { (context, false); } @Override protected void reThrowException(RenderContext<Object> context, Exception e) { ("Render picture " + () + " error: {}", ()); String alt = ""; if (() instanceof PictureRenderData) { alt = ((PictureRenderData) ()).getAltMeta(); } ().setText(alt, 0); } private static PictureRenderData wrapper(Object object) { return object instanceof PictureRenderData ? (PictureRenderData) object : (()).fitSize().create(); } public static class Helper { public static void renderPicture(XWPFRun run, PictureRenderData picture) throws Exception { Supplier<byte[]> supplier = (); byte[] imageBytes = (byte[]) (); if (null == imageBytes) { throw new IllegalStateException("Can't read picture byte arrays!"); } else { PictureType pictureType = (); if (null == pictureType) { pictureType = (imageBytes); } if (null == pictureType) { throw new RenderException("PictureRenderData must set picture type!"); } else { PictureStyle style = (); if (null == style) { style = new PictureStyle(); } int width = (); int height = (); if (pictureType == ) { imageBytes = (imageBytes, (float) width, (float) height); pictureType = ; } if (!isSetSize(style)) { BufferedImage original = (imageBytes); width = (); height = (); if (() == ) { BodyContainer bodyContainer = (((IBodyElement) ()).getBody()); int pageWidth = UnitUtils.twips2Pixel(((IBodyElement) ())); if (width > pageWidth) { double ratio = (double) pageWidth / (double) width; width = pageWidth; height = (int) ((double) height * ratio); } } } InputStream stream = new ByteArrayInputStream(imageBytes); Throwable var25 = null; try { align = (); if (null != align && () instanceof XWPFParagraph) { ((XWPFParagraph) ()).setAlignment((() + 1)); } XWPFRunWrapper wrapper = new XWPFRunWrapper(run, false); (stream, (), "Generated", (width), (height)); CTDrawing drawing = ().getDrawingArray(0); CTGraphicalObject graphicalobject = (0).getGraphic(); //Get the newly inserted image and add CTAnchor. Set floating attributes. Delete the inline attributes. CTAnchor anchor = (graphicalobject, "Generated", (width), (height),//Picture size (270), (-70), false);//Relative to the current paragraph position, the left offset of the existing content of the paragraph needs to be calculated. (new CTAnchor[]{anchor});//Add floating attribute (0);//Delete the in-line attribute } catch (Throwable var20) { var25 = var20; throw var20; } finally { if (stream != null) { if (var25 != null) { try { (); } catch (Throwable var19) { (var19); } } else { (); } } } } } } } private static boolean isSetSize(PictureStyle style) { return (() != 0 || () != 0) && () == ; } }
The core code floating above the text is extracted and placed in the tool class
public class ExportUtil { /** * @param ctGraphicalObject Image Data * @param deskFileName Image description * @param width * @param height * @param leftOffset Horizontal offset left * @param topOffset vertical offset top * @param behind above the text, below the text * @return * @throws Exception */ public static CTAnchor getAnchorWithGraphic(CTGraphicalObject ctGraphicalObject, String deskFileName, int width, int height, int leftOffset, int topOffset, boolean behind) { String anchorXML = "<wp:anchor xmlns:wp=\"/drawingml/2006/wordprocessingDrawing\" " + "simplePos=\"0\" relativeHeight=\"0\" behindDoc=\"" + ((behind) ? 1 : 0) + "\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\">" + "<wp:simplePos x=\"0\" y=\"0\"/>" + "<wp:positionH relativeFrom=\"column\">" + "<wp:posOffset>" + leftOffset + "</wp:posOffset>" + "</wp:positionH>" + "<wp:positionV relativeFrom=\"paragraph\">" + "<wp:posOffset>" + topOffset + "</wp:posOffset>" + "</wp:positionV>" + "<wp:extent cx=\"" + width + "\" cy=\"" + height + "\"/>" + "<wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"0\"/>" + "<wp:wrapNone/>" + "<wp:docPr id=\"1\" name=\"Drawing 0\" descr=\"" + deskFileName + "\"/><wp:cNvGraphicFramePr/>" + "</wp:anchor>"; CTDrawing drawing = null; try { drawing = (anchorXML); } catch (XmlException e) { (); } CTAnchor anchor = (0); (ctGraphicalObject); return anchor; } }
When exporting word using poi-tl
ConfigureBuilder builder = (); ('%',new MyPictureRenderPolicy());//Register plugin as new tag typeXWPFTemplate template = (templateIs,()).render(exportBO);
This will render all image tags in the template with prefixed % as floating above the text
This is the article about Java using poi-tl to set the word image surround method to float above the text. For more related Java poi-tl to set the word image surround method, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!