简体   繁体   English

将多个docx文件合并为一个

[英]Merging multiple docx files to one

I am developing a desktop application in C#. 我正在用C#开发桌面应用程序。 I have coded a function to merge multiple docx files but it does not work as expected. 我已经编码了一个函数来合并多个docx文件,但是它没有按预期工作。 I don't get the content exactly as how it was in the source files. 我得到的内容与源文件中的不完全一样。

A few blank lines are added in between. 在它们之间添加一些空白行。 The content extends to the next pages, header and footer information is lost, page margins gets changed, etc.. How can I concatenate docs as it is without and change in it.Any suggestions will be helpful. 内容扩展到下一页,页眉和页脚信息丢失,页边距被更改等。如何在没有文档的情况下连接文档并进行更改。任何建议都将有所帮助。

This is my code. 这是我的代码。

    public bool CombineDocx(string[] filesToMerge, string destFilepath)
    {
        Application wordApp = null;
        Document wordDoc = null;
        object outputFile = destFilepath;
        object missing = Type.Missing;
        object pageBreak = WdBreakType.wdPageBreak;

        try
        {
            wordApp = new Application { DisplayAlerts = WdAlertLevel.wdAlertsNone, Visible = false };
            wordDoc = wordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);

            Selection selection = wordApp.Selection;

            foreach (string file in filesToMerge)
            {
                selection.InsertFile(file, ref missing, ref missing, ref missing, ref missing);

                selection.InsertBreak(ref pageBreak);
            }

            wordDoc.SaveAs( ref outputFile, ref missing, ref missing, ref missing, 
                ref missing, ref missing, ref missing, ref missing, 
                ref missing, ref missing, ref missing, ref missing, 
                ref missing, ref missing, ref missing, ref missing);

            return true;
        }
        catch (Exception ex)
        {
            Msg.Log(ex);
            return false;
        }
        finally
        {
            if (wordDoc != null)
            {
                wordDoc.Close();
            }

            if (wordApp != null)
            {
                wordApp.DisplayAlerts = WdAlertLevel.wdAlertsAll;
                wordApp.Quit();
                Marshal.FinalReleaseComObject(wordApp);
            }
        }
    }

In my opinion it's not so easy. 我认为这不是那么容易。 Therefore I'll give you some tips here. 因此,我在这里给您一些提示。 I think you need to implement the following changes to your code. 我认为您需要对代码进行以下更改。

1.instead of pageBreak you need to add any of section breaks which could be the most appropriate: 的1.instead pageBreak ,你需要添加任何分节符的可能是最合适的:

object sectionBrak = WdBreakType.wdSectionBreakNextPage;
'other section break types also available

and use this new variable within your loop. 并在循环中使用此新变量。 As a result you get all- text, footers and headers of the source document to new one. 结果,您将源文档的所有文本,页脚和页眉都替换为新的。

2.However, you will still need to read margin parameters and apply them to your new document 'manually' using additional code. 2.但是,您仍然需要阅读边距参数,并使用其他代码将其“手动”应用于新文档。 Therefore you will need to open source document and check it's margins in this way: 因此,您将需要打开源文档并以这种方式检查其边距:

intLM = SourceDocument.Sections(1).PageSetup.LeftMargin;
'...and similar for other margins

and next you need to apply it to new document, to appropriate section: 接下来,您需要将其应用于新文档的相应部分:

selection.Sections(1).PageSetup.LeftMargin = intLM;

3.Some other document section could require some other techniques. 3.其他一些文档部分可能需要其他技术。

You could use the Open XML SDK and the DocumentBuilder tool. 您可以使用Open XML SDK和DocumentBuilder工具。

See Merge multiple word documents into one Open Xml 请参阅将多个Word文档合并到一个Open Xml中

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

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