添加文本水印
ComPDFKit 支持 两种方式 添加文本水印:
编程方式(API 调用)
通过 document.createWatermark 创建文本水印,适合需要在代码中自动生成水印的场景。
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('添加文本水印')),
Expanded(child: CPDFReaderWidget(
document: widget.documentPath,
configuration: CPDFConfiguration(),
onCreated: (controller) {
setState(() {
_controller = controller;
});
},
))
],));
}UI 弹窗方式
- 在 CPDFConfiguration 中配置水印默认样式,例如默认文本、颜色、字体大小。
- 调用
controller.showAddWatermarkView()打开添加水印的弹窗界面。
dart
// 在配置中设置默认文本水印样式
final configuration = CPDFConfiguration(
globalConfig: const CPDFGlobalConfig(
watermark: CPDFWatermarkConfig(
text: 'ComPDFKit-Flutter',
textColor: Colors.red,
textSize: 36,
opacity: 120,
rotation: -45
)
)
);
// 打开添加水印弹窗
controller.showAddWatermarkView(
config:
CPDFGlobalConfig(
watermark: CPDFWatermarkConfig(
text: 'ComPDFKit-Flutter',
textColor: Colors.red,
textSize: 36,
opacity: 120,
rotation: -45
)
)
);