Java如何替换word文档文字并指定位置插入图片-创新互联
这篇文章主要介绍Java如何替换word文档文字并指定位置插入图片,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联公司主营汨罗网站建设的网络公司,主营网站建设方案,成都app软件开发,汨罗h5重庆小程序开发搭建,汨罗网站营销推广欢迎汨罗等地区企业咨询先说下 需要的依赖包
org.apache.poi poi-excelant 3.12 org.apache.poi poi-scratchpad 3.12 org.apache.poi poi-ooxml 3.8 org.apache.poi poi-ooxml-schemas 3.8 org.jfree jfreechart 1.0.19 org.docx4j docx4j 3.3.1
示例,下图
如上图,需要替换的字符串地方“$1”为“1231”,在指定位置插入书签,并命名“test” ,插入的图片如下
本人也没太过多去研究,亲测通过有效,在这分享下
1.demo
import java.awt.Font; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.poi.POIXMLDocument; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.util.IOUtils; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.docx4j.TraversalUtil; import org.docx4j.dml.wordprocessingDrawing.Inline; import org.docx4j.finders.RangeFinder; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; import org.docx4j.wml.Body; import org.docx4j.wml.BooleanDefaultTrue; import org.docx4j.wml.CTBookmark; import org.docx4j.wml.Color; import org.docx4j.wml.Document; import org.docx4j.wml.Drawing; import org.docx4j.wml.HpsMeasure; import org.docx4j.wml.ObjectFactory; import org.docx4j.wml.P; import org.docx4j.wml.R; import org.docx4j.wml.RPr; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.labels.StandardPieSectionLabelGenerator; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.title.TextTitle; import org.jfree.data.general.DefaultPieDataset;import com.aisino.qysds.common.constant.ERRORConstants; import com.aisino.qysds.common.exception.SysException; import com.aisino.qysds.service.IExportBgService; import com.google.common.collect.Maps; public class ExportBgServiceImpl { public static void main(String[] args) throws Exception { Mapmap = Maps.newHashMap(); map.put("$1", "1231"); XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage("D:\\tp\\test.docx")); Iterator itPara = document.getParagraphsIterator(); while (itPara.hasNext()) { XWPFParagraph paragraph = (XWPFParagraph) itPara.next(); List runs = paragraph.getRuns(); for (int i = 0; i < runs.size(); i++) { String oneparaString = runs.get(i).getText(runs.get(i).getTextPosition()); for (Map.Entry entry : map.entrySet()) { if (oneparaString.equals(entry.getKey())) { oneparaString = oneparaString.replace(entry.getKey(), entry.getValue()); } } runs.get(i).setText(oneparaString, 0); } } FileOutputStream outStream = null; outStream = new FileOutputStream("D:\\tp\\test1.docx"); document.write(outStream); outStream.close(); //-----------------------------------这块为生成图片 和 插入图片 DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("修改类", 1); dataset.setValue("提示类", 1); dataset.setValue("校验不通过", 3); dataset.setValue("正常类", 3); JFreeChart chart = ChartFactory.createPieChart3D(null, dataset, true, false, false); chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15)); // 设置图例类别字体 // TextTitle title = new TextTitle(titleString); // title.setFont(new Font("黑体", Font.ITALIC, 20));//设置标题字体 // chart.setTitle(title); PiePlot piePlot = (PiePlot) chart.getPlot(); DecimalFormat df = new DecimalFormat("0.00%"); NumberFormat nf = NumberFormat.getInstance(); StandardPieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator("{0} {2}", nf, df);// 获得StandardPieSectionLabelGenerator对象,生成的格式, // {0}表示section名,{1}表示section的值,{2}表示百分比。可以自定义 piePlot.setLabelGenerator(generator);// 设置百分比 piePlot.setLabelFont(new Font("黑体", Font.ITALIC, 15));// 设置饼图中类别字体 piePlot.setNoDataMessage("此时并没有任何数据可用"); piePlot.setCircular(false); piePlot.setLabelGap(0.02D); piePlot.setIgnoreNullValues(true);// 设置不显示空位 piePlot.setIgnoreZeroValues(true);// 设置不显示负值或零值 String fName = "pie.png"; File file = new File("D:\\tp", fName); if (file.exists()) { file.delete(); } try { ChartUtilities.saveChartAsPNG(file, chart, 800, 500); File file2 = new File("D:\\tp\\test1.docx"); WordprocessingMLPackage wPackage = WordprocessingMLPackage.load(new FileInputStream(file2)); MainDocumentPart mainDocumentPart = wPackage.getMainDocumentPart(); Document wmlDoc = (Document) mainDocumentPart.getJaxbElement(); Body body = wmlDoc.getBody(); // 提取正文中所有段落 List
最后效果图如下:
以上是“Java如何替换word文档文字并指定位置插入图片”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
分享标题:Java如何替换word文档文字并指定位置插入图片-创新互联
转载来源:http://lswzjz.com/article/cohgoe.html