zoukankan      html  css  js  c++  java
  • 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 = ""
                      //+ "<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文件:

        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
  • 相关阅读:
    Python基础-socketserver
    MySQL数据库-pymysql模块操作数据库
    MySQL数据库-外键链表之一对多,多对多
    MySQL数据库-表内容操作
    第02组 Alpha冲刺(5/6)
    第02组 Alpha冲刺(4/6)
    第02组 Alpha冲刺(3/6)
    第02组 Alpha冲刺(2/6)
    第02组 Alpha冲刺(1/6)
    第02组 团队Git现场编程实战
  • 原文地址:https://www.cnblogs.com/zhangliming/p/2995655.html
Copyright © 2011-2022 走看看