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:
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 onCPDFReaderWidgetController
The code for this on Dart looks something like this:
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:
String? uri = await ComPDFKit.createUri('save_as.pdf');
if(uri != null){
bool saveResult = await _controller.document.saveAs(uri, removeSecurity: false, fontSubSet: true);
}
Parameter details:
Name | Type | Description |
---|---|---|
savePath | string | The target path for the "Save As" operation. |
removeSecurity | bool | Whether to remove the document's password Default: false |
fontSubSet | bool | Whether 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:
CPDFReaderWidget(
document: widget.documentPath,
configuration: CPDFConfiguration(),
onCreated: (controller) {
},
onSaveCallback: (){
debugPrint('CPDFDocument: save success');
},
)