Skip to content

Save Document

Manual save

When using the CPDFReaderWidget component, the document is automatically saved during operations such as sharing, flattening, or adding watermarks. Additionally, you can manually save the document at any desired moment using the CPDFReaderWidgetController. For example:

dart
late CPDFReaderWidgetController _controller;

Scaffold(
  resizeToAvoidBottomInset: false,
  appBar: AppBar(
    leading: IconButton(
      onPressed: () {
        _save();
        Navigator.pop(context);
      },
      icon: const Icon(Icons.arrow_back)),
  ),
  body: CPDFReaderWidget(
    document: widget.documentPath,
    configuration: CPDFConfiguration(),
    onCreated: (controller) {
      setState(() {
        _controller = controller;
      });
    },
  ));

void _save() async {
  bool saveResult = await _controller.document.save();
  debugPrint('ComPDFKit-Flutter: saveResult:$saveResult');
}

Save as

The ComPDFKit SDK for Flutter supports saving a document using the "Save As" feature. After displaying the document with CPDFReaderWidget in Flutter, you can follow these steps to implement the "Save As" functionality:

  • Open the document using CPDFReaderWidget

  • When a custom Flutter button is pressed, call the saveAs API on CPDFReaderWidgetController

The code for this on Dart looks something like this:

dart
late CPDFReaderWidgetController _controller;

@override
Widget build(BuildContext context) {
  return Scaffold(
    resizeToAvoidBottomInset: false,
    appBar: AppBar(),
    body: Column(children: [
      TextButton(onPressed: () async{
        final tempDir = await ComPDFKit.getTemporaryDirectory();
        String savePath = '${tempDir.path}/temp/test_save_as.pdf';
        bool saveResult = await _controller.document.saveAs(savePath, removeSecurity: false, fontSubSet: true);
      }, child: const Text('Save As')),
      Expanded(child: CPDFReaderWidget(
        document: widget.documentPath,
        configuration: CPDFConfiguration(),
        onCreated: (controller) {
          setState(() {
            _controller = controller;
          });
        },
      ))
    ],));
}

On the Android platform, the saveAs() method also supports saving to a specified directory via a Uri. As shown in the following example, the document will be saved to the Downloads folder in the device's public directory:

dart
String? uri = await ComPDFKit.createUri('save_as.pdf');
if(uri != null){
  bool saveResult = await _controller.document.saveAs(uri, removeSecurity: false, fontSubSet: true);
}

Parameter details:

NameTypeDescription
savePathstringThe target path for the "Save As" operation.
removeSecurityboolWhether to remove the document's password
Default: false
fontSubSetboolWhether to include a font subset in the document. This parameter affects how the PDF displays in other software. If the required fonts are unavailable in other software, the text may not display properly.
Default: true

Save Callback

When creating a CPDFReaderWidget, you can set a save listener to handle save events. For example:

dart
CPDFReaderWidget(
  document: widget.documentPath,
  configuration: CPDFConfiguration(),
  onCreated: (controller) {

  },
  onSaveCallback: (){
    debugPrint('CPDFDocument: save success');
  },
)