79583313

Date: 2025-04-20 11:43:18
Score: 0.5
Natty:
Report link

I finally found a solution for retrieving only the visible part of a PDField.

//Flatten the AcroForm first to remove the PDField but keep the text.
acroForm.flatten()
//Get the page of the document where the text is located.
PDPage page = document.getPage(0);
//Create a PDFTextStripperByArea.
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
//Create a Rectangle2D. All the text which is located within the rectangle will be
//included in the stripper. So put the rectangle excactly above the visible part of
//the text. The params for the coordinates are (x, y, width, height). You likely need
//to experiment a bit to find the right values.
Rectangle2D rectangle2D = new Rectangle2D.Float(50, 525, 500, 140);
//Add the rectangle as a region for the stripper.
stripper.addRegion("region", rectangle2D);
//Extract the region.
stripper.extractRegions(page);
//Get the text.
String text = stripper.getTextForRegion("region");
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: KrabimannBardo