首页 > 解决方案 > 为什么用 npoi 编辑后 docx 文件损坏

问题描述

我想编辑 .docx 文件并使用 npoi 库。

是一个片段:

XWPFDocument doc;
using( FileStream fileStream = new FileStream(@"D:\\template.docx", FileMode.Open, FileAccess.Read) ) {
doc = new XWPFDocument(fileStream);
    fileStream.Close();
}

// here can change doc or do nothing

using(FileStream fileStreamNew = new FileStream(@"D:\\test.docx", FileMode.CreateNew)) {
    doc.Write(fileStreamNew);
    fileStreamNew.Close();
}

但我得到了损坏的文件。当我尝试打开文件时,显示模式窗口:“我们很抱歉。我们无法打开 test.docx,我们发现其内容有问题。详细信息:未指定的错误。位置:/word/header1.xml,行:0 , 栏目: 0"

先感谢您

==================================================== ==========

更新:

问题出在模板文件中。如果我从页眉和页脚中删除图像,那么错误就会消失。但是,如果我将模板图像放回错误返回。有任何想法吗?

==================================================== ===========

更新:

当图像属性“Wrap Text”的值不是“In Line With Text”时,就会出现此问题。在我的情况下,图像是文本的背景。(“文本后面”值)。

标签: c#docxnpoi

解决方案


不确定你是否还需要它。 但是我修复了其中一些错误,这些错误与https://github.com/nissl-lab/npoi/pull/510中的换行文本图像有关, 让我知道。


推荐阅读