The sample source code below will teach you how to remove empty pages from PDF in C#. ByteScout PDF Extractor SDK is the SDK that helps developers to extract data from unstructured documents, pdf, images, scanned and electronic forms. Includes AI functions like automatic table detection, automatic table extraction and restructuring, text recognition and text restoration from pdf and scanned documents. Includes PDF to CSV, PDF to XML, PDF to JSON, PDF to searchable PDF functions as well as methods for low level data extraction. It can remove empty pages from PDF in C#.
The SDK samples like this one below explain how to quickly make your application do remove empty pages from PDF in C# with the help of ByteScout PDF Extractor SDK. Follow the instructions from the scratch to work and copy the C# code. You can use these C# sample examples in one or many applications.
Trial version of ByteScout PDF Extractor SDK is available for free. Source code samples are included to help you with your C# app.
On-demand (REST Web API) version:
Web API (on-demand version)
On-premise offline SDK for Windows:
60 Day Free Trial (on-premise)
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Bytescout.PDFExtractor;
namespace RemoveEmptyPagesExample
{
/// <summary>
/// The example demonstrates detection of empty pages, splitting the document to separate
/// pages excluding empty ones, then combine parts back to a single document.
/// </summary>
class Program
{
static string InputFile = @".\sample.pdf";
static string OutputFile = @".\result.pdf";
static string TempFolder = @".\temp";
static void Main(string[] args)
{
// Create and setup Bytescout.PDFExtractor.TextExtractor instance
TextExtractor extractor = new TextExtractor("demo", "demo");
// Load PDF document
extractor.LoadDocumentFromFile(InputFile);
// List to keep non-empty page numbers
List<string> nonEmptyPages = new List<string>();
// Iterate through pages
for (int pageIndex = 0; pageIndex < extractor.GetPageCount(); pageIndex++)
{
// Extract page text
string pageText = extractor.GetTextFromPage(pageIndex);
// If extracted text is not empty keep the page number
if (pageText.Length > 0)
nonEmptyPages.Add((pageIndex + 1).ToString());
}
// Cleanup
extractor.Dispose();
// Form comma-separated list of page numbers to split("1,3,5")
string ranges = string.Join(",", nonEmptyPages);
// Create Bytescout.PDFExtractor.DocumentSplitter instance
DocumentSplitter splitter = new DocumentSplitter("demo", "demo");
splitter.OptimizeSplittedDocuments = true;
// Split document by non-empty in temp folder
string[] parts = splitter.Split(InputFile, ranges, TempFolder);
// Cleanup
splitter.Dispose();
// Create Bytescout.PDFExtractor.DocumentMerger instance
DocumentMerger merger = new DocumentMerger("demo", "demo");
// Merge parts
merger.Merge(parts, OutputFile);
// Cleanup
merger.Dispose();
// Delete temp folder
Directory.Delete(TempFolder, true);
// Open result document in default associated application (for demo purpose)
ProcessStartInfo processStartInfo = new ProcessStartInfo(OutputFile);
processStartInfo.UseShellExecute = true;
Process.Start(processStartInfo);
}
}
}
60 Day Free Trial or Visit ByteScout PDF Extractor SDK Home Page
Explore ByteScout PDF Extractor SDK Documentation
Explore Samples
Sign Up for ByteScout PDF Extractor SDK Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
60 Day Free Trial or Visit ByteScout PDF Extractor SDK Home Page
Explore ByteScout PDF Extractor SDK Documentation
Explore Samples
Sign Up for ByteScout PDF Extractor SDK Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
also available as: