Java PDFBox 添加高亮注释 PDF高亮

2023-11-19  本文已影响0人  都是自己人_

1. 引入pom文件

    <dependency>
      <groupId>org.apache.pdfbox</groupId>
      <artifactId>pdfbox</artifactId>
      <version>2.0.28</version>
    </dependency>

2. 创建坐标点实体

通过四个点,确定一个高亮区域,创建一个实体方便输入坐标。

public class PositionPoint {

    private float[] leftLower;
    private float[] rightLower;
    private float[] leftTop;
    private float[] rightTop;

    public PositionPoint(){

    }

    public float[] getLeftLower() {
        return leftLower;
    }

    public PositionPoint setLeftLower(float x,float y) {
        this.leftLower = new float[]{x, y};
        return this;
    }

    public float[] getRightLower() {
        return rightLower;
    }

    public PositionPoint setRightLower(float x,float y) {
        this.rightLower = new float[]{x, y};
        return this;
    }

    public float[] getLeftTop() {
        return leftTop;
    }

    public PositionPoint setLeftTop(float x,float y) {
        this.leftTop = new float[]{x, y};
        return this;
    }

    public float[] getRightTop() {
        return rightTop;
    }

    public PositionPoint setRightTop(float x,float y) {
        this.rightTop = new float[]{x, y};
        return this;
    }
}

3. 通过PDFBox设置高亮区域

添加设置PDF高亮区域分为以下几个步骤

示例代码如下:

 public static void main( String[] args )
    {
        try {
            String inputPath = "your PDF input file path";
            String outputPath = "your PDF output file path";

            //创建需要添加高亮的区域块坐标
            List<PositionPoint> groupPoint = new ArrayList<>();
            PositionPoint positionPoint1 = new PositionPoint();
            positionPoint1.setLeftLower(100,100);
            positionPoint1.setLeftTop(100,200);
            positionPoint1.setRightLower(200,100);
            positionPoint1.setRightTop(200,200);
            PositionPoint positionPoint2 = new PositionPoint();
            positionPoint2.setLeftLower(300,100);
            positionPoint2.setLeftTop(300,200);
            positionPoint2.setRightLower(500,100);
            positionPoint2.setRightTop(500,200);
            groupPoint.add(positionPoint1);
            groupPoint.add(positionPoint2);

            float[] pointArray = new float[groupPoint.size()*8];
            int index = 0;
            for (int k = 0; k < groupPoint.size(); k++) {
                PositionPoint point = groupPoint.get(k);
                pointArray[index++] =  point.getLeftLower()[0];
                pointArray[index++] =  point.getLeftLower()[1];
                pointArray[index++] =  point.getRightLower()[0];
                pointArray[index++] =  point.getRightLower()[1];
                pointArray[index++] =  point.getLeftTop()[0];
                pointArray[index++] =  point.getLeftTop()[1];
                pointArray[index++] =  point.getRightTop()[0];
                pointArray[index++] =  point.getRightTop()[1];
            }

            //读取PDF文件
            PDDocument document = PDDocument.load(new File(inputPath));

            //获取添加注释的页对象
            PDPage page = document.getPage(0);

            //获取页对象中已包含的注释集合
            List<PDAnnotation> annotations = page.getAnnotations();
            if (annotations == null) {
                annotations = new ArrayList<>();
                page.setAnnotations(annotations);
            }

            //创建文本标记注释,并设置标记类型为高亮
            PDAnnotationTextMarkup textMarkupAnnotation = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);

            //设置PDF矩形大小
            textMarkupAnnotation.setRectangle(PDRectangle.A4);

            //设置注释的位置点
            textMarkupAnnotation.setQuadPoints(pointArray);

            //设置注释的颜色
            textMarkupAnnotation.setColor(new PDColor(new float[]{0, 0, 0}, PDDeviceRGB.INSTANCE));

            //添加创建的文本标记注释
            annotations.add(textMarkupAnnotation);

            //输出流
            document.save(outputPath);

            //关闭流
            document.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

这段示例代码是在一个注释中添加了两个区域的黑色高亮块,可以通过修改PDColor中的RGB值控制高亮颜色;如需要添加多个高亮注释可以创建多个PDAnnotationTextMarkup类,设置属性并加入到注释集合中;另外PDFBox并不支持直接在文本上添加高亮,需要获取到对应文本的位置,计算坐标来添加高亮,或者有更好的方案小伙伴欢迎一起讨论!

上一篇下一篇

猜你喜欢

热点阅读