C# OpenXML合并多个文件的Word文档格式

C# OpenXML合并多个文件的Word文档格式,c#,asp.net,openxml,C#,Asp.net,Openxml,我有多个模板docx文件,我正试图合并在一起。如果第一个文件的格式为两列,则当我合并这些文件时,它会自动将下一个文件设置为多列word文档,并更改格式,即使在合并之前它是一列文件。但是,当我首先选择没有多列的普通docx文件时,它会尝试将所有多列文本设置为一列,并更改格式 我正在尝试合并这些文档而不更改任何格式。以下是我目前的代码: private void MergeDocuments() { bool docfilechecked = keepworddoc.Checked;

我有多个模板docx文件,我正试图合并在一起。如果第一个文件的格式为两列,则当我合并这些文件时,它会自动将下一个文件设置为多列word文档,并更改格式,即使在合并之前它是一列文件。但是,当我首先选择没有多列的普通docx文件时,它会尝试将所有多列文本设置为一列,并更改格式

我正在尝试合并这些文档而不更改任何格式。以下是我目前的代码:

 private void MergeDocuments()
{

    bool docfilechecked = keepworddoc.Checked;
    string caseno = casetextboxinput.Value;
    string nameno = patentee;

    string[] mergeFiles = Directory.GetFiles(directoryrootmerge + caseno + @"\", sequenceCounter + "*.doc*")
       .Select(Path.GetFullPath)
       .ToArray();

    string filename = caseno + @" - " + nameno + " - PoA Form.docx";
    string directory = directoryrootmerge + caseno;
    string outputFileName = directoryrootmerge + caseno + @"\" + caseno + @" - " + nameno + " - PoA Form.docx";
    //string outputFileName2 = directoryrootmerge + caseno + @"\" + caseno + @" - " + nameno + "- PoA Form.pdf";


    if (mergeFiles.Length == 1)
    {
        System.IO.File.Copy(mergeFiles[0], outputFileName, true);
    }
    else
    {
        for (int i = 1; i < mergeFiles.Length; i++)
            using (WordprocessingDocument myDoc = WordprocessingDocument.Open(mergeFiles[0], true))
            {
                MainDocumentPart mainPart = myDoc.MainDocumentPart;
                string altChunkId = "AltChunkId" + i;

                //Append page break
                Paragraph para = new Paragraph(new Run((new DocumentFormat.OpenXml.Wordprocessing.Break() { Type = BreakValues.Page })));
                mainPart.Document.Body.InsertAfter(para, mainPart.Document.Body.LastChild);

                AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
                    AlternativeFormatImportPartType.WordprocessingML, altChunkId);
                using (FileStream fileStream = System.IO.File.Open(mergeFiles[i], FileMode.Open))
                {
                    chunk.FeedData(fileStream);
                 }

                AltChunk altChunk = new AltChunk();
                altChunk.Id = altChunkId;
                //new page, if you like it...
                //mainPart.Document.Body.AppendChild(new Paragraph(new Run(new DocumentFormat.OpenXml.Wordprocessing.Break() { Type = BreakValues.Page })));
                mainPart.Document.Body.InsertAfter(altChunk, mainPart.Document.Body.Elements<DocumentFormat.OpenXml.Wordprocessing.Paragraph>().Last());
                mainPart.Document.Save();
                myDoc.Close();
                System.IO.File.Copy(mergeFiles[0], outputFileName, true);

            }
    }
}
private void MergeDocuments()
{
bool docfilechecked=keepworddoc.Checked;
字符串caseno=casetextboxinput.Value;
字符串nameno=专利权人;
string[]mergeFiles=Directory.GetFiles(directoryrootmerge+caseno+@“\”,sequenceCounter+“*.doc*”)
.Select(Path.GetFullPath)
.ToArray();
字符串文件名=caseno+@“-”+nameno+“-PoA Form.docx”;
string directory=directoryrootmerge+caseno;
字符串outputFileName=directoryrootmerge+caseno+@“\”+caseno+@“-”+nameno+“-PoA Form.docx”;
//字符串outputFileName2=directoryrootmerge+caseno+@“\”+caseno+@“-”+nameno+“-PoA Form.pdf”;
如果(mergeFiles.Length==1)
{
System.IO.File.Copy(合并文件[0],输出文件名,true);
}
其他的
{
对于(int i=1;i

我在每个文件后都添加了一个分页符,以查看这是否会重置合并时的格式,但似乎不起作用-有人能看到我可能出错的地方吗?

您看过OpenXML PowerTools吗?它们包含组合OpenXML文档的代码


我做了一些研究,并建议使用名为match source的alt chunk属性,但不确定如何使用它-有人能帮忙吗?MatchSource matchSrc=new MatchSource();matchSrc.Val=DocumentFormat.OpenXml.Wordprocessing.BooleanValues.True;altChunk.AppendChild(matchSrc);命名空间“DocumentFormat.OpenXml.Wordprocessing”中不存在类型或命名空间名称“BooleanValues”(是否缺少程序集引用?