Merge PDF in .NET SDK
Merge two PDF files into one in C# Cloud API without the use of any software like Adobe PDF.
Get StartedHow to Merge Multiple PDF Files Using C# Cloud API
In order to merge two PDF files we’ll use
Aspose.PDF Cloud .NET SDK
This Cloud SDK allows you to easily build cloud-based PDF creator, editor & converter apps in C#, ASP.NET, or other .NET languages for various cloud platforms. Open
NuGet
package manager, search for
Aspose.PDF Cloud
and install. You may also use the following command from the Package Manager Console: PM> Install-Package Aspose.Pdf-Cloud
Steps for Merging PDF via Cloud .NET SDK
A basic merging pdfs programmatically with Aspose.PDF Cloud .NET SDK APIs can be done with just few lines of code.
- Create a new Configuration object with your Application Secret and Key
- Create an object to connect to the Cloud API
- Upload your document file
- Perform the merging
- Download the result
Merge Two PDF Files using C#
public static async Task Merge()
{
const string localFolder = @"C:\Samples";
const string storageTempFolder = "YourTempFolder";
const string resultFileName = "output_merged.pdf";
List<string> pdfFileNames = new List<string> {
"sample.pdf", "PdfWithLayers.pdf", "PdfWithLinks.pdf"
};
// Get your AppSid and AppSecret from https://dashboard.aspose.cloud (free registration required).
var pdfApi = new PdfApi(AppSecret, AppSid);
Aspose.Pdf.Cloud.Sdk.Model.MergeDocuments documetItems = new(new List<string>());
foreach (var fileName in pdfFileNames)
{
using var file = File.OpenRead(Path.Combine(localFolder, fileName));
await pdfApi.UploadFileAsync(Path.Combine(storageTempFolder, fileName), file);
documetItems.List.Add(Path.Combine( storageTempFolder, fileName));
}
DocumentResponse response = await pdfApi.PutMergeDocumentsAsync(resultFileName, documetItems, folder: storageTempFolder);
if (response == null)
Console.WriteLine("MergeDocuments(): Unexpected error!");
else if (response.Code < 200 || response.Code > 299)
Console.WriteLine("MergeDocuments(): Failed to documents.");
else
{
using Stream downloadStream = await pdfApi.DownloadFileAsync(Path.Combine(storageTempFolder, resultFileName));
using FileStream localStream = File.Create(Path.Combine(localFolder, resultFileName));
await downloadStream.CopyToAsync(localStream);
Console.WriteLine("MergeDocuments(): documents successfully merged to '{0}' file.", resultFileName);
}
}
- Learning Resources
- Documentation
- Source Code
- API References
- Product Support
- Free Support
- Paid Support
- Blog
- Why Aspose.PDF Cloud for .NET?
- Customers List
- Security