extractImages method
Extracts images from the current PDF document into a directory.
Parameters:
directoryPath - The directory where extracted images will be saved.
pages - Optional zero-based page indexes. If omitted or empty, images
from all pages will be extracted.
Returns: An extraction result containing success state, image count, output directory, and image paths found in that directory after extraction.
example:
final tempDir = await ComPDFKit.getTemporaryDirectory();
final outputDir = Directory('${tempDir.path}/extracted_images');
if (!await outputDir.exists()) {
await outputDir.create(recursive: true);
}
final result = await document.extractImages(
directoryPath: outputDir.path,
pages: [0, 1],
);
if (result.success) {
debugPrint('Extracted image count: ${result.count}');
for (final imagePath in result.imagePaths) {
debugPrint('Extracted image: $imagePath');
}
}
The generated image format is determined by the PDF embedded image data and the native SDK. Extracted files may be JPEG, PNG, or another supported image format.
Since v2.6.8
Implementation
Future<CPDFExtractImageResult> extractImages({
required String directoryPath,
List<int>? pages,
}) async {
final result = await _channel.invokeMethod('extract_images', {
'directory_path': directoryPath,
if (pages != null) 'pages': pages,
});
if (result is Map) {
return CPDFExtractImageResult.fromJson(
Map<String, dynamic>.from(result),
);
}
throw Exception(
'extractImages failed: unexpected result type ${result.runtimeType}',
);
}