Split PDF via .NET

Split PDF, HTML, TXT files. Use Aspose.PDF for .NET to modify PDF documents programmatically

Split Files Using Aspose.PDF for .NET

Need just part of your large files? To separate one or more parts from your document, you must split the pages of the original file. In order to split files, we’ll use Aspose.PDF for .NET API which is a feature-rich, powerful, and easy-to-use document manipulation API for net platform. Open NuGet package manager, search for Aspose.PDF and install. Our software tool also provides developers to split Word, PDF, HTML, TXT, and DOCX documents into parts. For more details please learn Documentation Pages. You may also use the following command from the Package Manager Console.

Package Manager Console

PM > Install-Package Aspose.PDF

Split documents via C#


You need Aspose.PDF for .NET to try the code in your environment.

  1. Load the PDF with an instance of Document.
  2. Create a new Document class object to split PDF pages.
  3. Add current page to the document.
  4. Save current page as a separate PDF

How to split PDF using C#

With the Aspose.PDF for .NET library, you can split large PDF documents. Splitting a PDF document is a common use case when working with PDF documents. It helps reduce the size of a PDF file by breaking large documents into smaller files to send via email.

Split PDF - C#

This sample code shows how to split PDF file - C#

var pdfEditor = new PdfFileEditor();
fs = new FileStream("1.pdf", FileMode.Open, FileAccess.Read);
doc = new Document(fs);
pdfEditor.Extract(pathSource, 1, doc.Pages.Count / 2, "pdf_half.pdf");

How to split HTML files using C#

HTML documents can be large as they can contain text, images, charts, and more. You may need to separate HTML files according to different requirements or use cases. The Aspose.PDF for .NET library will help you with this task.

Split HTML - C#

This sample code shows how to split HTML file - C#

//save input html to pdf to file
doc = new Document("1.html", new HtmlLoadOptions());
doc.Save("test.pdf", SaveFormat.Pdf);

var pdfEditor = new PdfFileEditor();
pdfEditor.SplitFromFirst("test.pdf", 1, "test.pdf");
doc = new Document("test.pdf");
doc.Save("html_first.html", SaveFormat.Html);

How to split TXT files using C#

The Aspose.PDF library afor .NET allows C# developers to split TXT files into separate parts. The function of splitting a TXT document into separate files will help you work with different sections of a large document at the same time and for several users. Splitting a TXT document will speed up and streamline your work. Use the following code snippet to extract the pages of your TXT file.

Split TXT - C#

This sample code shows how to split TXT file - C#

//save input text to pdf to file
pdfEditor = new PdfFileEditor();
var doc = new Document("1.txt", new TxtLoadOptions());
doc.Save("test.pdf", SaveFormat.Pdf);

MemoryStream [] pages = pdfEditor.SplitToPages("test.pdf");

int index = 1;
foreach(var ms in pages)
{
    page = new Document(ms);
    var textAbsorber = new TextAbsorber();
    page.Pages.Accept(textAbsorber);
    string extractedText = textAbsorber.Text;
    File.WriteAllText("text_"+index+".txt", extractedText);
    index++;
}