将 2x DOCX 文件合并为 1 个?

发布于 2024-07-12 10:30:15 字数 247 浏览 6 评论 0原文

我需要以编程方式合并一大堆 docx 文件。 想象一下它们的编号为 1 到 10。我本质上需要一个包含所有 10 个文档的 Final.docx 文件。 如果我可以以某种方式将第二个附加到第一个,那么我可以对第三个、第四个等重复它。

请注意,我不需要重建目录或类似的内容。 每个 docx 中都只有一些静态内容。 然而,这些文档上有页脚,需要保留它们。

最好是 C# 或 MSBuild 的答案。

感谢您。

I need to merge a whole bunch of docx files programatically. Imagine they are numbered 1 to 10. I essentially need a final.docx file that contains all 10 documents in it.
If I can append the second to the first somehow, then I can repeat it for the third, then fourth, etc.

Note that I do NOT need to rebuild a table of contents or anything like that. Each docx just has some static content in it. However there ARE footers on these documents, and they need to be preserved.

Preferably an answer in C# or MSBuild.

Thanking you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

篱下浅笙歌 2024-07-19 10:30:15

我用 C# 创建了一个应用程序,将 RTF 文件合并到一个文档中,我希望它也适用于 DOC 和 DOCX 文件。

        Word._Application wordApp;
        Word._Document wordDoc;
        object outputFile = outputFileName;
        object missing = System.Type.Missing;
        object vk_false = false;
        object defaultTemplate = defaultWordDocumentTemplate;
        object pageBreak = Word.WdBreakType.wdPageBreak;
        string[] filesToMerge = new string[pageCounter];
        filestoDelete = new string[pageCounter];

        for (int i = 0; i < pageCounter; i++)
        {
            filesToMerge[i] = @"C:\temp\temp" + i.ToString() + ".rtf";
            filestoDelete[i] = @"C:\temp\temp" + i.ToString() + ".rtf";                
        }
        try
        {
            wordDoc = wordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Word.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);  

希望这可以帮助!

I had made an application in C# to merge RTF files into one doc,Iam hopeful it should work for DOC and DOCX files as well.

        Word._Application wordApp;
        Word._Document wordDoc;
        object outputFile = outputFileName;
        object missing = System.Type.Missing;
        object vk_false = false;
        object defaultTemplate = defaultWordDocumentTemplate;
        object pageBreak = Word.WdBreakType.wdPageBreak;
        string[] filesToMerge = new string[pageCounter];
        filestoDelete = new string[pageCounter];

        for (int i = 0; i < pageCounter; i++)
        {
            filesToMerge[i] = @"C:\temp\temp" + i.ToString() + ".rtf";
            filestoDelete[i] = @"C:\temp\temp" + i.ToString() + ".rtf";                
        }
        try
        {
            wordDoc = wordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Word.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);  

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文