Hi...

my mission is to locate a certain word inside a pdf file and insert an image next to this word

if anyone can recommend me on some api's or examples i will be very grateful.


Thanx ahead

Daniel

Dani AI

Generated

This problem is best solved by finding the visual coordinates of the target word, then drawing the image as an overlay at those coordinates — rewriting raw content streams to splice graphics into existing text is fragile. Given the earlier posts, has tried a common Java PDF library and others raised editability concerns; a practical path is to extract character positions, compute a bounding box for the matched word, and append an image to the page at an offset from that box.

A concise workflow (works reliably with Apache PDFBox):

  • Walk the page and capture character-level positions (collect the TextPosition objects for each character as text is emitted).
  • Build the page string and map character indices to their TextPosition lists so a found substring maps to exact glyph positions.
  • For any match, combine the glyph positions into a single bounding rectangle (min X, max X, min Y, max Y).
  • Open a PDPageContentStream in append mode and draw the image (PDImageXObject) at X = boxRight + gap (or left), Y = boxBottom, scaling the image to match desired height.

Example (pseudo steps):

override writeString(text, List<TextPosition> positions) -> store positions per-char
search pageString for "target"
for each match -> compute bbox from positions[matchStart..matchEnd]
append image with PDPageContentStream at bbox coordinates

Caveats and tips: PDF coordinates are page-based (watch rotation and origin), text may be split into many small TextPosition objects (hyphenation, columns, ligatures), and some PDFs use outlines or paths instead of text (OCR required). Test across representative PDFs and scale/offset the image to avoid overlapping glyphs. See Apache PDFBox examples for detailed APIs and samples: Apache PDFBox examples.

Recommended Answers

All 4 Replies

Try iText for PDF processing.

I know this api and I'm working alot with it... but i haven't found a way to do it with this api...

small wonder. PDF files are readonly, so you can't change them.

Try iText for PDF processing.

I do remember any search functions in iText...
However it should be possible if you use PdfDictionary object as described in chapter 18 of iText book. Only problem is you have to get around cross-referencing of PDF document. Very-very dirty work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.