Method ExtractPages
ExtractPages(Attachment, string)
Extracts the specified pages from a PDF attachment and returns a new attachment containing only those pages.
public static Attachment ExtractPages(Attachment source, string pageRange)
Parameters
sourceAttachmentThe source PDF attachment.
pageRangestringA 1-based page range string (e.g., "1-5, 7, 9-12"). Null, empty, or "*" extracts all pages.
Returns
- Attachment
A new Attachment containing the extracted pages.
Examples
using System;
using LMKit.Data;
using LMKit.Document;
// Load a multi-page PDF
var source = new Attachment("contract.pdf");
Console.WriteLine($"Source has {source.PageCount} pages");
// Extract pages 2 through 4
Attachment extracted = PdfSplitter.ExtractPages(source, "2-4");
Console.WriteLine($"Extracted PDF has {extracted.PageCount} pages");
Exceptions
- ArgumentNullException
Thrown when
sourceisnull.- ArgumentException
Thrown when the source attachment is not a PDF or when the page range resolves to zero pages.
ExtractPages(Attachment, int[])
Extracts the specified pages from a PDF attachment and returns a new attachment containing only those pages.
public static Attachment ExtractPages(Attachment source, int[] pageIndexes)
Parameters
sourceAttachmentThe source PDF attachment.
pageIndexesint[]Zero-based page indexes to extract.
Returns
- Attachment
A new Attachment containing the extracted pages.
Examples
using System;
using LMKit.Data;
using LMKit.Document;
// Load a PDF
var source = new Attachment("report.pdf");
// Extract the first, third, and fifth pages (zero-based indexes)
Attachment extracted = PdfSplitter.ExtractPages(source, new[] { 0, 2, 4 });
Console.WriteLine($"Extracted PDF has {extracted.PageCount} pages");
Exceptions
- ArgumentNullException
Thrown when
sourceorpageIndexesisnull.- ArgumentException
Thrown when the source attachment is not a PDF or when no page indexes are provided.
ExtractPages(string, string, string)
Extracts the specified pages from a PDF file and writes the result to an output file.
public static void ExtractPages(string inputPath, string pageRange, string outputPath)
Parameters
inputPathstringPath to the source PDF file.
pageRangestringA 1-based page range string (e.g., "1-5, 7, 9-12"). Null, empty, or "*" extracts all pages.
outputPathstringPath where the extracted PDF will be written.
Examples
using LMKit.Document;
// Extract the first 5 pages into a separate file
PdfSplitter.ExtractPages(
"full_report.pdf",
"1-5",
"summary_pages.pdf");
// Extract non-contiguous pages (cover, TOC, and last page)
PdfSplitter.ExtractPages(
"full_report.pdf",
"1, 3, 50",
"selected_pages.pdf");
Exceptions
- ArgumentNullException
Thrown when
inputPathoroutputPathisnull.- FileNotFoundException
Thrown when
inputPathdoes not exist.
ExtractPages(string, int[], string)
Extracts the specified pages from a PDF file and writes the result to an output file.
public static void ExtractPages(string inputPath, int[] pageIndexes, string outputPath)
Parameters
inputPathstringPath to the source PDF file.
pageIndexesint[]Zero-based page indexes to extract.
outputPathstringPath where the extracted PDF will be written.
Examples
using LMKit.Document;
// Extract pages by zero-based indexes
PdfSplitter.ExtractPages(
"full_report.pdf",
new[] { 0, 4, 9 },
"selected_pages.pdf");
Exceptions
- ArgumentNullException
Thrown when
inputPathoroutputPathisnull.- FileNotFoundException
Thrown when
inputPathdoes not exist.