Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby-Merge-Word-Documents-API.md
# Create an instance of the File API
@fileApi = GroupDocsConversionCloud::FileApi.from_keys(@client_id, @client_secret)
# Download joined document request
@request = GroupDocsConversionCloud::DownloadFileRequest.new("merge/join.docx", @mystorage)
@response = @fileApi.download_file(@request)
puts("Document downloading successfully completed." + (@response).to_s )
# Create an instance of the Document API
@mergerApi = GroupDocsMergerCloud::DocumentApi.from_keys(@client_id, @client_secret)
@item1 = GroupDocsMergerCloud::JoinItem.new
@item1.file_info = GroupDocsMergerCloud::FileInfo.new
@item1.file_info.file_path = 'merge/ten-pages.docx'
@item1.pages = [3, 6, 8]
@item2 = GroupDocsMergerCloud::JoinItem.new
@item2.file_info = GroupDocsMergerCloud::FileInfo.new
@item2.file_info.file_path = 'merge/four-pages.docx'
@item2.start_page_number = 1
@item2.end_page_number = 4
@item2.range_mode = "OddPages"
@options = GroupDocsMergerCloud::JoinOptions.new
@options.join_items = [@item1, @item2]
@options.output_path = "merge/join-pages.docx"
@result = @mergerApi.join(GroupDocsMergerCloud::JoinRequest.new(@options))
puts("Merge multiple word documents pages in Ruby.")
@mergerApi = GroupDocsMergerCloud::DocumentApi.from_keys(@client_id, @client_secret)
@item1 = GroupDocsMergerCloud::JoinItem.new
@item1.file_info = GroupDocsMergerCloud::FileInfo.new
@item1.file_info.file_path = 'merge/four-pages.docx'
@item2 = GroupDocsMergerCloud::JoinItem.new
@item2.file_info = GroupDocsMergerCloud::FileInfo.new
@item2.file_info.file_path = 'merge/one-page.docx'
@options = GroupDocsMergerCloud::JoinOptions.new
@options.join_items = [@item1, @item2]
@options.output_path = "merge/join.docx"
@result = @mergerApi.join(GroupDocsMergerCloud::JoinRequest.new(@options))
puts("Successfully merged word documents pages using Ruby.")

Learn how to merge Word documents in Ruby using Cloud REST API:

The following topics shall be covered in this article:

  1. Word Merger REST API and Ruby SDK
  2. Merge Multiple Word Documents using REST API in Ruby
  3. Merge Specific Pages of Multiple Word Documents using Ruby
  4. Online Word Merger | Combine DOCX Online
# Create an instance of the File API
@fileApi = GroupDocsConversionCloud::FileApi.from_keys(@client_id, @client_secret)
# Upload documents one by one to cloud storage from local system
@document = "#{Rails.root}/public/groupdocs-files/four-pages.docx"
@fileStream = File.new(@document, "r")
@request = GroupDocsConversionCloud::UploadFileRequest.new('merge/four-pages.docx', @fileStream, @mystorage)
@response = @fileApi.upload_file(@request)
puts("Document successfully uploaded on the cloud." + (@response).to_s )
# Load the document merger gem
require 'groupdocs_merger_cloud'
@client_id = "xxxxxxxxx-xxxxx-xxxx-xxxxxxxxxx"
@client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Add your storage name here
@mystorage = "LocalStorage"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment