You need to enable JavaScript to run this app. 如何在ng2-pdfjs-viewer中配置PDFViewerApplication添加自定义工具栏链接
优惠活动
大模型
产品
解决方案
定价
更多

如何在ng2-pdfjs-viewer中配置PDFViewerApplication添加自定义工具栏链接

方案1:通过PDFViewerApplication/PDFViewerApplicationOptions直接注入自定义工具栏项

该方案不需要改动底层组件,直接操作pdfjs暴露的全局对象注入自定义按钮,适配性最强。

前置配置

首先在你的组件ts文件顶部声明全局变量,避免Typescript类型报错:

declare var PDFViewerApplication: any;
declare var PDFViewerApplicationOptions: any;

然后检查angular.json的scripts配置,确保已经导入pdfjs的核心和viewer脚本:

"scripts": [
  "node_modules/pdfjs-dist/build/pdf.min.js",
  "node_modules/pdfjs-dist/web/viewer.js"
]

核心实现代码

在组件的ngAfterViewInit生命周期中操作,确保pdf实例已经加载完成:

import { Component, ViewChild, AfterViewInit } from '@angular/core';
import { PdfJsViewerComponent } from 'ng2-pdfjs-viewer';

@Component({
  selector: 'app-pdf-preview',
  templateUrl: './pdf-preview.component.html',
  styleUrls: ['./pdf-preview.component.css']
})
export class PdfPreviewComponent implements AfterViewInit {
  @ViewChild('pdfViewer') pdfViewer!: PdfJsViewerComponent;
  pdfSrc = '你的PDF文件地址';

  ngAfterViewInit(): void {
    // 监听pdf viewer初始化完成事件,所有操作必须在这个回调内执行
    this.pdfViewer.onPDFViewerApplicationInitialized.subscribe(() => {
      // 关闭沙箱模式,允许操作工具栏DOM
      PDFViewerApplicationOptions.set('disableSandbox', true);
      // 启用自定义工具栏配置
      PDFViewerApplicationOptions.set('toolbarConfig', {
        allowCustomElements: true
      });

      // 获取原有工具栏容器
      const toolbar = PDFViewerApplication.appConfig.toolbar.viewerContainer.querySelector('.toolbar');
      
      // 新增「联系我们」按钮
      const contactBtn = document.createElement('button');
      contactBtn.className = 'toolbarButton'; // 继承原有工具栏按钮样式
      contactBtn.title = '联系我们';
      contactBtn.textContent = '联系我们';
      contactBtn.addEventListener('click', () => {
        // 自定义跳转逻辑,比如跳转到站内联系页、弹出咨询模态框
        window.location.href = '/contact-us';
      });

      // 新增「跳转外部站点」按钮
      const externalBtn = document.createElement('button');
      externalBtn.className = 'toolbarButton';
      externalBtn.title = '访问官网';
      externalBtn.textContent = '访问官网';
      externalBtn.addEventListener('click', () => {
        window.open('你的站点地址', '_blank');
      });

      // 可以自定义插入位置,比如插到下载按钮后方,这里默认插到工具栏末尾
      toolbar.appendChild(contactBtn);
      toolbar.appendChild(externalBtn);

      // 刷新工具栏渲染
      PDFViewerApplication.toolbar.update();
    });
  }
}

对应的组件模板代码:

<ng2-pdfjs-viewer #pdfViewer 
  [pdfSrc]="pdfSrc"
  [externalWindow]="false"
  [download]="true"
  [print]="true">
</ng2-pdfjs-viewer>

方案2:自定义替换整套工具栏(更高可控度)

如果需要对工具栏做深度改造,不需要默认的部分功能,可以直接隐藏原生工具栏,自己实现全套工具栏:

  1. 先在组件样式中隐藏原生工具栏:
::ng-deep .toolbar {
  display: none !important;
}
  1. 自己编写工具栏HTML结构,原有PDF操作直接调用PDFViewerApplication暴露的API即可:
  • 翻页:PDFViewerApplication.page = 目标页码
  • 缩放:PDFViewerApplication.currentScale = 缩放比例
  • 下载:PDFViewerApplication.download()
  • 打印:PDFViewerApplication.print()
  • 自定义按钮直接绑定你需要的跳转、弹窗逻辑即可

注意事项
  • 如果初始化时找不到PDFViewerApplication对象,可以加500ms的setTimeout做兜底,避免加载顺序问题
  • 自定义按钮的样式可以通过覆盖toolbarButton类的样式调整,和原生按钮风格保持一致

内容的提问来源于stack exchange,提问作者Manikandan Thangaraj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 22:15:06