package com.simuwang.daq.components;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.TextPosition;
import org.apache.pdfbox.util.Matrix;

import java.io.IOException;
import java.util.List;

/**
 * @author wangzaijun
 * @date 2024/9/12 14:00
 * @description 自定义的文本去水印方法，发现水印基本是旋转文字并且比报告内其他文字都大
 */
public class CustomPDFTextStripper extends PDFTextStripper {
    private final float[] watermarkWidth = {0f};

    @Override
    protected void writeString(String text, List<TextPosition> textPositions) throws IOException {
        List<String> newTexts = ListUtil.list(false);
        for (TextPosition textPosition : textPositions) {
            Matrix textMatrix = textPosition.getTextMatrix();
            float col = textMatrix.getValue(0, 1);
            float width = textPosition.getWidth();
            if (col == 0.) {
                if (width < watermarkWidth[0]) {
                    newTexts.add(textPosition.getUnicode());
                }
            } else {
                if (width > watermarkWidth[0]) {
                    watermarkWidth[0] = width;
                }
                newTexts.add("++");
            }
        }
        if (CollUtil.isNotEmpty(newTexts)) {
            super.writeString(String.join("", newTexts));
        }
    }
}
