1

I have a PDF File (ex:Hello.pdf) and what i need to do with this PDF is Read the file and get text content and replace the text with HashMap Values (If find the key in Text) ex:- If PDF Content Text is (My Name is [Name]) and hashMap Key "Name":"Vikrant" Then overwrite this as "My Name is Vikrant" and write it to the PDF. I have Tried iTextPDF Jar but still won't got any logical Solution. /* example inspired from "iText in action" (2006), chapter 2 */

        PdfReader reader = new PdfReader("D:/HelloWorld.pdf"); // input PDF
        PdfStamper stamper = new PdfStamper(reader,
          new FileOutputStream("D:/Bubi_modified.pdf")); // output PDF
        BaseFont bf = BaseFont.createFont(
                BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); // set font

        //loop on pages (1-based)
        for (int i=1; i<=reader.getNumberOfPages(); i++){

            // get object for writing over the existing content;
            // you can also use getUnderContent for writing in the bottom layer
            PdfContentByte over = stamper.getOverContent(i);
            // write text
            over.beginText();
            over.setFontAndSize(bf, 10);    // set font and size
            over.setTextMatrix(107, 740);   // set x,y position (0,0 is at the bottom left)
            over.showText("I can write at page " + i);  // set text
            over.endText();

            // draw a red circle
            over.setRGBColorStroke(0xFF, 0x00, 0x00);
            over.setLineWidth(5f);
            over.ellipse(250, 450, 350, 550);
            over.stroke();
        }

        stamper.close();

    }

I have Tried PDFStamper(iTextPDF.jar) But this class can put the content to the pdf not edit the earlier Text or Image.

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
  • PDF is allows limited editing even with Itext or other libraries as its an end file format meant for read only views. I would suggest not to do anything too complex instead try to get the source document in another format if possible which can be updated easily. Check out related thread http://stackoverflow.com/questions/4131031/editing-pdf-text-using-java – Sheetal Mohan Sharma Feb 12 '16 at 09:22
  • @SheetalMohanSharma , Actually my problem is PDF content is Fetched from any CRM Tool and then edit it (the way i described in Question) and send it as a mail attachment. I can draw my own PDF content using (iTextPDF.jar) but i need to do with the existed content. Because PDF Files can be modified by any other admin user it is not fixed . – Vikrant Kashyap Feb 12 '16 at 09:28

0 Answers0