Splitting PDF documents into separate pages is a common task in software development. In this article, we will look at how you can split PDF documents directly in memory, without saving intermediate files to disk, using the C# programming language and platform .NET with a component PDF .Net from SautinSoft.
To complete the task, we will need the following steps:
Splitting PDF documents in memory using C# and .NET is an efficient way to process PDF without having to work with the file system. The SautinSoft PDF library .Net provides a simple and powerful API for performing such tasks. Using the above code example, you can easily integrate PDF splitting functionality into your applications.
Complete code
using System;
using System.IO;
using System.Collections.Generic;
using SautinSoft;
using SautinSoft.Pdf;
using SautinSoft.Pdf.Content;
using SautinSoft.Pdf.Facades;
namespace Sample
{
class Sample
{
/// <summary>
/// Split PDF documents in memory using C# and .NET.
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/merge-pdf-documents-in-memory-using-csharp-and-dotnet.php
/// </remarks>
static void Main(string[] args)
{
// Before starting this example, please get a free 100-day trial key:
// https://sautinsoft.com/start-for-free/
// Apply the key here:
// PdfDocument.SetLicense("...");
SplitPdfInMemory();
}
static void SplitPdfInMemory()
{
int page = 0;
using var fs = new FileStream(@"..\..\..\005.pdf", FileMode.Open, FileAccess.ReadWrite);
foreach (var stream in PdfSplitter.Split(fs, PdfLoadOptions.Default, 0, int.MaxValue))
{
using var output = new FileStream($"Page {++page}.pdf", FileMode.Create, FileAccess.ReadWrite);
stream.CopyTo(output);
}
// Show the "Page 5.pdf"
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("Page 5.pdf") { UseShellExecute = true });
}
}
}
If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below: