Npoi XWPF Word 导出时插入图片无法显示

摘要:
XWPFRun在npoi AddPicture中,各种尝试,各种源代码,都无法显示插入的图片。使用RAR程序打开word以查看文档。xml文件,并提示xml文件错误。在互联网上找到javapoi的解决方案,并定制pic元素。intEMU=9525;宽度*=动车组;高度*=动车组;varrun=doc.CreateParagraph()。CreateRun();CT_英利
  1. npoi中XWPFRun.AddPicture,各种尝试,各种看源代码,也无法将插入的图片显示出来,用RAR程序打开word查看Document.xml文件,提示xml文件错误.在网上找到java的poi的解决办法,自定义Pic元素.
        int EMU = 9525;
                width *= EMU;
                height *= EMU;
    
                var run = doc.CreateParagraph().CreateRun();
                CT_Inline inline = run.GetCTR().AddNewDrawing().AddNewInline();
    
                String picXml = ""
                    //+ "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">"
                    //+ "   <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
                        + "      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">"
                        + "         <pic:nvPicPr>" + "            <pic:cNvPr id=\""
                        + "0"
                        + "\" name=\"Generated\"/>"
                        + "            <pic:cNvPicPr/>"
                        + "         </pic:nvPicPr>"
                        + "         <pic:blipFill>"
                        + "            <a:blip r:embed=\""
                        + id
                        + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>"
                        + "            <a:stretch>"
                        + "               <a:fillRect/>"
                        + "            </a:stretch>"
                        + "         </pic:blipFill>"
                        + "         <pic:spPr>"
                        + "            <a:xfrm>"
                        + "               <a:off x=\"0\" y=\"0\"/>"
                        + "               <a:ext cx=\""
                        + width
                        + "\" cy=\""
                        + height
                        + "\"/>"
                        + "            </a:xfrm>"
                        + "            <a:prstGeom prst=\"rect\">"
                        + "               <a:avLst/>"
                        + "            </a:prstGeom>"
                        + "         </pic:spPr>"
                        + "      </pic:pic>";
                //+ "   </a:graphicData>" + "</a:graphic>";
    
                CT_GraphicalObjectData graphicData = inline.graphic.AddNewGraphicData();
                graphicData.uri = "http://schemas.openxmlformats.org/drawingml/2006/picture";
    
                XmlDocument xmlDoc = new XmlDocument();
                try {
                    xmlDoc.LoadXml(picXml);
                    var element = xmlDoc.DocumentElement;
                    graphicData.AddPicElement(element);
    
                } catch (XmlException xe) {
                }
    
                CT_PositiveSize2D extent = inline.AddNewExtent();
                extent.cx = width;
                extent.cy = height;
    
                CT_NonVisualDrawingProps docPr = inline.AddNewDocPr();
                docPr.id = 1;
                docPr.name = "图片" + id;
  2. 需要强调的是:

      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">"

    这行代码中需要把xmlns:a定义出来,因为标签中含有a的使用,需要定义这个命名空间的本地名称.否则在创建元素时会抛出异常.

  3. 下边的这行代码也是必不可少的.
  4.     graphicData.uri = "http://schemas.openxmlformats.org/drawingml/2006/picture";
  5. 源代码中的方法,不知道为什么创建PIC标签的内容的代码,都没能序列化出来.可能是我还没有找到正确的使用方法,或者是不够完善吧.

测试的源代码CS文件:

Npoi XWPF Word 导出时插入图片无法显示第1张Npoi XWPF Word 导出时插入图片无法显示第2张
    public class XWPFInsertPicture
    {
        public void WordIndertPicTest()
        {
            var wordDoc = new XWPFDocument();
            var picAbsolutePath = @"D:\Test.png";
            if (File.Exists(picAbsolutePath)) {
                var picID = wordDoc.AddPictureData(new FileStream(picAbsolutePath, FileMode.Open), (int)PictureType.PNG);
                CreatePicture(wordDoc, picID, 100, 100);
            }

            var outputPath = Path.Combine(@"D:\", Guid.NewGuid().ToString() + ".docx");
            var writeStream = new FileStream(outputPath, FileMode.Create);
            wordDoc.Write(writeStream);
            writeStream.Close();
        }

        public static void CreatePicture(XWPFDocument doc, string id, int width, int height)
        {
            int EMU = 9525;
            width *= EMU;
            height *= EMU;

            var run = doc.CreateParagraph().CreateRun();
            CT_Inline inline = run.GetCTR().AddNewDrawing().AddNewInline();

            String picXml = ""
                //+ "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">"
                //+ "   <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
                    + "      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">"
                    + "         <pic:nvPicPr>" + "            <pic:cNvPr id=\""
                    + "0"
                    + "\" name=\"Generated\"/>"
                    + "            <pic:cNvPicPr/>"
                    + "         </pic:nvPicPr>"
                    + "         <pic:blipFill>"
                    + "            <a:blip r:embed=\""
                    + id
                    + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>"
                    + "            <a:stretch>"
                    + "               <a:fillRect/>"
                    + "            </a:stretch>"
                    + "         </pic:blipFill>"
                    + "         <pic:spPr>"
                    + "            <a:xfrm>"
                    + "               <a:off x=\"0\" y=\"0\"/>"
                    + "               <a:ext cx=\""
                    + width
                    + "\" cy=\""
                    + height
                    + "\"/>"
                    + "            </a:xfrm>"
                    + "            <a:prstGeom prst=\"rect\">"
                    + "               <a:avLst/>"
                    + "            </a:prstGeom>"
                    + "         </pic:spPr>"
                    + "      </pic:pic>";
            //+ "   </a:graphicData>" + "</a:graphic>";

            CT_GraphicalObjectData graphicData = inline.graphic.AddNewGraphicData();
            graphicData.uri = "http://schemas.openxmlformats.org/drawingml/2006/picture";

            XmlDocument xmlDoc = new XmlDocument();
            try {
                xmlDoc.LoadXml(picXml);
                var element = xmlDoc.DocumentElement;
                graphicData.AddPicElement(element);

            } catch (XmlException xe) {
            }

            CT_PositiveSize2D extent = inline.AddNewExtent();
            extent.cx = width;
            extent.cy = height;

            CT_NonVisualDrawingProps docPr = inline.AddNewDocPr();
            docPr.id = 1;
            docPr.name = "图片" + id;
        }
    }
View Code

免责声明:文章转载自《Npoi XWPF Word 导出时插入图片无法显示》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇spring--集合注入(常规方法)关于谷歌不然安装除商店之外的拓展应用解决下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

NPOI 自定义单元格背景颜色-Excel

NPOI针对office2003使用HSSFWorkbook,对于offce2007及以上使用XSSFWorkbook;今天我以HSSFWorkbook自定义颜色为例说明,Office2007的未研究呢 在NPOI中默认的颜色类是HSSFColor,它内置的颜色有几十种供我们选择,如果不够怎么办,不能修改底层的HSSFColor类; 大概解决思路: 1、将...

NPOI操作Excel简单示例

     根据网上的资料,学习了一下NPOI操作Excel的基本方法:    1、导出至Excel:标题行合并居中、设置列宽、写入列标题及数据。     public class ExportToExcel : IHttpHandler //一般处理程序ExportToExcel.ashx    {        public void ProcessRe...

通过NPOI操作Excel

最近在做的一个项目中需要生成Excel,通过学习使用NPOI实现了相关需求,写了一个简便操作的类,记录如下: public class NPOIHelperForExcel { #region excel文件属性 //作者 public string Author { get; set; }...

SVG初识

SVG 意为可缩放矢量图形(Scalable Vector Graphics) 个人认为现在svg可能有点过时了,svg的很多功能css3或者canvas都能做到很好的效果, 但是刚刚研究了一下还是看到了一些很实在的写法。 优势: SVG 图像可通过文本编辑器来创建和修改 SVG 图像可被搜索、索引、脚本化或压缩 SVG 是可伸缩的 SVG 图像可在任何...

【HTML5版】导出Table数据并保存为Excel

首发我的博客 http://blog.meathill.com/tech/js/export-table-data-into-a-excel-file.html 最近接到这么个需求,要把<table>显示的数据导出成Excel表。类似的需求并不稀罕,过去我通常用PHP输出.csv文件,不过这次似乎不能这么做:数据源表格允许用户筛选和排序,与原...

照片上传(缩略图实现)

1.获取所有的提交到服务器的文件集合 HttpFileCollection fileColl= Request.Files; 2.取得一个文件(这里是一张照片)     HttpPostedFile pic = fileColl[0]; 3.判断文件是否为空     1.获取服务器存放图片的物理路径(Server.MapPath)        strin...