Skip to content

Instantly share code, notes, and snippets.

@groupdocs-cloud-gists
Last active June 9, 2022 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groupdocs-cloud-gists/f0095586bf55ff9d3c42aaec85b88561 to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/f0095586bf55ff9d3c42aaec85b88561 to your computer and use it in GitHub Desktop.
Move, Swap and Delete PDF Pages in Ruby
# This code example demonstrates how to move pages within PDF documents.
# Create an instance of the Pages API
@pages_api = GroupDocsMergerCloud::PagesApi.from_keys(@app_sid, @app_key)
# Initialize MoveOptions
@options = GroupDocsMergerCloud::MoveOptions.new
@options.file_info = GroupDocsMergerCloud::FileInfo.new
@options.file_info.file_path = 'rearrange_pages/pdf-pages.pdf'
@options.output_path = "rearrange_pages/move-pages.pdf"
@options.page_number = 2
# At which position to move pdf page
@options.new_page_number = 5
# Reorder pages in PDF file
@result = @pages_api.move(GroupDocsMergerCloud::MoveRequest.new(@options))
puts("Successfully moved pdf page by page number.")
# This code example demonstrates how to delete pages from PDF document.
# Create an instance of the Pages API
@pages_api = GroupDocsMergerCloud::PagesApi.from_keys(@app_sid, @app_key)
# Initialize RemoveOptions
@options = GroupDocsMergerCloud::RemoveOptions.new
@options.file_info = GroupDocsMergerCloud::FileInfo.new
@options.file_info.file_path = 'rearrange_pages/pdf-pages.pdf'
@options.output_path = "rearrange_pages/remove-pages.pdf"
@options.pages = [2, 4, 6]
# Remove pages in PDF file
@result = @pages_api.remove(GroupDocsMergerCloud::RemoveRequest.new(@options))
puts("Successfully deleted specific pages from PDF.")
# This code example demonstrates how to swap pages in PDF document.
# Create an instance of the Pages API
@pages_api = GroupDocsMergerCloud::PagesApi.from_keys(@app_sid, @app_key)
# Initialize SwapOptions
@options = GroupDocsMergerCloud::SwapOptions.new
@options.file_info = GroupDocsMergerCloud::FileInfo.new
@options.file_info.file_path = 'rearrange_pages/pdf-pages.pdf'
@options.output_path = "rearrange_pages/swap-pages.pdf"
@options.first_page_number = 2
@options.second_page_number = 4
# Swap pages in PDF file
@result = @pages_api.swap(GroupDocsMergerCloud::SwapRequest.new(@options))
puts("Successfully swapped pdf pages with page numbers.")

Learn how to rearrange and reorder PDF documents in Ruby using Cloud REST API:

The following topics shall be covered in this article:

  1. Rearrange PDF Pages REST API and Ruby SDK
  2. How to Reorder or Rearrange PDF Pages in Ruby
  3. How to Swap PDF Pages using REST API in Ruby
  4. How to Delete PDF Pages using REST API in Ruby
# Load the gem https://github.com/groupdocs-merger-cloud/groupdocs-merger-cloud-ruby in Ruby application for http://api.groupdocs.cloud
require 'groupdocs_merger_cloud'
# Get your client_id and client_secret from https://dashboard.groupdocs.cloud after free registration.
@app_sid = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
@app_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment