0

I have a requirement to do below items,

  1. Read the existing PDF file
  2. Search the specific keywords in the PDF
  3. Highlight them in specific color or bold
  4. Save the PDF

and i have to tried below code,

public static void main(String[] args) throws IOException, DocumentException 
{ 
File file = new File(DEST); file.getParentFile().mkdirs();
new BrefingPackageHighlight_Main2().manipulatePdf(SRC, DEST);
} 
public void manipulatePdf(String src, String dest) throws IOException, DocumentException 
{ 
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); PdfContentByte canvas = stamper.getOverContent(2); canvas.saveState(); canvas.setColorFill(BaseColor.YELLOW);
canvas.rectangle(200, 786, 5, 5);
canvas.fill();
canvas.restoreState(); 
stamper.close(); 
reader.close(); 
}

the above code only highlights top of the second page. Please provide me samples to search one specific keyword and highlight them alone.

Raj GT
  • 25
  • 2
  • 8
  • Searching for specific occurrences of certain words isn't easy due to the nature of pdf. Have you tried anything already? You can in theory use itext to iterate over every dictionary and check for text content in the content-stream, but any implementation depends on the structure of the pdf you want to read. – Samuel Huylebroeck Nov 28 '16 at 10:29
  • [This answer](http://stackoverflow.com/a/33959759/1729265) refers to [tag:iTextSharp], the .Net port of [tag:iText]. That difference aside, it deals with your problem. – mkl Nov 28 '16 at 11:06

0 Answers0