【问题标题】:Remove Large Tokens from PDF using PDFBox or equivalent library使用 PDFBox 或等效库从 PDF 中删除大标记
【发布时间】:2020-07-26 21:26:40
【问题描述】:

我的 PDF:s 在许多 pdf 文档的整个首页上都贴有一个非常大的标记,请参见图片。我正在寻找一种自动化的方法来删除这些。

Apache PDFBox 有一个相当广泛的 API,有什么方法可以通过正则表达式匹配这些令牌并简单地删除它们并重新保存 pdf?

来自下面发布的 PDF 示例的图像。我要删除的标记是:[KS/2019:589] Lokalvård Grundskolor & Idrottshallar,贴在常规文本之上。 Google Drive link to full PDF-file

【问题讨论】:

    标签: java parsing pdf pdfbox


    【解决方案1】:

    您可以像这样使用this answer 中的PdfContentStreamEditor 类(不要忘记应用答案底部提到的修复程序):

    try (   PDDocument document = ...   ) {
        PDPage page = document.getPage(0);
        PdfContentStreamEditor editor = new PdfContentStreamEditor(document, page) {
            @Override
            protected void write(ContentStreamWriter contentStreamWriter, Operator operator, List<COSBase> operands) throws IOException {
                String operatorString = operator.getName();
    
                if (TEXT_SHOWING_OPERATORS.contains(operatorString))
                {
                    float fs = getGraphicsState().getTextState().getFontSize();
                    Matrix matrix = getTextMatrix().multiply(getGraphicsState().getCurrentTransformationMatrix());
                    Point2D.Float transformedFsVector = matrix.transformPoint(0, fs);
                    Point2D.Float transformedOrigin = matrix.transformPoint(0, 0);
                    double transformedFs = transformedFsVector.distance(transformedOrigin);
                    if (transformedFs > 50)
                        return;
                }
    
                super.write(contentStreamWriter, operator, operands);
            }
    
            final List<String> TEXT_SHOWING_OPERATORS = Arrays.asList("Tj", "'", "\"", "TJ");
        };
        editor.processPage(page);
        document.save(...);
    }
    

    (EditPageContent 测试testRemoveBigTextKommersAnnonsElite)

    您可以在参考答案中找到一些解释。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-21
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      • 1970-01-01
      相关资源
      最近更新 更多