【发布时间】:2020-02-13 22:23:26
【问题描述】:
我有一个完整的.docx,其中包括两个数学问题,docx 嵌入了一些图片和 MathType 方程(oleobject),我根据this 拆分文档,得到两个文件(first.docx,second.docx), first.docx 工作正常,但是 second.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."
点击“是”后,doc可以打开了,内容也正确,我想知道第二个.docx有什么问题?我用“Open xml sdk 2.5 生产力工具”检查了它,但找不到任何原因。非常感谢任何帮助。谢谢。
三个文件已经上传到here。
显示一些代码:
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();
【问题讨论】:
-
您没有提及您如何使用生产力工具。您是否将修复后的文档保存为新名称,关闭它,然后在工具中打开原始(问题)文档并使用比较功能查看更改了什么?
-
@Cindy Meister,谢谢,我对比了 second.docx 和新修复的,发现 /word/_rels/document2.xml.rels 和 /word/_rels/document.xml.rels 之间的区别,在修复的docx中,我发现second.docx中遗漏了一些embeddings/oleObjectx.bin(x为1、2、3 4)(错误的docx),不知道如何在拆分时复制这些oleobjects。
-
@Cindy Meister,在拆分过程中,副本是基于段落的,同时也处理了 Blip 和 ImageData。但是 oleobject 没有特殊处理。我认为 oleobject 包含在 Paragraph 中。
-
我不能为你回答这个问题,至少不能以这种格式回答。首先:查看 Prod.Tool 生成的用于从一开始就创建修复版本的 代码 - 这应该会为您提供一些线索。如果这没有帮助,我建议您使用问题下方的edit 链接将问题更改为真正的问题(您的代码未复制 OLE 对象)...
-
... 保留背景信息(根据“链接”拆分文档)。然后问题第二个文档无效,因为 xyz (提供一些细节)没有被正确复制。包括“坏”和“修复”文档的相关 Word Open XML。还包括您根据生产力工具生成的代码尝试解决问题并描述它如何没有产生正确的结果。
标签: ms-word openxml openxml-sdk