Skip to content

拆分页面

拆分页面可以将当前文档指定的页码拆分重组为一份新的PDF文档,以下是拆分页面的步骤:

  1. 指定要拆分的页码
  2. 指定拆分后的页面PDF文档保存路径

使用CPDFReaderWidget

dart
CPDFReaderWidgetController? _controller;
// 初始化 CPDFReaderWidget,并在 onCreated 回调中获取 controller
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';
// 在 Android 上,您可以使用 ComPDFKit.createUri() 来创建 Uri
// var savePath = ComPDFKit.createUri('split_document.pdf');
bool splitResult = await _controller.document.splitDocumentPages(savePath, pages);

使用CPDFDocument

dart
// 创建并打开文档
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';
  // 在 Android 上,您可以使用 ComPDFKit.createUri() 来创建 Uri
  // var savePath = ComPDFKit.createUri('split_document.pdf');
  bool splitResult = await document.splitDocumentPages(savePath, pages);
}