0

want to get the white text background color but incorrect, and the text RGB is incorrect 0,0,0, that should (255,255,255);
how to get the white text and backgroud color?

demo pdf

enter image description here

how to get the white text and backgroud color?

override def processTextPosition(text: TextPosition): Unit = {
  super.processTextPosition(text)
  val color: PDColor = getGraphicsState.getNonStrokingColor
  val components: Array[Float] = color.getComponents
  val r: Int = Math.round(components(0) * 255)
  val g: Int = Math.round(components(1) * 255)
  val b: Int = Math.round(components(2) * 255)
  linePos.append((r, g, b))
  if(r!=0||g!=0||b!=0) bad.append(text.getUnicode)



  val color2: PDColor = getGraphicsState.getStrokingColor
  val components2: Array[Float] = color2.getComponents
  if (components2.size >= 3){
    val r2: Int = Math.round(components2(0) * 255)
    val g2: Int = Math.round(components2(1) * 255)
    val b2: Int = Math.round(components2(2) * 255)
    if (r2 != 0 || g2 != 0 || b2 != 0) bad.append(text.getUnicode)
  }
}
mkl
  • 90,588
  • 15
  • 125
  • 265
  • addOperator(new SetStrokingColorSpace) addOperator(new SetNonStrokingColorSpace) addOperator(new SetStrokingColor) addOperator(new SetNonStrokingColor) addOperator(new SetStrokingColorN) addOperator(new SetNonStrokingColorN) addOperator(new SetStrokingDeviceGrayColor) addOperator(new SetNonStrokingDeviceGrayColor) addOperator(new SetStrokingDeviceRGBColor) addOperator(new SetNonStrokingDeviceRGBColor) addOperator(new SetStrokingDeviceCMYKColor) addOperator(new SetNonStrokingDeviceCMYKColor) – li peng Apr 04 '23 at 07:12
  • 1
    Unclear what you're asking here, is this the PrintTextColors example in a different programming language? That one can't tell the text background, only the font stroking / non-stroking color. There is no concept of "text background" like in WORD. There may be an image or a shape behind the text, that's all. – Tilman Hausherr Apr 04 '23 at 08:30
  • it is scala. My question is how to get the color of the white text and the background color behind the text, such as the white text on a blue background in the screenshot, need to get the background color as blue and the text color as white. If use the shape behind the text, how to get the shape color? – li peng Apr 04 '23 at 12:29
  • 1
    Either you render the PDF without the text (by extending the text painting stuff and replace it with dummy) and look outside of the "text", or you collect the shapes similar to this question: https://stackoverflow.com/questions/38931422/ (this one is about collecting lines, but you can adjust it to collect shapes) – Tilman Hausherr Apr 04 '23 at 13:23
  • 1
    There is nothing like a "background color" attribute to text. Background is what had been drawn before. Thus, you'll only be able to query the color of the text itself. To get the background, you have to parse and interpret everything that has been drawn before the text and consider what everything drawn thereafter may do to it. – mkl Apr 04 '23 at 13:23
  • 1
    Furthermore, you simply assume you have RGB colors (in the stroking color case you at least check for at least 3 components) but there might also be colors in completely different color spaces, e.g. CMYK, L\*a\*b\*, or spot colors. Thus, without the PDF we can only speculate whether the *0,0,0* you observed is correct or not. – mkl Apr 04 '23 at 13:32
  • Thank you for the guidance, it was my mistake, my color space is indeed cmyk. Now I can get the correct color of the text. But what you said "To get the background, you have to parse and interpret everything that has been drawn before the text and consider what everything drawn thereafter may do to it", can you give some tips? – li peng Apr 06 '23 at 02:39

0 Answers0