簡體   English   中英

使用 openxml 拆分 docx 后,Word 在 xxx.docx 中發現不可讀的內容

[英]Word found unreadable content in xxx.docx after split a docx using openxml

我有一個 full.docx,其中包括兩個數學問題,docx 嵌入了一些圖片和 MathType 方程(oleobject),我根據這個拆分了 doc,得到兩個文件(first.docx,second.docx),first.docx 工作正常,但是,當我嘗試打開它時,第二個.docx 會彈出一個警告對話框:

"Word found unreadable content in second.docx. Do you want to recover the contents of this document? If you trust the source of this document, click Yes."

點擊“是”后,可以打開文檔,內容也正確,我想知道第二個.docx有什么問題? 我已經用“Open xml sdk 2.5生產力工具”檢查過,但沒有找到原因。 非常感謝您的幫助。 謝謝。

這三個文件已經上傳到這里

顯示一些代碼:

        byte[] templateBytes = System.IO.File.ReadAllBytes(TEMPLATE_YANG_FILE);
        using (MemoryStream templateStream = new MemoryStream())
        {
            templateStream.Write(templateBytes, 0, (int)templateBytes.Length);

            string guidStr = Guid.NewGuid().ToString();

            using (WordprocessingDocument document = WordprocessingDocument.Open(templateStream, true))
            {
                document.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);

                MainDocumentPart mainPart = document.MainDocumentPart;

                mainPart.Document = new Document();
                Body bd = new Body();

                foreach (DocumentFormat.OpenXml.Wordprocessing.Paragraph clonedParagrph in lst)
                {
                    bd.AppendChild<DocumentFormat.OpenXml.Wordprocessing.Paragraph>(clonedParagrph);

                    clonedParagrph.Descendants<Blip>().ToList().ForEach(blip =>
                    {
                        var newRelation = document.CopyImage(blip.Embed, this.wordDocument);
                        blip.Embed = newRelation;
                    });

                    clonedParagrph.Descendants<DocumentFormat.OpenXml.Vml.ImageData>().ToList().ForEach(imageData =>
                    {
                        var newRelation = document.CopyImage(imageData.RelationshipId, this.wordDocument);
                        imageData.RelationshipId = newRelation;
                    });
                }

                mainPart.Document.Body = bd;
                mainPart.Document.Save();
            }

            string subDocFile = System.IO.Path.Combine(this.outDir, guidStr + ".docx");
            this.subWordFileLst.Add(subDocFile);

            File.WriteAllBytes(subDocFile, templateStream.ToArray());
        }

lst 包含從原始 docx 克隆的段落,使用:

(DocumentFormat.OpenXml.Wordprocessing.Paragraph)p.Clone();

使用生產力工具,發現oleobjectx.bin沒有復制,所以我在復制Blip和ImageData后添加以下代碼:

clonedParagrph.Descendants<OleObject>().ToList().ForEach(ole =>
{
    var newRelation = document.CopyOleObject(ole.Id, this.wordDocument);
    ole.Id = newRelation;
});

解決了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM