PDF Merge — C# source
Combine PDF files in order, entirely in your browser — reorder, merge, download. Nothing uploads.
This is the C# implementation — the same logic the interactive tool runs, in a shareable, citable form.
// pdf-merge — merge many PDFs into one with PDFsharp (PdfDocument.Open,
// docs.pdfsharp.net).
//
// Language: C# (.NET 8), PDFsharp 6 for the PDF I/O (display-only
// dependency — the pure helpers below are standard-library only).
// CosmoDev polyglot showcase port of the `pdf-merge` tool.
// Ported from src/lib/pdf-merge.ts — display source, part of CosmoDev's
// polyglot tool pages. Structure mirrors java.java in this directory.
//
// Same contract: empty input rejected, encrypted vs invalid classified from
// the raw load-error message, page count returned for UI display.
using System;
using System.Collections.Generic;
using System.IO;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
/// Load failure kind: Encrypted (password-protected) or Invalid (unparseable).
public enum ErrorKind
{
Encrypted,
Invalid,
}
/// The C# twin of the TS PdfToolError class.
public sealed class PdfToolError : Exception
{
public ErrorKind Kind { get; }
public PdfToolError(ErrorKind kind, string message)
: base(message)
{
Kind = kind;
}
}
public static class PdfMerge
{
/// Pure classifier — encrypted vs invalid, from the raw error message.
/// Same /encrypt|password/i heuristic the TS lib applies.
public static ErrorKind ClassifyLoadError(string msg)
{
if (msg != null)
{
string lower = msg.ToLowerInvariant();
if (lower.Contains("encrypt") || lower.Contains("password"))
return ErrorKind.Encrypted;
}
return ErrorKind.Invalid;
}
/// Map a thrown load/parse error to a user-facing PdfToolError.
public static PdfToolError MapError(Exception e)
{
ErrorKind kind = ClassifyLoadError(e.Message);
return new PdfToolError(
kind,
kind == ErrorKind.Encrypted
? "This PDF is password-protected. Unlock it first."
: "Not a readable PDF file.");
}
/// The merge outcome: output bytes plus the page count for UI display.
public readonly record struct MergeResult(byte[] Bytes, int PageCount);
/// Copy every page of every input, in order, into one fresh PdfDocument.
public static MergeResult MergePdfs(IReadOnlyList<byte[]> docs)
{
if (docs.Count == 0)
throw new PdfToolError(ErrorKind.Invalid, "Nothing to merge.");
using var outDoc = new PdfDocument();
try
{
foreach (byte[] bytes in docs)
{
using var ms = new MemoryStream(bytes);
// PDFsharp refuses password-protected files at Open() — the
// exception message carries the keyword, and MapError
// classifies it (the TS ignoreEncryption: false path).
using PdfDocument src = PdfDocument.Open(ms);
foreach (PdfPage page in src.Pages)
outDoc.AddPage(page); // imports a clone of the page
}
using var msOut = new MemoryStream();
outDoc.Save(msOut, streamCantSeek: false);
return new MergeResult(msOut.ToArray(), outDoc.PageCount);
}
catch (PdfToolError)
{
throw; // classification failures surface as themselves
}
catch (Exception e)
{
// parse failures -> the shared error mapping (PDFsharp's
// password errors carry the keyword in their message)
throw MapError(e);
}
}
/// Page count for UI display — the tool's only other PDFsharp need.
public static int CountPages(byte[] bytes)
{
try
{
using var ms = new MemoryStream(bytes);
using PdfDocument doc = PdfDocument.Open(ms);
return doc.PageCount;
}
catch (PdfToolError)
{
throw;
}
catch (Exception e)
{
throw MapError(e);
}
}
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →