NPOI向word中插入图片的Bug解决方案

NPOI包中插入图片,会导致最终生成的word打不开。使用自定义的插入。

 public class XWPFInsertPicture
    {
        public void WordIndertPicTest()
        {
            try
            {
                var wordDoc = new XWPFDocument();
                var picAbsolutePath = @"D:\test.jpg";
                if (File.Exists(picAbsolutePath))
                {
                    var picID = wordDoc.AddPictureData(new FileStream(picAbsolutePath, FileMode.Open), (int)PictureType.JPEG);
                    CreatePicture(wordDoc, picID, 800, 400);
                }

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


            }

        }

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

            string picXml = ""
                    //+ ""
                    //+ "   "
                    + "      "
                    + "         " + "            "
                    + "            "
                    + "         "
                    + "         "
                    + "            "
                    + "            "
                    + "               "
                    + "            "
                    + "         "
                    + "         "
                    + "            "
                    + "               "
                    + "               "
                    + "            "
                    + "            "
                    + "               "
                    + "            "
                    + "         "
                    + "      ";
            //+ "   " + "";
            var run = doc.CreateParagraph().CreateRun();
            CT_Inline inline = run.GetCTR().AddNewDrawing().AddNewInline();

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

            // 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;
                inline.graphic.graphicData.AddPicElement(picXml);

            }
            catch (XmlException xe)
            {

            }

            NPOI.OpenXmlFormats.Dml.WordProcessing.CT_PositiveSize2D extent = inline.AddNewExtent();
            extent.cx = width;
            extent.cy = height;

            NPOI.OpenXmlFormats.Dml.WordProcessing.CT_NonVisualDrawingProps docPr = inline.AddNewDocPr();
            docPr.id = 1;
            docPr.name = "Image" + id;
        }
    }

你可能感兴趣的:(C#.Net)