# empira/pdfsharp architecture

> Architecture diagram of the GitHub repository empira/pdfsharp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/empira/pdfsharp
- Repository: https://github.com/empira/pdfsharp

## Overview

##### Project Type and Purpose
- This is an open-source .NET library suite (monorepo) for PDF creation and document layout: PDFsharp (PDF generation engine) and MigraDoc (higher-level document object model and rendering).
- It targets .NET 6/C# 12, produces NuGet packages, contains command-line tools, sample apps, tests, and build scripts.

##### File Structure Analysis
- Top‐level solution (`PdfSharp.sln`) and global config (central package management, CI pipeline).
- `dev/`: PowerShell scripts to download assets, build packages, run tests.
- `src/`: three main areas:
- `foundation/nuget`: project templates for packaging PDFsharp/MigraDoc variants (GDI, WPF).
- `foundation/src`: core MigraDoc and PDFsharp code organized by layers: DocumentObjectModel, Rendering (GDI, WPF), RTF rendering, core PDF engine, charting, barcodes, cryptography, filters, I/O, security, signatures, accessibility.
- `samples/` and `tools/`: sample applications and CLI utilities that consume the libraries.
- `tests/`: unit and integration tests (xUnit) for each module.
- CI config (`azure-pipelines.yml`, GitVersion scripts).

##### Core Components & Relationships
a. **Document Object Model (DOM)**
– Defines `Document`, `Sections`, `Paragraphs`, `Tables`, `Charts`, etc.
b. **Renderer Layer**
– Renders DOM to intermediate drawing calls. Variants: GDI, WPF, RTF.
c. **PDF Engine**
– Implements low‐level PDF objects (catalog, pages, streams, encryption, filters).
d. **Graphics Abstraction**
– `IXGraphicsRenderer` interface; platform implementations for GDI and WPF.
e. **Utilities & Extensions**
– Charting module, barcode generation, cryptography/signatures, accessibility support.
f. **Packaging & Samples**
– NuGet packaging projects wrap the above into distributable packages; sample apps (HelloWorld) demonstrate usage.
g. **Dev/CI Pipeline**
– PowerShell scripts and Azure Pipelines orchestrate asset download, build, test, and packaging.

##### Architectural Patterns & Principles
- **Layered Architecture**: clear separation from high‐level DOM → rendering → PDF serialization.
- **Plug-in/Provider Pattern**: `IXGraphicsRenderer` abstraction for platform‐specific drawing.
- **Modular Packaging**: separate NuGet projects for each feature (GDI vs. WPF, RTF vs. PDF).
- **Central Package Management** and **GitVersion** ensure consistency and reproducible builds.

- Clear component labels and folder or namespace paths.
- Annotations with key technologies (C#, .NET 6, xUnit, PowerShell, Azure Pipelines, GitVersion, NuGet).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Layer 1: Document Model
    subgraph "Layer 1: Document Model"
        direction TB
        D1["MigraDoc.DocumentObjectModel\n(src/foundation/src/MigraDoc/src/MigraDoc.DocumentObjectModel)"]:::core
    end

    %% Layer 2: Rendering Engines
    subgraph "Layer 2: Rendering Engines"
        direction TB
        RCore["MigraDoc.Rendering\n(src/foundation/src/MigraDoc/src/MigraDoc.Rendering)"]:::render
        RGD["MigraDoc.Rendering-gdi\n(src/foundation/src/MigraDoc/src/MigraDoc.Rendering-gdi)"]:::render
        RWPF["MigraDoc.Rendering-wpf\n(src/foundation/src/MigraDoc/src/MigraDoc.Rendering-wpf)"]:::render
        RTFCore["MigraDoc.RtfRendering\n(src/foundation/src/MigraDoc/src/MigraDoc.RtfRendering)"]:::render
        RTFGDI["MigraDoc.RtfRendering-gdi\n(src/foundation/src/MigraDoc/src/MigraDoc.RtfRendering-gdi)"]:::render
        RTFWPF["MigraDoc.RtfRendering-wpf\n(src/foundation/src/MigraDoc/src/MigraDoc.RtfRendering-wpf)"]:::render
    end

    %% Layer 3: PDF Engine
    subgraph "Layer 3: PDF Engine"
        direction TB
        PDFCore["PdfSharp (core PDF engine)\n(src/foundation/src/PDFsharp/src/PdfSharp)"]:::core
        IXGR["IXGraphicsRenderer Interface\n(src/foundation/src/PDFsharp/src/PdfSharp/Drawing/IXGraphicsRenderer.cs)"]:::interface
    end

    %% Layer 4: Graphics Providers
    subgraph "Layer 4: Graphics Providers"
        direction TB
        PDFGDI["PdfSharp-gdi\n(src/foundation/src/PDFsharp/src/PdfSharp-gdi)"]:::render
        PDFWPF["PdfSharp-wpf\n(src/foundation/src/PDFsharp/src/PdfSharp-wpf)"]:::render
    end

    %% Layer 5: Utilities & Extensions
    subgraph "Layer 5: Utilities & Extensions"
        direction TB
        Chart["PdfSharp.Charting\n(src/foundation/src/PDFsharp/src/PdfSharp.Charting)"]:::util
        Barcode["PdfSharp.BarCodes\n(src/foundation/src/PDFsharp/src/PdfSharp.BarCodes)"]:::util
        Crypto["PdfSharp.Cryptography & Security\n(src/foundation/src/PDFsharp/src/PdfSharp.Cryptography\nand src/foundation/src/PDFsharp/src/PdfSharp.Pdf.Security.Encryption)"]:::util
        Access["Universal Accessibility\n(src/foundation/src/PDFsharp/src/PdfSharp/UniversalAccessibility)"]:::util
    end

    %% Layer 6: Packaging & Samples
    subgraph "Layer 6: Packaging & Samples"
        direction TB
        Nuget["NuGet Templates\n(src/foundation/nuget/src)"]:::pkg
        SMig["MigraDoc HelloWorld\n(src/samples/src/MigraDoc/src/HelloWorld)"]:::pkg
        SPdf["PDFsharp HelloWorld\n(src/samples/src/PDFsharp/src/HelloWorld)"]:::pkg
        Tools["CLI Tools & Utils\n(src/tools/src)"]:::pkg
    end

    %% Dev & CI
    subgraph "Dev & CI"
        direction TB
        Dev["PowerShell Scripts\n(dev/*.ps1)"]:::ci
        CI["Azure Pipelines\n(azure-pipelines.yml)"]:::ci
    end

    %% Dependencies
    D1 --> RCore
    D1 --> RTFCore
    RCore --> RGD
    RCore --> RWPF
    RTFCore --> RTFGDI
    RTFCore --> RTFWPF
    RCore --> PDFCore
    RTFCore -.-> PDFCore
    PDFCore --> IXGR
    IXGR --> PDFGDI
    IXGR --> PDFWPF

    PDFCore --> Chart
    PDFCore --> Barcode
    PDFCore --> Crypto
    PDFCore --> Access
    Chart --> D1
    Barcode --> D1

    %% Packaging and Samples relations
    PDFCore --> Nuget
    RCore --> Nuget
    D1 --> Nuget
    Chart --> Nuget
    Nuget --> SMig
    Nuget --> SPdf
    RGD --> Tools
    RWPF --> Tools
    PDFGDI --> Tools
    PDFWPF --> Tools

    %% CI and Dev
    Dev --> PDFCore
    Dev --> RCore
    Dev --> D1
    Dev --> Nuget
    CI --> Dev
    CI --> SMig
    CI --> SPdf
    CI --> Tools

    %% Click events
    click D1 "https://github.com/empira/pdfsharp/blob/master/src/foundation/src/MigraDoc/src/MigraDoc.DocumentObjectModel"
    click RCore "https://github.com/empira/pdfsharp/blob/master/src/foundation/src/MigraDoc/src/MigraDoc.Rendering"
    click RGD "https://github.com/empira/pdfsharp/blob/master/src/foundation/src/MigraDoc/src/MigraDoc.Rendering-gdi"
    click RWPF "https://github.com/empira/pdfsharp/blob/master/src/foundation/src/MigraDoc/src/MigraDoc.Rendering-wpf"
    click RTFCore "https://github.com/empira/pdfsharp/blob/master/src/foundation/src/MigraDoc/src/MigraDoc.RtfRendering"
    click RTFGDI "https://github.com/empira/pdfsharp/blob/master/src/foundation/src/MigraDoc/src/MigraDoc.RtfRendering-gdi"
    click RTFWPF "https://github.com/empira/pdfsharp/blob/master/src/foundation/src/MigraDoc/src/MigraDoc.RtfRendering-wpf"
    click PDFCore "https://github.com/empira/pdfsharp/tree/master/src/foundation/src/PDFsharp/src/PdfSharp"
    click IXGR "https://github.com/empira/pdfsharp/blob/master/src/foundation/src/PDFsharp/src/PdfSharp/Drawing/IXGraphicsRenderer.cs"
    click PDFGDI "https://github.com/empira/pdfsharp/tree/master/src/foundation/src/PDFsharp/src/PdfSharp-gdi"
    click PDFWPF "https://github.com/empira/pdfsharp/tree/master/src/foundation/src/PDFsharp/src/PdfSharp-wpf"
    click Chart "https://github.com/empira/pdfsharp/blob/master/src/foundation/src/PDFsharp/src/PdfSharp.Charting"
    click Barcode "https://github.com/empira/pdfsharp/blob/master/src/foundation/src/PDFsharp/src/PdfSharp.BarCodes"
    click Crypto "https://github.com/empira/pdfsharp/blob/master/src/foundation/src/PDFsharp/src/PdfSharp.Cryptography"
    click Access "https://github.com/empira/pdfsharp/tree/master/src/foundation/src/PDFsharp/src/PdfSharp/UniversalAccessibility"
    click Nuget "https://github.com/empira/pdfsharp/tree/master/src/foundation/nuget/src"
    click SMig "https://github.com/empira/pdfsharp/tree/master/src/samples/src/MigraDoc/src/HelloWorld"
    click SPdf "https://github.com/empira/pdfsharp/tree/master/src/samples/src/PDFsharp/src/HelloWorld"
    click Tools "https://github.com/empira/pdfsharp/tree/master/src/tools/src"
    click Dev "https://github.com/empira/pdfsharp/blob/master/dev/*.ps1"
    click CI "https://github.com/empira/pdfsharp/blob/master/azure-pipelines.yml"

    %% Styles
    classDef core fill:#D0E8FF,stroke:#0080FF,stroke-width:1px
    classDef render fill:#E2F8D0,stroke:#50A800,stroke-width:1px
    classDef util fill:#FFE5B4,stroke:#FF8C00,stroke-width:1px
    classDef pkg fill:#F0F0F0,stroke:#A0A0A0,stroke-width:1px
    classDef ci fill:#E5D0FF,stroke:#8000FF,stroke-width:1px
    classDef interface fill:#FFF5BA,stroke:#D4A017,stroke-width:1px
```
