Merge and Split Pdf files

Pdf files can merged and split using DGtalSharp.Pdf

Install nuget package DGtalSharp.Pdf

Install-Package DGtalSharp.Pdf -Version 1.0.1-beta

Adding using in namespaces

using DGtalSharp.Pdf;

Merge Pdf

Merge Pdf streams using code

var outStream = PdfDocument.Merge(stream1, stream2);

Pdf files merged, now you can save output stream to file using code


Split Pdf

Split Pdf using code

var splitStream = PdfDocument.SplitPages(inputStream, 1, 5);

You can pass page range to split. In above example, a new pdf will be created with page range from 1 to 5. You can save this pdf stream to file using code following code



Here is the complete example

var url = "";
//Generate Pdf from Url
var stream1 = await HtmlDocument.GetPdfStreamFromUrl(url, actions: document => document.AddStamp("Draft"));

//Open existing pdf file
var stream2 = PdfDocument.OpenPdf("sample.pdf");

var outStream = PdfDocument.Merge(stream1, stream2);

var fileStream = PdfDocument.OpenPdf("sample.pdf");
var split = PdfDocument.SplitPages(fileStream, 1, 5);
await split.SaveToFileAsync("Split.pdf");