Documentation

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

outStream.SaveToFile("merged.pdf");

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

splitStream.SaveToFile("split.pdf");

Summary

Here is the complete example

//MERGE PDF
var url = "https://www.google.com/";
//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);
outStream.SaveToFile("merged.pdf");

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