Jul 21, 2020

2 min read

How to copy Pages from one PDF document to another in C# .NET Framework

Transferring pages between documents is actually a lot more challenging of a task than you might initially think. There are a great many details that go into this format that each need to be parsed, preserved, and transferred over. Using raw code, there is no simple way of handling this. Instead, I will be giving you a quick demonstration of how to apply an API to the situation, getting you up and running very fast indeed.

First we will need our library:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.2.8

And next it’s just a matter of calling our function for PDF page copying:

using System;using System.Diagnostics;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;namespace Example{public class EditPdfInsertPagesExample{public void main(){// Configure API key authorization: ApikeyConfiguration.Default.AddApiKey("Apikey", "YOUR_API_KEY");// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");var apiInstance = new EditPdfApi();var sourceFile = new System.IO.Stream(); // System.IO.Stream | Source PDF file to copy pages from.var destinationFile = new System.IO.Stream(); // System.IO.Stream | Destination PDF file to copy pages into.var pageStartSource = 56;  // int? | Page number (1 based) to start copying pages from (inclusive) in the Source file.var pageEndSource = 56;  // int? | Page number (1 based) to stop copying pages pages from (inclusive) in the Source file.var pageInsertBeforeDesitnation = 56;  // int? | Page number (1 based) to insert the pages before in the Destination file.try{// Insert, copy pages from one PDF document into anotherbyte[] result = apiInstance.EditPdfInsertPages(sourceFile, destinationFile, pageStartSource, pageEndSource, pageInsertBeforeDesitnation);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling EditPdfApi.EditPdfInsertPages: " + e.Message );}}}}