获取PDFBox中的文字颜色

Get text color in PDFBox

我正在使用 PDFBox 解析 PDF,并尝试获取文本颜色。使用 TextPosition 属性,我可以毫无问题地获取字体、大小和位置等其他属性。我是这样做的:

@Override
protected void writeString (String string, List<TextPosition> textPositions) {

    for (TextPosition textPosition : textPositions) {

        System.out.println(textPosition.getFont());
        System.out.println(textPosition.getFontSizeInPt());
        System.out.println(textPosition.getXDirAdj() + ", " + textPosition.getYDirAdj());

    }

但是,我无法检索文本的颜色。我已经搜索了 Google 的解决方案,但到目前为止没有任何效果。我看到的每个教程似乎都在使用旧版本的 PDFBox。我没有这些人正在使用的几种方法。例如,在一个 SO 问题中,他们建议使用此代码:

@Override
protected void processTextPosition(TextPosition text) {

    try {
        PDGraphicsState graphicsState = getGraphicsState();
        System.out.println("R = " + graphicsState.getNonStrokingColor().getJavaColor().getRed());
        System.out.println("G = " + graphicsState.getNonStrokingColor().getJavaColor().getGreen());
        System.out.println("B = " + graphicsState.getNonStrokingColor().getJavaColor().getBlue());
    }

    catch (IOException ioe) {}

}

当我尝试使用它时,IntelliJ 告诉我 "getJavaColor()" 未定义。我也试过这个代码:

@Override
protected void processTextPosition(TextPosition text) {

    try {
        PDGraphicsState graphicsState = getGraphicsState();
        System.out.println("R = " + graphicsState.getNonStrokingColor().toRGB());
    }
    catch (IOException ioe) {System.out.println(ioe); }

}

并且,虽然该方法按预期被调用,并且按预期次数调用,但它始终打印 0,即使在我的 PDF 文件中我有黑色文本和红色文本。

这是我的 Maven 依赖项:

<dependencies>

    <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox</artifactId>
        <version>2.0.17</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox -->
    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>fontbox</artifactId>
        <version>2.0.17</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox-tools -->
    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox-tools</artifactId>
        <version>2.0.17</version>
    </dependency>

</dependencies>

感谢任何帮助

显然在 PDFBox 2.0.0+ 版本中您需要添加以下代码行:

addOperator(new SetStrokingColorSpace());
addOperator(new SetNonStrokingColorSpace());
addOperator(new SetStrokingDeviceCMYKColor());
addOperator(new SetNonStrokingDeviceCMYKColor());
addOperator(new SetNonStrokingDeviceRGBColor());
addOperator(new SetStrokingDeviceRGBColor());
addOperator(new SetNonStrokingDeviceGrayColor());
addOperator(new SetStrokingDeviceGrayColor());
addOperator(new SetStrokingColor());
addOperator(new SetStrokingColorN());
addOperator(new SetNonStrokingColor());
addOperator(new SetNonStrokingColorN());

到您的 PDFTextStripper 覆盖 class 构造函数。现在,如果您使用:

@Override
protected void processTextPosition (TextPosition textPosition) {

    try {

        PDGraphicsState graphicsState = getGraphicsState();
        System.out.println(graphicsState.getNonStrokingColor().toRGB());

    }

    catch (Exception ioe) {}

}

它实际上打印了一个真实的值。