[英]Why docx file is corrupted after editing with npoi
我想編輯 .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”時,就會出現問題。 在我的情況下,圖像是文本的背景。 (“文本后面”值)。
嘗試僅使用常規 Stream 和 System.IO.File 讀取文件,然后使用 FileStream 和 System.IO.File 進行保存。
如何:創建文件或文件夾(C# 編程指南) C# (CSharp) NPOI.XWPF.UserModel.XWPFDocument 代碼示例
XWPFDocument doc;
using( Stream fileStream = File.OpenRead("D:\\template.docx") ) {
doc = new XWPFDocument(fileStream);
fileStream.Close();
}
using(FileStream fileStreamNew = File.Create("D:\\test.docx")) {
doc.Write(fileStreamNew);
fileStreamNew.Close();
}
不確定你是否還需要它。 但是我修復了一些與https://github.com/nissl-lab/npoi/pull/510 中的換行文本圖像相關的錯誤,如果您發現某些內容沒有被它捕獲,我將無法捕獲所有情況, 讓我知道。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.