C# 使用open xml将altChunk转换为段落

C# 使用open xml将altChunk转换为段落,c#,ms-word,openxml,openxml-sdk,C#,Ms Word,Openxml,Openxml Sdk,我已经用OpenXML用C#生成了Word/docx文档。 添加/合并了另一个带有altChunk的docx文档。 当你打开那个文档时,它看起来很棒。但我还需要用C#代码处理该文档。然后,我看到仍然存在一个altChunk,它实际上具有指向合并文档的二进制序列化内容的指针。 如果您使用Word-SaveAs将文档保存到新文件并反映该文档(OpenXML工具或VS扩展名),您将发现Word引擎将altChunk转换为段落。太好了。但我无法通过C代码实现这一点 如果复制到新文件并进行保存,则不会发生

我已经用OpenXML用C#生成了Word/docx文档。 添加/合并了另一个带有altChunk的docx文档。 当你打开那个文档时,它看起来很棒。但我还需要用C#代码处理该文档。然后,我看到仍然存在一个altChunk,它实际上具有指向合并文档的二进制序列化内容的指针。 如果您使用Word-SaveAs将文档保存到新文件并反映该文档(OpenXML工具或VS扩展名),您将发现Word引擎将altChunk转换为段落。太好了。但我无法通过C代码实现这一点

如果复制到新文件并进行保存,则不会发生任何情况:

    public static void SaveAgain(string masterDocumentPath)
    {
        string newDocumentPath = @"F:\test.docx";
        using (Stream original = new FileStream(masterDocumentPath, FileMode.Open))
        {
            using (FileStream fs = File.Create(newDocumentPath))
            {
                original.CopyTo(fs);
            }
        }

        using (WordprocessingDocument myDocNew =
            WordprocessingDocument.Open(newDocumentPath, true))
        {
            myDocNew.MainDocumentPart.Document.Save();
        }
    }
我在构建代码时使用了这个post()。 我错过了什么

更新

我已经通过Interop成功地做到了这一点。以下是片段:

    public static string SaveAsWithInterop(string masterDocumentPath)
    {
        string directoryPath = Path.GetDirectoryName(masterDocumentPath);
        string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(masterDocumentPath);
        string fileExtension = Path.GetExtension(masterDocumentPath);

        string filenNameToSave = string.Concat(fileNameWithoutExtension, "Saved", fileExtension);
        object savedFilePath = Path.Combine(directoryPath, filenNameToSave);

        Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
        object missing = Missing.Value;
        object isReadOnly = false;

        Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(masterDocumentPath);
        doc.SaveAs(ref savedFilePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref isReadOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

        doc.Close();

        return savedFilePath.ToString();
    }

我仍然在想,是否可以用OpenXML实现这一点。我希望避免互操作。

升级投票,因为我也有同样的问题。