java - Looking for a way to replace a string from a pdf by retaining the formatting -
i have 2 static pdfs. second pdf having few place holders.at runtime replacing place holders strings application property file.finally append second pdf first pdf. formatting lost while doing it.please see code below,
private void appendpdf() throws documentexception, ioexception { //source pdf pdfreader reader_glossary = new pdfreader(gloosary); pdfstamper stamper = new pdfstamper(reader_glossary, new fileoutputstream(dest)); //adding new page in end of src pdf stamper.insertpage(reader_glossary.getnumberofpages() + 1, reader_glossary .getpagesizewithrotation(1)); pdfcontentbyte srccbytes = stamper .getovercontent(reader_glossary.getnumberofpages()); pdfreader readerout = new pdfreader(src); pdfreadercontentparser parser = new pdfreadercontentparser(readerout); textextractionstrategy strategy = parser.processcontent(1, new locationtextextractionstrategy()); string data = new string(strategy.getresultanttext().getbytes()); system.out.println(data); columntext ct = new columntext(srccbytes); ct.setsimplecolumn(50, 5, 600, 780); //replacing string before stamping paragraph p = new paragraph(data.replace("[name of covered entity]","xxx")); ct.addelement(p); ct.go(); stamper.close(); reader_glossary.close(); readerout.close(); }
i parsing plain text hence losing information.what best way replace place holders retaining format.again string going replace can of variable length.so after replace there shouldn't overlap/truncation of text.in above code achieving cost of losing formatting.
any highly appreciated.
Comments
Post a Comment