简体   繁体   English

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

[英]OpenXML Merge Word Documents Formatting of multiple files

I have multiple template docx files which I am trying to merge together. 我有多个要合并在一起的docx模板文件。 If the first file is formatted as two columns, when I merge the files it automatically sets the next files as multiple column word document and changes the formatting even if it was a one column file previously before the merge. 如果第一个文件格式化为两列,则在合并文件时,它将自动将下一个文件设置为多列word文档,并更改格式,即使该文件先前是合并前的一列文件也是如此。 However, when i select the normal docx file with no multiple columns first, it tries to set all the multiple column text into a single column and changes the formatting. 但是,当我首先选择没有多列的普通docx文件时,它会尝试将所有多列文本设置为单列并更改格式。

I am trying to merge these documents without any formatting changing. 我正在尝试合并这些文档,而无需更改任何格式。 Heres my code so far: 到目前为止,这是我的代码:

 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);

            }
    }
}

I have added a page break after each file to see if that resets the formatting on merge but doesnt seem to work - can anyone see where i might be going wrong? 我在每个文件后添加了一个分页符,以查看是否在合并时重置了格式但似乎不起作用-有人可以看到我可能要出问题的地方吗?

Have you seen the OpenXML PowerTools? 您看过OpenXML PowerTools吗? They contain code to combine OpenXML documents... 它们包含用于组合OpenXML文档的代码...

DocumentBuilder of PowerTools PowerTools的DocumentBuilder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM