Skip to content

Insert Pages

You can insert blank pages or pages from another PDF into the target document.

Insert a Blank Pages

Here is an example of inserting a blank page:

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;
    });
  },
)

CPDFPageSize pageSize = CPDFPageSize.a4;
// Or use a custom page size:
// CPDFPageSize.custom(500, 800);
bool insertResult = await _controller.document.insertBlankPage(pageIndex: 0, pageSize = pageSize);

Using CPDFDocument:

dart
// Create and open the document
CPDFDocument document = await CPDFDocument.createInstance();
var error = await document.open(pdfFilePath);
if (error == CPDFDocumentError.success) {
  CPDFPageSize pageSize = CPDFPageSize.a4;
  // Or use a custom page size:
  // CPDFPageSize.custom(500, 800);
  bool insertResult = await document.insertBlankPage(pageIndex: 0, pageSize = pageSize);
}

Insert Pages from Another PDF

Here is an example of inserting pages from another 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;
    });
  },
)

final filePath = '/data/user/0/com.compdfkit.flutter.example/cache/temp/PDF_Document.pdf';
final pages = [0, 1, 2]; // Pages to import from the source document
final insertPosition = 0; // Insert at the beginning of the document
final result = await _controller.document.importDocument(
  filePath: filePath,
  pages: pages,
  insertPosition: insertPosition,
);

Using CPDFDocument:

dart
// Create and open the document
CPDFDocument document = await CPDFDocument.createInstance();
var error = await document.open(pdfFilePath);
if (error == CPDFDocumentError.success) {
  final filePath = '/data/user/0/com.compdfkit.flutter.example/cache/temp/PDF_Document.pdf';
  final pages = [0, 1, 2]; // Pages to import from the source document
  final insertPosition = 0; // Insert at the beginning of the document
  final result = await document.importDocument(
    filePath: filePath,
    pages: pages,
    insertPosition: insertPosition,
  );
}