Guides
Add Text Watermark
ComPDFKit supports two ways to add text watermarks:
API
Use document.createWatermark to create a text watermark. This method is suitable for scenarios where watermarks need to be generated automatically in code.
dart
late CPDFReaderWidgetController _controller;
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(),
body: Column(children: [
TextButton(onPressed: () async{
await _controller.document.createWatermark(CPDFWatermark.text(
textContent: 'ComPDFKit',
scale: 1.0,
fontSize: 60,
rotation: 0,
horizontalAlignment: CPDFWatermarkHorizontalAlignment.center,
verticalAlignment: CPDFWatermarkVerticalAlignment.center,
textColor: Colors.red,
pages: [0, 1, 2, 3]));
}, child: const Text('Add Text Watermark')),
Expanded(child: CPDFReaderWidget(
document: widget.documentPath,
configuration: CPDFConfiguration(),
onCreated: (controller) {
setState(() {
_controller = controller;
});
},
))
],));
}UI Popup Approach
- Configure default watermark properties in CPDFConfiguration, such as default text, color, font size.
- Call controller.showAddWatermarkView() to open the interactive watermark popup.
dart
// Set default text watermark in configuration
final configuration = CPDFConfiguration(
globalConfig: const CPDFGlobalConfig(
watermark: CPDFWatermarkConfig(
text: 'ComPDFKit-Flutter',
textColor: Colors.red,
textSize: 36,
opacity: 120,
rotation: -45
)
)
);
// Open watermark popup
controller.showAddWatermarkView(
config: CPDFGlobalConfig(
watermark: CPDFWatermarkConfig(
text: 'ComPDFKit-Flutter',
textColor: Colors.red,
textSize: 36,
opacity: 120,
rotation: -45
)
)
);