svn2github / npoi

This is a clone of an SVN repository at http://npoi.googlecode.com/svn/trunk/. It had been cloned by http://svn2github.com/ , but the service was since closed. Please read a closing note on my blog post: http://piotr.gabryjeluk.pl/blog:closing-svn2github . If you want to continue synchronizing this repo, look at https://github.com/gabrys/svn2github
Other
60 stars 50 forks source link

NPOI版本2.3.0生成的带图片的docx,Word打开时报错,但2.1.3版本则可正常打开 #24

Open tomtdev opened 6 years ago

tomtdev commented 6 years ago

使用NPOI版本2.3.0生成的带图片的docx,Word打开时报错。但切换回旧版本2.1.3版本则可以正常打开,测试代码:

        XWPFDocument docx = new XWPFDocument();
        using (FileStream picFile = new FileStream("1.png", FileMode.Open, FileAccess.Read))
        {
            docx.CreateParagraph().CreateRun().AddPicture(picFile, (int)PictureType.PNG, "1", (int)(400.0 * 9525), (int)(300.0 * 9525));
        }

        using (FileStream file = File.Create("Image.docx"))
        {
            docx.Write(file);
        }

经过检查,发现用2.3.0版本生成的docx文件中,word/document.xml有问题:

对比2.1.3版本,2.1.3版本则多了id属性: 将id="1"添加到2.3.0版本的 word/document.xml ,打包生成docx,用Word打开正确。 这可能是序列化时产生的bug。临时解决方案: XWPFDocument docx = new XWPFDocument(); XWPFParagraph para = docx.CreateParagraph(); XWPFRun run = para.CreateRun(); run.AddPicture(imageStream, (int)PictureType.PNG, "1.png", width, height); CT_Inline inline = run.GetCTR().GetDrawingList()[0].inline[0]; inline.docPr.id = 1; //id必须从1开始 也就是强制赋予一个Id值以后,问题即可解决。 这样看,是否可在NPOI的代码里修复? 谢谢!
wlclass commented 6 years ago

谢谢,我也碰到过。

tomtdev commented 6 years ago

@wlclass 主项目中已经合并了补丁:https://github.com/tonyqus/npoi