在选中单元格内包含了背景图,在转换图片过程中会报错,代码如下
首先插入背景图:
var style = new GC.Spread.Sheets.Style();
style.backColor = "lightgreen";
style.backgroundImage = "https://cdn.grapecity.com.cn/online/%E7%AB%8B%E5%8D%B3%E4%B8%8B%E8%BD%BD_spjs.png";
sheet.setStyle(1,1,style,GC.Spread.Sheets.SheetArea.viewport);
然后截图
let oldPrintInfo = window.sheet.printInfo();
let printInfo = new GC.Spread.Sheets.Print.PrintInfo()
//打印质量大于4时才会生成图片
printInfo.qualityFactor(5)
printInfo.margin({
top: 0,
bottom: 0,
left: 0,
right: 0,
header: 0,
footer: 0
});
printInfo.showColumnHeader(GC.Spread.Sheets.Print.PrintVisibilityType.hide)
printInfo.showRowHeader(GC.Spread.Sheets.Print.PrintVisibilityType.hide)
printInfo.paperSize(new GC.Spread.Sheets.Print.PaperSize(GC.Spread.Sheets.Print.PaperKind.a3))
printInfo.showBorder(false)
printInfo.showGridLine(true)
let selection = window.sheet.getSelections()[0]
printInfo.rowStart(selection.row);
printInfo.rowEnd(selection.row + selection.rowCount - 1);
printInfo.columnStart(selection.col);
printInfo.columnEnd(selection.col + selection.colCount - 1);
printInfo.fitPagesTall(1);
printInfo.fitPagesWide(1);
window.sheet.printInfo(printInfo)
window.spread.bind(GC.Spread.Sheets.Events.BeforePrint + ".screenshot", (s, e) => {
console.log(s,e, 'sdfeefee')
let iframe = e.iframe
let imgs = iframe.contentWindow.document.getElementsByTagName("img")
if (imgs && imgs.length) {
let img = imgs[0]
console.log(img)
let canvas = document.createElement("canvas")
canvas.height = img.naturalHeight
canvas.width = img.naturalWidth
let ctx = canvas.getContext('2d')
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.drawImage(img, 0, 0)
console.log( Math.floor(canvas.height / 800), ' Math.floor(canvas.height / 800)')
for (let i = 0; i < Math.floor(canvas.height / 800); i++) {
ctx.font = '100px Arial';
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)'; // 水印颜色和透明度
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.translate(canvas.width / 10, canvas.height / 4);
// ctx.rotate(-Math.atan(canvas.height / canvas.width));
ctx.fillText('songyinglei', 0, 0);
}
canvas.toBlob((blob) => {
resolve(blob)
// navigator.clipboard.write([
// new ClipboardItem({
// [blob.type]: blob
// })
// ]);
})
e.cancel = true
window.spread.unbind(GC.Spread.Sheets.Events.BeforePrint + ".screenshot")
window.sheet.printInfo(oldPrintInfo)
}
})
window.spread.print(window.spread.getActiveSheetIndex());
报错如下:
|