Skip to content
bitzorcas
中EN

Concept

Structured Legal Document Templating & Streaming Generation

In-depth architecture of BitzOrcas.Platform.Documents: structured contract templating, dynamic token replacement, PDF streaming generation, and anti-leak watermarking.

Last updated

Structured Legal Document Templating & Streaming Generation

In enterprise legal and commerce workflows, generating non-disclosure agreements (NDAs), commercial contracts, legal briefs, and statutory notices represents a core high-frequency requirement. These documents require exacting typesetting control:

  • Strict Formatting: Font sizing, indentation, headers/footers, and widow/orphan pagination prevention;
  • Security Watermarking: Embedding dynamic diagonal watermarks containing operator names, tenant tokens, and timestamps;
  • Streaming Throughput: Large multi-megabyte exports must bypass heap buffer allocation, streaming directly to S3 or HTTP responses.

BitzOrcas.Platform.Documents provides an enterprise-grade document templating and streaming generation engine.


1. Document Pipeline Architecture

The document pipeline comprises four decoupled subsystems:

Persistence & DeliveryBitzOrcas Document EngineContext & Master Assets

Matter / Contract Payload (JSON DTO)

Document Master (.docx / XML AST)

Safe Document Parser (Anti-XXE)

Watermark Interceptor (WatermarkInjector)

Word / PDF Streaming Renderer

MinIO / AWS S3 Object Storage

HTTP 200 FileStreamResult (Direct Download)


2. Core Templating Contract: IDocumentGenerator

In BitzOrcas.Platform.Documents.Contracts, the service exposes streaming interfaces:

using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using BitzOrcas.Domain.Results;
namespace BitzOrcas.Platform.Documents.Contracts;
/// <summary>
/// Output document format options
/// </summary>
public enum DocumentOutputFormat
{
Docx = 1,
Pdf = 2
}
/// <summary>
/// Port contract for structured document generation
/// </summary>
public interface IDocumentGenerator
{
/// <summary>
/// Renders the document based on a registered template and parameter dictionary directly to a target stream
/// </summary>
/// <param name="templateKey">Unique template token, such as NDA_COMMERCIAL_V2</param>
/// <param name="parameters">Dictionary of strongly typed replacement tokens</param>
/// <param name="outputFormat">Target format (Docx or Pdf)</param>
/// <param name="targetOutputStream">Writable output stream (HTTP Response or S3 stream)</param>
/// <param name="cancellationToken">Cancellation token</param>
Task<Result> RenderDocumentStreamAsync(
string templateKey,
IReadOnlyDictionary<string, object> parameters,
DocumentOutputFormat outputFormat,
Stream targetOutputStream,
CancellationToken cancellationToken = default);
}

3. Watermarking & Anti-XXE Defense

3.1 Diagonal Translucent Watermark Injection (WatermarkInjector)

For sensitive internal dossiers, the engine renders a diagonal vector watermark across every PDF page:

Watermark Composition Rule: Watermark Text = TenantName + " | " + OperatorName + " (" + EmployeeId + ") | " + Timestamp (UTC+8)

Embedded as native vector paths, the watermark remains permanently legible across both display screenshots and physical paper prints.

3.2 Anti-XXE Parsing Security

Because .docx files are ZIP packages of XML streams, parsing custom uploaded templates presents XML External Entity (XXE) injection risks. BitzOrcas explicitly hardens the XML reader:

var settings = new XmlReaderSettings
{
DtdProcessing = DtdProcessing.Prohibit, // Physically prohibits DTD processing
XmlResolver = null // Disables external entity resolution
};

100%

Scroll or use controls to zoom · drag when enlarged · double-click for 100% / 200%