createNewTextArea method

Future<bool> createNewTextArea({
  1. required int pageIndex,
  2. required String content,
  3. required Offset offset,
  4. double? maxWidth,
  5. CPDFEditorTextAttr attr = const CPDFEditorTextAttr(),
})

Inserts a new text area on the specified page.

This method creates a new text box within the page's coordinate system and writes the given text content into it. 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 text will be inserted, starting from 0.
  • content: The text content to be inserted.
  • offset: The position of the text area’s bottom-left corner in PDF page coordinates (unit: point).
  • maxWidth: The maximum width of the text area. Text will automatically wrap when exceeding this width. If null, the width is not constrained.
  • attr: Text attributes, such as font size, color, alignment, etc. Defaults to the standard CPDFEditorTextAttr configuration.

Returns:

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

Platform Requirements:

  • This method is only supported on Android. Calling it on other platforms will throw an UnsupportedError.

Example:

bool result = await document.createNewTextArea(
      pageIndex: 0,
      content: 'ComPDFKit insert text This is Test create text Area ...',
      offset: const Offset(50, 800),
      maxWidth: 300,
      attr: const CPDFEditorTextAttr(
        fontSize: 18,
        fontColor: Colors.red,
        alignment: CPDFAlignment.left,
      ),
);

Implementation

Future<bool> createNewTextArea({
  required int pageIndex,
  required String content,
  required Offset offset,
  double? maxWidth,
  CPDFEditorTextAttr attr = const CPDFEditorTextAttr(),
}) async {
  if (defaultTargetPlatform != TargetPlatform.android) {
    throw UnsupportedError(
        'createNewTextArea() is only supported on Android platform.');
  }
  return await _channel.invokeMethod(
    'create_new_text_area',
    {
      'page_index': pageIndex,
      'content': content,
      'x': offset.dx,
      'y': offset.dy,
      'max_width': maxWidth,
      'attr': attr.toJson(),
    },
  );
}