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

  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 = ""
                    //+ "http://schemas.openxmlformats.org/drawingml/2006/main\">"
                    //+ "   http://schemas.openxmlformats.org/drawingml/2006/picture\">"
                        + "      "
                        + "         " + "            "
                        + "0"
                        + "\" name=\"Generated\"/>"
                        + "            "
                        + "         "
                        + "         "
                        + "            "
                        + id
                        + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>"
                        + "            "
                        + "               "
                        + "            "
                        + "         "
                        + "         "
                        + "            "
                        + "               "
                        + "               "
                        + width
                        + "\" cy=\""
                        + height
                        + "\"/>"
                        + "            "
                        + "            "
                        + "               "
                        + "            "
                        + "         "
                        + "      ";
                //+ "   " + "";
    
                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. 需要强调的是:

      "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文件:

    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 = ""
                //+ "http://schemas.openxmlformats.org/drawingml/2006/main\">"
                //+ "   http://schemas.openxmlformats.org/drawingml/2006/picture\">"
                    + "      "
                    + "         " + "            "
                    + "0"
                    + "\" name=\"Generated\"/>"
                    + "            "
                    + "         "
                    + "         "
                    + "            "
                    + id
                    + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>"
                    + "            "
                    + "               "
                    + "            "
                    + "         "
                    + "         "
                    + "            "
                    + "               "
                    + "               "
                    + width
                    + "\" cy=\""
                    + height
                    + "\"/>"
                    + "            "
                    + "            "
                    + "               "
                    + "            "
                    + "         "
                    + "      ";
            //+ "   " + "";

            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

 

转载于:https://www.cnblogs.com/zhangliming/archive/2013/04/02/2995655.html

你可能感兴趣的:(Npoi XWPF Word 导出时插入图片无法显示)