ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

pdf高亮文本

2021-11-13 16:04:38  阅读:108  来源: 互联网

标签:高亮 ctx length points var pdf 文本


效果图:
在这里插入图片描述

  1. 前端(vue)高亮是在canvas画布上进行操作实现的
  mounted() {
    window.addEventListener('scroll', this.highlight);
  },
 //荧光笔方法
    highlight(){
      const that=this;
      var el = document.getElementById('the-canvas');
      var ctx = el.getContext('2d');
      var canvaswidth = this.canvaswidth;
      var Width = (window.innerWidth - canvaswidth) / 2;
      var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
      var Height = 35;
      //设置绘制线条样式
      // ctx.strokeStyle = 'rgb(250,250,202)';
      ctx.strokeStyle = 'rgb(255,255,0)';
      ctx.globalAlpha = 1;
      ctx.lineWidth = 15;
      ctx.lineJoin = 'round';
      ctx.lineCap = 'round';
      var  Isdraw=this.Isdraw;
      var Erase=this.Iserase;
      var isDrawing;//标记是否要绘制
      //存储坐标点
      let points = [];
      document.body.onpointerdown = function (e) {
        isDrawing = true;
        points.push({x: e.clientX - Width, y: e.clientY - Height + scrollTop});
        this.clientY = e.clientY - Height + scrollTop;

      };
      document.body.onpointermove = function (e) {
        if (isDrawing&&Isdraw===1&&Erase===0) {
          draw(e.clientX - Width, this.clientY);
        }
      };
      //点击点
      document.body.onpointerup = function (e) {
        if (isDrawing&&Isdraw===1) {
          // draw(e.clientX-Width, this.clientY);
        }
        points = [];

        isDrawing = false;
      };

      function draw(mousex, mousey) {
        points.push({x: mousex, y: mousey});
        ctx.globalCompositeOperation = "darken";//Retains the darkest pixels of both layers.
        ctx.beginPath();
        let x = (points[points.length - 2].x + points[points.length - 1].x) / 2,
          y = (points[points.length - 2].y + points[points.length - 1].y) / 2;
        if (points.length === 2) {
          ctx.moveTo(points[points.length - 2].x, points[points.length - 2].y);
          ctx.lineTo(x, y);
        } else {
          let lastX = (points[points.length - 3].x + points[points.length - 2].x) / 2,
            lastY = (points[points.length - 3].y + points[points.length - 2].y) / 2;
          ctx.moveTo(lastX, lastY);
          ctx.quadraticCurveTo(points[points.length - 2].x, points[points.length - 2].y, x, y);
        }
        ctx.stroke();
        points.slice(0, 1);
      }

    },
  1. 前端将高亮过的文本传到后端,后端(springboot)来实现对pdf进行文本高亮,并保存高亮过文本的pdf
    PDF类库:free spire.Pdf.jar 3.9.0
    先引入依赖:
<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.pdf.free</artifactId>
    <version>3.9.0</version>
</dependency>
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>

高亮pdf文本使用方法

public static void main(String[] args) throws Exception {
        //加载PDF源文档
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("test.pdf");

        PdfTextFind[] result1;
        for (Object pageObj : pdf.getPages()) {
            PdfPageBase page =(PdfPageBase)pageObj;
            // 查找跨行文本
            result1 = page.findText("电子邮件", EnumSet.of(TextFindParameter.CrossLine)).getFinds();
            for (PdfTextFind find : result1) {
                //高亮文本
                find.applyHighLight(Color.pink);//指定高亮颜色
                find.getBounds();
            }
        }

        PdfTextFind[] result2;
        for (Object pageObj : pdf.getPages()) {
            PdfPageBase page =(PdfPageBase)pageObj;
            // 查找跨行文本
            result2 = page.findText("心智模型中内在的隐喻", EnumSet.of(TextFindParameter.CrossLine)).getFinds();
            for (PdfTextFind find : result2) {
                //高亮文本
                find.applyHighLight(Color.GREEN);//指定高亮颜色
                find.getBounds();
            }
        }

        //保存文档
        pdf.saveToFile("output.pdf", FileFormat.PDF);
        pdf.dispose();
    }

标签:高亮,ctx,length,points,var,pdf,文本
来源: https://blog.csdn.net/weixin_51751522/article/details/121305549

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有