Click or drag to resize

Getting Started with ExpertPdf PDF Merger Library for .NET

There are 2 ways to install ExpertPdf PdfMerge Library:

  • Using a NuGet package (.NET Framework and .NET Core)

  • Downloading assemblies

Installation using Nuget Packages

The easiest way to install ExpertPdf PdfMerge is by using a Nuget Package. Create your project, go to Nuget Package Manager and install one of the following packages:

.NET Framework and .NET Core version - AnyCPU - ExpertPdf.MergePdf: https://www.nuget.org/packages/ExpertPdf.MergePdf/.

.NET Framework and .NET Core version - x64 optimized version - ExpertPdf.MergePdf.x64: https://www.nuget.org/packages/ExpertPdf.MergePdf.x64/.

Note 1: If the x64 version is used, the application needs to target x64 platform and it needs to run in an x64 worker process.

Note 2: The .NET Core version is for .NET Core 2.0 or above through .NET Standard 2.0. Currently it requires a Windows system to run. It does not work on Linux or Mac or Xamarin.

Installation using Downloaded Assemblies

The ExpertPdf PDF Merge Library for .NET can be used in any type of application to merge PDF documents and images. The product archive contains the development libraries for .NET 2.0 or .NET 4.0 and ready to use samples written in C# and VB.NET (console application and Windows Forms application in the Samples folder).

Here are the steps needed to get started working with the library referencing the assembly directly:

  1. Add epmergepdf.dll to the bin folder of your application.

  2. Add a reference in the project to epmergepdf.dll.

  3. Write your code (see the samples and the API reference for help).

PDF Merge Development Library

The PDF Merge development library epmergepdf.dll is a managed assembly that can be linked into any .NET application, either Windows Forms and console applications or ASP.NET 2.0 or 4.0 web sites. The interface is very easy to use and understand. The main class in the assembly is PDFMerge. An instance of this class can be constructed by specifying the merged document options as an instance of the PdfDocumentOptions class or using the default document options (normal compression, A4 page size and portrait orientation). If you need to change any of these settings you have to pass a PdfDocumentOptions object to the PDFMerge constructor. For a complete reference of the assembly classes and methods, see the API section in this manual.

After the instance of the PDFMerge class is created, then you need to append elements to the merged document one by one. The PDFMerge class offers interfaces to append PDF files, PDF streams, Image files, Image objects, Empty pages to merged document. There is an Append method for each of these elements. Each element appears in the merged document starting on a new page in the order you appended them to the document. After you have added the merge elements you simply call the RenderMergedPDFDocument to get an in memory representation of the PDF document or SaveMergedPDFToFile(String) to save the generated document in a specified disk file.

The PDFMerge class also offers a few properties that can be used to further customize the generated PDF document:

  • DocumentInfo - can be used to set the merged document information like author, title, creation date, etc.

  • SecurityOptions - can be used to set the merged document encryption, password protection, print and copy rights, etc.

Code Sample

Below there is a simple code sample taken from the command line utility.

PdfDocumentOptions pdfDocumentOptions = new PdfDocumentOptions();

pdfDocumentOptions.PdfCompressionLevel = PDFCompressionLevel.Normal;
pdfDocumentOptions.PdfPageSize =  PdfPageSize.A4;
pdfDocumentOptions.PdfPageOrientation = PDFPageOrientation.Portrait;

PDFMerge pdfMerge = new PDFMerge(pdfDocumentOptions);

pdfMerge.AppendPDFFile(pdfFilePath);
pdfMerge.AppendImageFile(imageFilePath);
pdfMerge.AppendEmptyPage();

pdfMerge.SaveMergedPDFToFile(outFile);
PDF Merge Command Line Tool

The PDF Merge Command Line Tool is a simple application constructed based on the development libraries. It is also a ready to use application in case you don't need to build your own application. The command line syntax is:

EPMergePDFDemo.exe {[/pdf:pdf_file] | [/emptypage] | [/image:img_file] }+ [/pagesize:(A4|A3|A2|A1|A0)] [/compression:(NoCompression|Normal|Best)] [/orientation:(Portrait|Landscape)] /out:out_file.pdf

In simple terms, this syntax means that you can specify the pdf files and image files in any order. The resulted merged document is specified by the /out: argument. The full path of the source files must be specified in the arguments list. If the file path contains spaces you should quote the file name (e.g /pdf:"C:\My Documents\MyPdfFile.pdf").

Beside the elements to be merged, you can also specify the resulted document options like compression level, page orientation and page size.

Command line example:

EPMergePDFDemo.exe /pdf:"C:\Report.pdf" /emptypage /image:"C:\Image.Jpeg" /out:MergedDocument2.pdf /compression:Normal /orientation:Landscape

Licensing

The LicenseKey property of the PDFMerge class should be set with the license key string you have received after the product purchase.