function getFormatDateWithSep(date, separator) {
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
return `${year}${separator}${month}${separator}${day}`;
}
// 渲染水印 之 九宫格水印
function addRenderWaterMark() {
// 定义水印配置的通用部分
const commonConfig = {
type: "text",
content: `212999/李白\n${getFormatDateWithSep(new Date(), '/')}`,
pageStart: 0,
pageEnd: 5,
watermarkSettings: {
offsetX: 0,
offsetY: 0,
scaleX: 1,
scaleY: 1,
rotation: 45,
opacity: 80
},
watermarkTextProperties: {
font: "SimSun",
fontSize: 50,
color: "#ff0000",
fontStyle: "normal",
lineSpace: 20,
alignment: "center"
}
};
// 定义九宫格的位置
const positions = [
"TopLeft",
"TopCenter",
"TopRight",
"CenterLeft",
"Center",
"CenterRight",
"BottomLeft",
"BottomCenter",
"BottomRight"
];
// 生成九宫格水印配置数组
const watermarkConfigs = positions.map(position => {
return {
...commonConfig,
watermarkSettings: {
...commonConfig.watermarkSettings,
position
}
};
});
pdfui.getPDFDocRender().then((docRender) => {
// 设置水印配置
docRender.setWatermarkConfig(watermarkConfigs);
});
// 重绘 PDF视图
pdfui.redraw(true);
}
//要在文档打开成功或渲染成功的监听事件回调函数中去执行 插入渲染水印的操作
pdfui.addViewerEventListener(Events.openFileSuccess, function () {
openFileError = null
loadingComponentPromise.then(function (component) {
component.close();
});
console.log("openFileSuccess");
addRenderWaterMark();
});