Method SplitToFiles
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
sourceAttachmentThe source PDF attachment.
pageRangesIEnumerable<string>A collection of 1-based page range strings (e.g., "1-3", "4-6", "7"). Each range produces one output file.
outputDirectorystringDirectory where the output files will be written. Created if it does not exist.
fileNamePrefixstringOptional prefix for output file names. Defaults to the source file name without extension. Files are named
{prefix}_part{N}.pdfwhere N starts at 1.
Returns
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, oroutputDirectoryisnull.- 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
sourceAttachmentThe source PDF attachment.
splittingResultDocumentSplittingResultThe result of a Split(Attachment, CancellationToken) or SplitAsync(Attachment, CancellationToken) operation.
outputDirectorystringDirectory where the output files will be written. Created if it does not exist.
fileNamePrefixstringOptional prefix for output file names. Defaults to the source file name without extension. Files are named
{prefix}_part{N}.pdfwhere N starts at 1.
Returns
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, oroutputDirectoryisnull.- 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
inputPathstringPath to the source PDF file.
pageRangesIEnumerable<string>A collection of 1-based page range strings. Each range produces one output file.
outputDirectorystringDirectory where the output files will be written. Created if it does not exist.
fileNamePrefixstringOptional prefix for output file names. Defaults to the input file name without extension. Files are named
{prefix}_part{N}.pdfwhere N starts at 1.
Returns
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, oroutputDirectoryisnull.- FileNotFoundException
Thrown when
inputPathdoes not exist.