Skip to content

Split Pages

Page splitting allows you to extract specific pages from a document and reorganize them into a new PDF. The steps are as follows:

  1. Specify the page indices to split.
  2. Set the output path for the new PDF document.

Using CPDFReaderWidget:

dart
CPDFReaderWidgetController? _controller;
// Initialize CPDFReaderWidget and get the controller in the onCreated callback
CPDFReaderWidget(
  document: documentPath,
  configuration: CPDFConfiguration(),
  onCreated: (controller) {
    setState(() {
      this._controller = controller;
    });
  },
)

var pages = [0, 1, 2];
var savePath = '/data/user/0/com.compdfkit.flutter.example/cache/temp/PDF_Document.pdf';
// On Android, you can use ComPDFKit.createUri() to generate a Uri
// var savePath = ComPDFKit.createUri('split_document.pdf');
bool splitResult = await _controller.document.splitDocumentPages(savePath, pages);

Using CPDFDocument:

dart
// Create and open the document
CPDFDocument document = await CPDFDocument.createInstance();
var error = await document.open(pdfFilePath);
if (error == CPDFDocumentError.success) {
  var pages = [0, 1, 2];
  var savePath = '/data/user/0/com.compdfkit.flutter.example/cache/temp/PDF_Document.pdf';
  // On Android, you can use ComPDFKit.createUri() to generate a Uri
  // var savePath = ComPDFKit.createUri('split_document.pdf');
  bool splitResult = await document.splitDocumentPages(savePath, pages);
}