保存 WordprocessingDocument 后与 ZIP 相关的异常

发布于 2025-01-09 09:32:58 字数 1574 浏览 0 评论 0原文

我正在尝试根据此示例替换 docx 文档中的文本,并进行一些修改:https://learn.microsoft.com/en-us/office/open-xml/how-to-search-and-replace-text-in-a-document-part#sample-code

但是,保存的文档不再有效。 Word 能够更正该文件,但中央目录末尾中预期的条目数与中央目录中的条目数不符。System.IO 中引发异常。当尝试使用 WordprocessingDocument 再次打开创建的文件时, Compression.ZipArchive.ReadCentralDirectory()

我的代码如下所示:

using (var fs = new FileStream(fn, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var ms = new MemoryStream())
{
    await fs.CopyToAsync(ms);
    
    using (var wordDoc = WordprocessingDocument.Open(ms, true))
    {
        string docText;
        using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
        {
            docText = sr.ReadToEnd();
        }
        
        /*Regex regexText = new Regex("text to replace");
        docText = regexText.Replace(docText, "new text");*/

        using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
        {       
            sw.Write(docText);
        }
    }
    
    await File.WriteAllBytesAsync(target, ms.GetBuffer());
}

using (var wordDoc = WordprocessingDocument.Open(target, true))
{
    
}

该问题不会重复到替换本身。即使以任何方式读取 MainDocumentPart 也会导致引发此异常。

为什么是溪流?我想从模板创建和修改文档,然后将其保存到流中。但我没有发现任何 CreateFromTemplate 重载,也没有发现任何接受流的 Save/SaveAs 重载。

I am trying to replace text in a docx document based on this sample, with some modifications: https://learn.microsoft.com/en-us/office/open-xml/how-to-search-and-replace-text-in-a-document-part#sample-code

However, the saved document is not valid anymore. Word is able to correct the file, but there is a Number of entries expected in End Of Central Directory does not correspond to number of entries in Central Directory. exception is thrown at System.IO.Compression.ZipArchive.ReadCentralDirectory() when trying to open the created file again with WordprocessingDocument.

My code looks like this:

using (var fs = new FileStream(fn, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var ms = new MemoryStream())
{
    await fs.CopyToAsync(ms);
    
    using (var wordDoc = WordprocessingDocument.Open(ms, true))
    {
        string docText;
        using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
        {
            docText = sr.ReadToEnd();
        }
        
        /*Regex regexText = new Regex("text to replace");
        docText = regexText.Replace(docText, "new text");*/

        using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
        {       
            sw.Write(docText);
        }
    }
    
    await File.WriteAllBytesAsync(target, ms.GetBuffer());
}

using (var wordDoc = WordprocessingDocument.Open(target, true))
{
    
}

The issue is not repated to the replace itself. Even reading the MainDocumentPart in any way causes this exception to be thrown.

Why the streams? I want to create and modify a document from template and save it afterwards to a stream. But I haven't found any CreateFromTemplate overload neither a Save/SaveAs overload that accepts a stream.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文