开发者

merge word documents to a single document

开发者 https://www.devze.com 2023-01-27 15:33 出处:网络
I used t开发者_如何转开发he code in the link mentioned below to merge word files into a single file

I used t开发者_如何转开发he 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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号