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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM