将 Word 文档合并为单个文档

发布于 2024-10-05 14:17:59 字数 360 浏览 0 评论 0原文

我使用下面提到的链接中的代码将word文件合并到单个文件

http://devpinoy.org/blogs/keithrull/archive/2007/06/09/updated-how-to-merge-multiple-microsoft-word- document.aspx

但是,看到输出文件,我意识到它无法复制第一个文档中的标题图像。我们如何合并保留格式和内容的文档。

I used the code in the link mentioned below to merge word files into a single file

http://devpinoy.org/blogs/keithrull/archive/2007/06/09/updated-how-to-merge-multiple-microsoft-word-documents.aspx

However, seeing the output file i realized that it was unable to copy header image in the first document. How do we merge documents preserving format and content.

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

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

发布评论

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

评论(3

软糖 2024-10-12 14:17:59

我建议使用 GroupDocs.Merger Cloud 用于将多个Word文档合并为单个Word文档,它保留源文档的格式和内容。它是一个独立于平台的REST API解决方案,不依赖于任何第三方工具或软件。

C# 代码示例:

var configuration = new GroupDocs.Merger.Cloud.Sdk.Client.Configuration(MyAppSid, MyAppKey);
var apiInstance_Document = new GroupDocs.Merger.Cloud.Sdk.Api.DocumentApi(configuration);
var apiInstance_File = new GroupDocs.Merger.Cloud.Sdk.Api.FileApi(configuration);

var pathToSourceFiles = @"C:/Temp/input/";
var remoteFolder = "Temp/";
var joinItem_list = new List<JoinItem>();
try
{

    DirectoryInfo dir = new DirectoryInfo(pathToSourceFiles);
    System.IO.FileInfo[] files = dir.GetFiles();
    foreach (System.IO.FileInfo file in files)
    {
        var request_upload = new GroupDocs.Merger.Cloud.Sdk.Model.Requests.UploadFileRequest(remoteFolder + file.Name, File.Open(file.FullName, FileMode.Open));
        var response_upload = apiInstance_File.UploadFile(request_upload);
        var item = new JoinItem
        {
            FileInfo = new GroupDocs.Merger.Cloud.Sdk.Model.FileInfo
            { FilePath = remoteFolder + file.Name }
        };
        joinItem_list.Add(item);
    }

    var options = new JoinOptions
    {
        JoinItems = joinItem_list,
        OutputPath = remoteFolder + "Merged_Document.docx"
    };

    var request = new JoinRequest(options);
    var response = apiInstance_Document.Join(request);

    Console.WriteLine("Output file path: " + response.Path);

}
catch (Exception e)
{
    Console.WriteLine("Exception while Merging Documents: " + e.Message);
}

I will suggest to use GroupDocs.Merger Cloud for merging multiple word document to a single word document, it keeps the formatting and contents of the source documents. It is a platform independent REST API solution without depending on any third-party tool or software.

Sample C# code:

var configuration = new GroupDocs.Merger.Cloud.Sdk.Client.Configuration(MyAppSid, MyAppKey);
var apiInstance_Document = new GroupDocs.Merger.Cloud.Sdk.Api.DocumentApi(configuration);
var apiInstance_File = new GroupDocs.Merger.Cloud.Sdk.Api.FileApi(configuration);

var pathToSourceFiles = @"C:/Temp/input/";
var remoteFolder = "Temp/";
var joinItem_list = new List<JoinItem>();
try
{

    DirectoryInfo dir = new DirectoryInfo(pathToSourceFiles);
    System.IO.FileInfo[] files = dir.GetFiles();
    foreach (System.IO.FileInfo file in files)
    {
        var request_upload = new GroupDocs.Merger.Cloud.Sdk.Model.Requests.UploadFileRequest(remoteFolder + file.Name, File.Open(file.FullName, FileMode.Open));
        var response_upload = apiInstance_File.UploadFile(request_upload);
        var item = new JoinItem
        {
            FileInfo = new GroupDocs.Merger.Cloud.Sdk.Model.FileInfo
            { FilePath = remoteFolder + file.Name }
        };
        joinItem_list.Add(item);
    }

    var options = new JoinOptions
    {
        JoinItems = joinItem_list,
        OutputPath = remoteFolder + "Merged_Document.docx"
    };

    var request = new JoinRequest(options);
    var response = apiInstance_Document.Join(request);

    Console.WriteLine("Output file path: " + response.Path);

}
catch (Exception e)
{
    Console.WriteLine("Exception while Merging Documents: " + e.Message);
}
棒棒糖 2024-10-12 14:17:59

该代码在每个文件后插入一个分页符。

由于节控制标题,如果第二个或后续文档有标题,您可能希望保留原始节属性,并将它们插入到第一个文档之后。

如果您将原始文档视为 docx,您可能会发现您的部分是文档级部分属性元素。

解决问题的最简单方法可能是在最后一段(其中包含标题信息)内创建第二个部分属性元素。然后,当文档合并时,这应该保留在那里(即在其后添加其他段落)。

这就是理论。另请参阅http://www.pcreview.co.uk/forums/thread-898133 .php
但我还没有尝试过;它假设 InsertFile 的行为符合我的预期。

That code is inserting a page break after each file.

Since sections control headers, if a second or subsequent document has a header, you'll probably be wanting to keep the original section properties, and insert those after your first document.

If you look at your original document as a docx, you'll probably see that your section is a document level section properties element.

The easiest way around your problem may be to create a second section properties element inside the last paragraph (which contains the header information). Then this should just stay there when the documents are merged (ie other paragraphs added after it).

That's the theory. See also http://www.pcreview.co.uk/forums/thread-898133.php
But I haven't tried it; it assumes InsertFile behaves as I expect it should.

筱武穆 2024-10-12 14:17:59

我需要做同样的事情,我的方法,无需编码的最简单方法是:

在Mac中:

  1. 文件夹的键盘快捷键 - “选项”然后单击该文件夹旁边的箭头以展开其中的所有文件
  2. 将所有这些文件拖到microsoft word中,它会将标题复制为标题,将其内容复制为正文。

对于 Windows:

  1. 使用插入选项卡 Microsoft Word,选择插入对象,然后从文件资源管理器中选择要插入/合并的所有文件

I needed to do the same, my approach, easiest way without coding is:

In Mac:

  1. keyboard shortcut to the folder - "option" then click the arrow beside that folder to expand all files within it
  2. drag all those files into microsoft word, it will copy the title as a header and its contents as the body.

For windows:

  1. Use the insert tab microsoft word, choose insert object, then choose all files you wish to insert/merge from file explorer
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文