Skip to content
Guides

注释扁平化

注释扁平化是指将可编辑的注释转换为不可编辑、不可修改的静态图像或纯文本形式。当对注释进行扁平化时,整个文档所有可编辑元素(包含注释和表单)都将执行扁平化,所以注释扁平化又称为文档扁平化。

以下是注释扁平化的示例代码:

javascript
let docViewer = null
ComPDFKitViewer.init({
  pdfUrl: 'Your PDF Url',
  license: 'Input your license here',
  // 其他设置
}, viewer)
  .then((core) => {
    docViewer = core.docViewer;
    docViewer.addEvent('documentloaded', () => {
      console.log('ComPDFKit Web Demo loaded');
    })
  });

const downloadBtn = document.getElementById('downloadBtn')
downloadBtn.addEventListener('click', async () => {
  const docStream = await docViewer.flattenPdf();
  const docBlob = new Blob([docStream], { type: 'application/pdf' });
});

保存到本地文件

javascript
const fileName = "document.pdf";

const url = URL.createObjectURL(docBlob);
const a = document.createElement("a");
a.href = url;
a.style = "display: none";
a.download = fileName;
document.body.appendChild(a);
a.click();
URL.revokeObjectURL(url);
document.body.removeChild(a);

保存到服务器

javascript
const fileName = 'document.pdf';

const data = new FormData();
data.append('file', docBlob, fileName);

const res = await fetch('https://example.com/api/test', {
  method: 'POST',
  body: data,
});