Table of Contents

👉 Try the demo: https://github.com/LM-Kit/lm-kit-net-samples/tree/main/console_net/document-intelligence/pdf-toolkit/pdf_merger_batch

PDF Merger Batch for C# .NET Applications


🎯 Purpose of the Demo

An interactive console app that combines multiple PDFs into a single packet. Either type the input paths in the order you want them stitched, or point at a folder and let the demo enumerate every PDF inside (with a configurable sort order). Built on PdfInfo for validation and PdfMerger.MergeFilesAsync for the actual concatenation.

All processing runs on-device.


👥 Industry Target Audience

  • Finance / accounting: month-end packets (cover, invoice, receipts, signatures).
  • Legal: case file assembly, exhibit binders, signed-contract packages.
  • Healthcare: patient record consolidation across departmental exports.
  • HR / onboarding: stitching offer, NDA, policies, and ID copies into one signed packet.
  • Any back office that emails or archives multi-source PDF bundles.

🚀 Problem Solved

Online merge tools are convenient but force confidential documents through a third-party. Hand-written stitching with iText or PdfSharp is doable but requires non-trivial boilerplate per project. This demo collapses both: a single local app that takes an ordered list or a folder, validates every file, and emits one merged PDF. The validation pass catches encrypted or corrupt PDFs before the merge starts, so failures are visible up-front.


💻 Application Overview

Interactive menu (no command-line arguments) with two modes:

Mode What it does
List Prompts for PDF paths one per line. Blank line ends the list. Then asks for the output path and merges in the order you typed them.
Folder Prompts for a folder, asks whether to recurse, asks for sort order (name or mtime), then merges every .pdf found.
Quit Exit.

Each mode runs the same pipeline: PdfInfo.GetPageCountAsync validates every input and prints a per-file page count, then PdfMerger.MergeFilesAsync produces the output. Ctrl-C cancels the merge cleanly.

✨ Key Features

  • PdfInfo.GetPageCountAsync(path, ct) for cheap input validation.
  • PdfMerger.MergeFilesAsync(IEnumerable<string>, string, CancellationToken) for the actual merge.
  • In-memory variant: PdfMerger.Merge(IEnumerable<Attachment>) is also available if your inputs are already in memory.
  • Order control: list mode preserves user-typed order; folder mode lets you sort by name or modification time.

🧠 Model

  • None. This demo is pure document plumbing and does not load an LLM.

🛠️ Getting Started

📋 Prerequisites

  • .NET 8.0 or later

▶️ Running the Application

git clone https://github.com/LM-Kit/lm-kit-net-samples
cd lm-kit-net-samples/console_net/document-intelligence/pdf-toolkit/pdf_merger_batch
dotnet run

Pick a mode from the menu and follow the prompts.

Share