我想從PDF使用pdfbox從所有信息中提取文本。除了顏色,我得到了我想要的所有信息。我嘗試了不同的方式來獲取fontcolor(包括Getting Text Colour with PDFBox)。但不工作。現在我複製了pdfBox的PageDrawer類中的代碼。但是,RGB值也不正確。如何使用pdfbox獲取字體顏色
protected void processTextPosition(TextPosition text) {
Composite com;
Color col;
switch(this.getGraphicsState().getTextState().getRenderingMode()) {
case PDTextState.RENDERING_MODE_FILL_TEXT:
com = this.getGraphicsState().getNonStrokeJavaComposite();
int r = this.getGraphicsState().getNonStrokingColor().getJavaColor().getRed();
int g = this.getGraphicsState().getNonStrokingColor().getJavaColor().getGreen();
int b = this.getGraphicsState().getNonStrokingColor().getJavaColor().getBlue();
int rgb = this.getGraphicsState().getNonStrokingColor().getJavaColor().getRGB();
float []cosp = this.getGraphicsState().getNonStrokingColor().getColorSpaceValue();
PDColorSpace pd = this.getGraphicsState().getNonStrokingColor().getColorSpace();
break;
case PDTextState.RENDERING_MODE_STROKE_TEXT:
System.out.println(this.getGraphicsState().getStrokeJavaComposite().toString());
System.out.println(this.getGraphicsState().getStrokingColor().getJavaColor().getRGB());
break;
case PDTextState.RENDERING_MODE_NEITHER_FILL_NOR_STROKE_TEXT:
//basic support for text rendering mode "invisible"
Color nsc = this.getGraphicsState().getStrokingColor().getJavaColor();
float[] components = {Color.black.getRed(),Color.black.getGreen(),Color.black.getBlue()};
Color c1 = new Color(nsc.getColorSpace(),components,0f);
System.out.println(this.getGraphicsState().getStrokeJavaComposite().toString());
break;
default:
System.out.println(this.getGraphicsState().getNonStrokeJavaComposite().toString());
System.out.println(this.getGraphicsState().getNonStrokingColor().getJavaColor().getRGB());
}
我正在使用上面的代碼。得到的值是 r = 0,g = 0,b = 0,在cosp對象內部值爲[0.0],在pd對象數組內= null和colorSpace = null。而RGB值始終爲-16777216。請幫幫我。提前致謝。
我看到你變黑了,你期待着什麼顏色? –