apple_2021 2021-12-29 15:03 采纳率: 100%
浏览 259
已结题

react-pdf怎么实现操作pdf,例如高亮显示,划线,标注

问题:目前已使用react-pdf 实现上传pdf并分页展示,但是还想编辑pdf,怎么实现呀
代码如下:
import React from 'react'
// 引入组件
import 'antd/dist/antd.css';
// 引入样式
import styles from './index.module.scss'
import 'pdfjs-dist/web/pdf_viewer.css';
import { Input, message, Pagination, Upload } from 'antd'
import { Document, Page, pdfjs } from 'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;
const Dragger = Upload.Dragger;
const { Search } = Input;
interface Props {
}

interface State {
  pdf: string,
  base64:any,
  numPages: number,
  page: number,
  displayPagination: string
}

class BrowserPdf extends React.PureComponent<Props, State> {
  constructor(props: Props){
    super(props)
    this.state = {
      pdf: '',
      base64: '',
      numPages: 1,
      page: 1,
      displayPagination: 'none'
    }
  }

  componentDidMount () {
  }

  readPdf(file: any) {
    // pdf.js无法直接打开本地文件,所以利用FileReader转换
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = (e: any) => {
      let base64 = e.target.result;
      this.setState({
        base64: base64
      })
    }
  }

  selectMenu = (menu:any) => {
    console.log(menu.key)
  }

   //numPages 是pdf文件的总页数
  onLoadSuccess= (param: any) => {
    this.setState({ 
      numPages: param.numPages,
      displayPagination: ''
     });
  };

  pageChange = (page:number, pageSize: number) => {
    console.log(page, pageSize)
    this.setState({
      page
    })
  }

  onSearch = (value: any) => {
    console.log(value);
  }

  render() {
    console.log(this.state)
    const self = this;
    const uploadProps = {
      name: 'file',
      accept: '.pdf',
      action: '',
      onChange(info: any) {
        const status = info.file.status;
        if (status !== 'done') {
          // self.setState({ loading: true });
        }
        if (status !== 'uploading') {
          console.log(info.file, info.fileList);
        }
        if (status === 'done') {
          // self.setState({ loading: false });
          message.success(`${info.file.name} 导入成功`);
          // 解析 pdf 文件
          // self.readPdf(info.file.originFileObj);
          console.log(info.file.originFileObj)
        } else if (status === 'error') {
          console.log(`${info.file.name} 导入失败`);
          self.readPdf(info.file.originFileObj);
        }
      },
    };
    return (
      <div className={`${styles.pdfClass}`}>
        <Dragger {...uploadProps} id="document">
          <p className="ant-upload-text">点击或将文件拖拽到这里上传</p>
          <p className="ant-upload-hint">支持扩展名:.pdf</p>
          <div id='pdf-container' style={{ height: 0, overflow: 'hidden' }}></div>
        </Dragger>
        <Search placeholder="关键字" onSearch={this.onSearch} style={{width: '30%', margin: "20px 0 20px 10% "}} />
        {/* <Input  placeholder='关键字'></Input> */}
        <div className={`${styles.pdfContent}`}>
          <Document
            file={this.state.base64}
            options={{
              cMapUrl: 'https://unpkg.com/pdfjs-dist@2.0.943/cmaps/',
              cMapPacked: true,
              workerSrc: '/pdf.worker.js'
            }}
            renderMode='svg'
            onLoadSuccess={this.onLoadSuccess}
            onItemClick={({ pageNumber }) => console.log('Clicked an item from page ' + pageNumber + '!')}
          >
            {/* {
              new Array(this.state.numPages + 1).fill('').map((item, index) => {
                return <div><Page width={900} key={index} pageNumber={index}/><br/></div>;
              })
            }  */}
            <div><Page width={900} key={1} pageNumber={this.state.page} /><br/></div>
          </Document>
        </div>
        <Pagination
          simple
          defaultCurrent={1} 
          total={this.state.numPages*10} 
          style={{ backgroundColor: "#eee",zIndex: 5, marginLeft: "300px", display: this.state.displayPagination, position: 'fixed', bottom: "30px" }}
          onChange={this.pageChange}
        />
        <br></br>
      </div>
    )
  }
}

  • 写回答

1条回答 默认 最新

  • 查尔斯猫 2021-12-30 15:00
    关注

    在图层上 盖一层 canvas

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月18日
  • 已采纳回答 1月10日
  • 创建了问题 12月29日

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败