Apache POI实现含内网图片的HTML作为altChunk嵌入DOCX并本地化图片至media文件夹的可行性及相关文档查询
Embed LAN Images into DOCX with Apache POI
Absolutely, this approach works perfectly—you can embed those LAN-hosted images directly into the DOCX file's media folder, and Apache POI handles the relationship references in the _rels folder automatically (no need to manually edit those files!). Here's a step-by-step breakdown of how to implement this, plus where to find relevant Apache POI documentation:
Step 1: Use a Custom Image Provider with HTML Converter
Apache POI's HtmlConverter lets you hook into image processing to replace external LAN image URLs with embedded images. Here's a working code example:
import org.apache.poi.util.Units; import org.apache.poi.xwpf.converter.html.HtmlConverter; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.w3c.dom.Element; import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL; public class LanImageToDocx { public static void main(String[] args) throws Exception { XWPFDocument doc = new XWPFDocument(); // Custom handler to fetch LAN images and embed them HtmlConverter.ImageProvider imageProvider = new HtmlConverter.ImageProvider() { @Override public void processImage(Element imgElement, XWPFRun run) { String lanImageUrl = imgElement.getAttribute("src"); try (InputStream imageStream = new URL(lanImageUrl).openStream()) { // Detect image type (adjust based on your supported formats) int imageType = getImageTypeFromUrl(lanImageUrl); // Embed image: POI auto-adds it to media folder and creates rels run.addPicture(imageStream, imageType, lanImageUrl, Units.toEMU(400), Units.toEMU(300)); // Width/Height in EMU } catch (Exception e) { // Handle errors (e.g., image not accessible) e.printStackTrace(); } } private int getImageTypeFromUrl(String url) { if (url.toLowerCase().endsWith(".png")) return XWPFDocument.PICTURE_TYPE_PNG; if (url.toLowerCase().matches(".+\\.(jpg|jpeg)")) return XWPFDocument.PICTURE_TYPE_JPEG; if (url.toLowerCase().endsWith(".gif")) return XWPFDocument.PICTURE_TYPE_GIF; return XWPFDocument.PICTURE_TYPE_JPEG; // Fallback } }; // Configure converter options with our custom image handler HtmlConverter.HtmlConverterOptions options = HtmlConverter.HtmlConverterOptions.create() .imageProvider(imageProvider); // Your HTML content with LAN image links String htmlContent = "<div><p>Sample text with a LAN image:</p><img src='http://192.168.1.50/images/company-logo.png' /></div>"; // Convert HTML to DOCX HtmlConverter.convertToXWPF(htmlContent, doc, options); // Save the final DOCX try (FileOutputStream out = new FileOutputStream("embedded-images.docx")) { doc.write(out); } } }
Key Notes
- You don't need to manually edit the
mediaor_relsfolders: Apache POI'sXWPFRun.addPicture()method automatically handles writing the image to the media directory and creating the necessary relationship entries in the_relsfolder. - Make sure your application has access to the LAN image URLs (check firewall/network permissions if you run into connection issues).
Where to Find Apache POI Documentation
- XWPFRun API Docs: Look up the
org.apache.poi.xwpf.usermodel.XWPFRunclass to learn more aboutaddPicture()and other run-level operations. - HTML Converter Docs: The
org.apache.poi.xwpf.converter.html.HtmlConverterand its associated interfaces (likeImageProvider) are documented in Apache POI's official API reference. - POI User Guide: The official Apache POI user guide has a section on Working with Images in DOCX that covers embedding, sizing, and relationship management.
内容的提问来源于stack exchange,提问作者marcoF76IT
相关产品推荐
相关产品推荐

