Table of Contents

Method SplitToFiles

Namespace
LMKit.Document.Pdf
Assembly
LM-Kit.NET.dll

SplitToFiles(Attachment, IEnumerable<string>, string, string)

Splits a PDF attachment into multiple files based on the provided page ranges.

public static List<string> SplitToFiles(Attachment source, IEnumerable<string> pageRanges, string outputDirectory, string fileNamePrefix = null)

Parameters

source Attachment

The source PDF attachment.

pageRanges IEnumerable<string>

A collection of 1-based page range strings (e.g., "1-3", "4-6", "7"). Each range produces one output file.

outputDirectory string

Directory where the output files will be written. Created if it does not exist.

fileNamePrefix string

Optional prefix for output file names. Defaults to the source file name without extension. Files are named {prefix}_part{N}.pdf where N starts at 1.

Returns

List<string>

A list of full paths to the created files.

Examples

using System;
using System.Collections.Generic;
using LMKit.Data;
using LMKit.Document;

var source = new Attachment("annual_report.pdf");

// Split a 12-page report into quarterly sections
List<string> files = PdfSplitter.SplitToFiles(
    source,
    new[] { "1-3", "4-6", "7-9", "10-12" },
    "output/quarters",
    "Q_report");

// Output: Q_report_part1.pdf, Q_report_part2.pdf, ...
foreach (string path in files)
{
    Console.WriteLine($"Created: {path}");
}

Exceptions

ArgumentNullException

Thrown when source, pageRanges, or outputDirectory is null.

ArgumentException

Thrown when the source attachment is not a PDF.

SplitToFiles(Attachment, DocumentSplittingResult, string, string)

Splits a PDF attachment into multiple files based on the segments detected by DocumentSplitting.

public static List<string> SplitToFiles(Attachment source, DocumentSplittingResult splittingResult, string outputDirectory, string fileNamePrefix = null)

Parameters

source Attachment

The source PDF attachment.

splittingResult DocumentSplittingResult

The result of a Split(Attachment, CancellationToken) or SplitAsync(Attachment, CancellationToken) operation.

outputDirectory string

Directory where the output files will be written. Created if it does not exist.

fileNamePrefix string

Optional prefix for output file names. Defaults to the source file name without extension. Files are named {prefix}_part{N}.pdf where N starts at 1.

Returns

List<string>

A list of full paths to the created files.

Examples

using System;
using System.Collections.Generic;
using LMKit.Data;
using LMKit.Document;
using LMKit.Extraction;
using LMKit.Model;

// Load a scanned batch of mixed documents
var source = new Attachment("batch_scan.pdf");

// Detect logical document boundaries with AI
var model = LM.LoadFromModelID("qwen2-vl:8b");
var splitting = new DocumentSplitting(model);
DocumentSplittingResult result = splitting.Split(source);

// Save each detected document to its own file
List<string> files = PdfSplitter.SplitToFiles(
    source,
    result,
    "output/split_docs",
    "doc");

for (int i = 0; i < files.Count; i++)
{
    Console.WriteLine($"Segment '{result.Segments[i].Label}' saved to: {files[i]}");
}

Exceptions

ArgumentNullException

Thrown when source, splittingResult, or outputDirectory is null.

ArgumentException

Thrown when the source attachment is not a PDF.

SplitToFiles(string, IEnumerable<string>, string, string)

Splits a PDF file into multiple files based on the provided page ranges.

public static List<string> SplitToFiles(string inputPath, IEnumerable<string> pageRanges, string outputDirectory, string fileNamePrefix = null)

Parameters

inputPath string

Path to the source PDF file.

pageRanges IEnumerable<string>

A collection of 1-based page range strings. Each range produces one output file.

outputDirectory string

Directory where the output files will be written. Created if it does not exist.

fileNamePrefix string

Optional prefix for output file names. Defaults to the input file name without extension. Files are named {prefix}_part{N}.pdf where N starts at 1.

Returns

List<string>

A list of full paths to the created files.

Examples

using System;
using System.Collections.Generic;
using LMKit.Document;

// Split a PDF file into 3 parts by page ranges
List<string> files = PdfSplitter.SplitToFiles(
    "full_report.pdf",
    new[] { "1-10", "11-20", "21-30" },
    "output/chapters",
    "chapter");

// Output: chapter_part1.pdf, chapter_part2.pdf, chapter_part3.pdf
foreach (string path in files)
{
    Console.WriteLine($"Created: {path}");
}

Exceptions

ArgumentNullException

Thrown when inputPath, pageRanges, or outputDirectory is null.

FileNotFoundException

Thrown when inputPath does not exist.