createNewImageArea method

Future<bool> createNewImageArea({
  1. required int pageIndex,
  2. required CPDFImageData imageData,
  3. required Offset offset,
  4. double width = 200,
})

Inserts a new image area on the specified page.

This method creates a new image box within the page's coordinate system. The coordinate origin (0, 0) is located at the bottom-left corner of the page.


Parameters:

  • pageIndex: The index of the target page where the image will be inserted, starting from 0.
  • imageData: The image data to be inserted. Supports multiple formats:
    • File path: CPDFImageData.fromPath('/path/to/image.png')
    • Base64: CPDFImageData.fromBase64('iVBORw0KGgo...')
    • Asset: CPDFImageData.fromAsset('assets/images/logo.png')
    • Uri (Android): CPDFImageData.fromUri('content://...')
  • offset: The position of the image area's bottom-left corner in PDF page coordinates (unit: point).
  • width: The width of the image area. Height is calculated automatically based on aspect ratio. If null, uses original image width.

Returns:

  • true if the image area is successfully created.
  • false if the operation fails.

Example:

// From file path
bool result1 = await document.createNewImageArea(
  pageIndex: 0,
  imageData: CPDFImageData.fromPath('/path/to/image.png'),
  offset: const Offset(50, 700),
  width: 200,
);

// From base64
bool result2 = await document.createNewImageArea(
  pageIndex: 1,
  imageData: CPDFImageData.fromBase64('iVBORw0KGgoAAAANSUhEUgAAAAUA...'),
  offset: const Offset(100, 600),
  width: 150,
);

// From asset
bool result3 = await document.createNewImageArea(
  pageIndex: 2,
  imageData: CPDFImageData.fromAsset('logo.png'),
  offset: const Offset(200, 500),
  width: null, // Use original width
);

Implementation

Future<bool> createNewImageArea({
  required int pageIndex,
  required CPDFImageData imageData,
  required Offset offset,
  double width = 200,
}) async {
  if (defaultTargetPlatform != TargetPlatform.android) {
    throw UnsupportedError(
        'createNewTextArea() is only supported on Android platform.');
  }
  try {
    return await _channel.invokeMethod('create_new_image_area', {
      'page_index': pageIndex,
      'image_data': imageData.toJson(),
      'x': offset.dx,
      'y': offset.dy,
      'width': width,
    });
  } on PlatformException catch (e) {
    debugPrint('Error creating image area: ${e.message}');
    return false;
  }
}