PdfBox-Android icon indicating copy to clipboard operation
PdfBox-Android copied to clipboard

When draw an “X” to the contentStream it stays below the content (CheckBox)

Open enriquebautista opened this issue 1 year ago • 2 comments

Describe the bug I need when the user taps on the screen to “draw” an X where he clicked on the pdf, in my case, it is in the checkBoxes, but my pdf reader does not interact with the checkBoxes, I am just trying to draw an X on top of the pdf. With pdfBox I have managed to edit the pdf and save the changes, but to my surprise the red “X” exists but when there is a “checkbox” right on the “X” , the “X” is under the checkBox. Any idea how to fix it ? If I add an annotation by drawing lines it does work, but I don't really want an annotation (since it can be edited).

To reproduce Code snippet to reproduce the behavior:

val document = PDDocument.load(pdfFile)

for ((pageIndex, pointF) in tappedPositions) {
    val page = document.getPage(pageIndex)
    val x = pointF.x
    val y = pointF.y

val contentStream = PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true)
contentStream.setStrokingColor(0f, 1f, 0.65f, 0.08f)
contentStream.setLineWidth(2f)

// Draw X
contentStream.moveTo(x - 10, y - 10)
contentStream.lineTo(x + 10, y + 10)
contentStream.moveTo(x + 10, y - 10)
contentStream.lineTo(x - 10, y + 10)
contentStream.stroke()
contentStream.close()
}

document.save(newPdfFile)

PDF example https://drive.google.com/file/d/1q5mw9XfATXOtjtEpOQHiRbX6HWwm2DDk/view?usp=sharing

Expected behavior What I expect is that the “X” will be drawn on top of any element in the PDF.

Actual behavior Currently the “X” is below the checkBoxes.

Environment details:

  • PdfBox-Android version: 2.0.27.0
  • Android API version: 33 (Android 13)

enriquebautista avatar Feb 13 '25 15:02 enriquebautista

Update: I found an solution: set pdf flatten before draw X.

enriquebautista avatar Feb 14 '25 19:02 enriquebautista

the correct solution would be to use the acroform API. Your attachment link goes to a PNG file, not a PDF. Btw your code uses the 4 parameter constructor of PDPageContentStream, and you should use the 5 parameter constructor.

THausherr avatar Mar 11 '25 12:03 THausherr