【问题标题】:Is there any way to add outline to text with destinations, so that it can viewed in getOutline of ng2-pdf-viewer有什么方法可以在带有目的地的文本中添加大纲,以便可以在 ng2-pdf-viewer 的 getOutline 中查看
【发布时间】:2020-07-27 07:44:54
【问题描述】:

使用 ng2-pdf-viewer 显示轮廓,其中使用 pdfmake 库生成 pdf。在定义文档定义时,是否有任何选项可以在 pdfmake 中将一些文本作为大纲文本?

【问题讨论】:

    标签: javascript pdfmake ng2-pdfjs-viewer


    【解决方案1】:

    如果我们知道需要概述的标题。为此功能实现的解决方案是使用 Toc(目录)

    像这样定义文档定义:

    generatePDF(): Promise<any>{
      this.documentDefinition = {
        content: [
         {
          tocItem: 'coverpage',
          stack: [
            {
              columns: [
                {
                  text: '',
                  width: '50%',
                  tocItem: true,
                },
                {
                  width: '*',
                  text: ''
                }
              ]
            }
          ]
          pageBreak: 'after',
        },
        {
          text: 'Second toc header',
          tocItem: true,
          pageBreak: 'after',
          bold: true,
        }
       ]
    }
    // generate 
    this.generatedPDF = pdfMake.createPdf(this.documentDefinition)
    this.generatedPDF.getBuffer((buffer) => {
        this.pdfSrc = buffer
    })
    }
    

    加载 ng2-pdf-viewer 后开启:

    afterLoadComplete(pdf: PDFDocumentProxy): void {
       this.pdf = pdf
       const toc = this.generatedPDF.docDefinition.content.filter( content => 
       content.toc)
       this.loadOutline(toc)
    }
    
    loadOutline(toc: any): void {
      const outlineTitles = [
        'item 1',
        'item 2',
        'item 3',
        'item 4'
      ]
      this.pdf.getDestinations().then((outline: any[]) => {
         this.outline = Object.entries(outline).map( (item, index) => 
         Object.assign(item, {title : outlineTitles[index]}))
      })
    }
    

    这样就会得到带有目的地的标题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-07
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-25
      • 2015-08-09
      相关资源
      最近更新 更多