You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PDFBox:如何在文本提取器中过滤彩色背景上的白色文本

Filtering White Text in PDF Extraction with Apache PDFBox

If you’re dealing with complex PDFs that include white text (which often adds unwanted noise to extraction results), overriding the processTextPosition method in your PDFTextStripper subclass is a reliable way to exclude those white characters. Here’s a practical implementation:

First, define the RGB value for pure white—since 24-bit RGB uses integers, white translates to 16777215 (equivalent to hex 0xFFFFFF). Then, in the overridden method, we check the non-stroking color (this is typically the fill color used for text) of the current text position. Only if the color isn’t white do we proceed with the default text processing logic.

private static final int COLOR_WHITE = 16777215;

@Override
protected void processTextPosition(TextPosition text) {
    PDGraphicsState gs = getGraphicsState();
    PDColor nonStrokingColor = gs.getNonStrokingColor();
    try {
        // Skip processing if the text color is white
        if (nonStrokingColor.toRGB() != COLOR_WHITE) {
            super.processTextPosition(text);
        }
    } catch (IOException e) {
        logger.error("Could not convert non-stroking color to RGB", e);
    }
}

Key Details to Keep in Mind:

  • Non-Stroking vs. Stroking Color: We use getNonStrokingColor() because most standard text is filled rather than stroked (outlined). If your PDFs contain stroked text, you might want to add a check for getStrokingColor() too.
  • Error Handling: The toRGB() method can throw an IOException, so wrapping it in a try-catch prevents your extraction process from crashing if there’s a color conversion issue.
  • White RGB Value: 16777215 is the correct integer representation of full-intensity red, green, and blue combined (255 each), which makes pure white.

This method effectively filters out white text during extraction, cleaning up your output and eliminating noise from hidden or overlayed white characters.

内容的提问来源于stack exchange,提问作者D.F. Stones

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:57:05