Hi All Request your help on this
Not able to replace a text in PDF using PDFBox 2.0.2 http://stackoverflow.com/questions/38306151/not-able-to-replace-a-text-in-pdf-using-pdfbox-2-0-2 My requirements 1) I need to identify a particular text pattern 2) Then replace that text pattern with pre-defined text-value with the same format of text pattern, such as font, font colour, bold … 3) I am able to identify the text, replace that text with predefined values, But writing to PDF is failing. I tried the following 2 approaches to write to PDF 1) By Overriding writeString(String string, List<TextPosition> textPositions)of PDFTextStripper 2) By using cosArray.add(new COSString(replacedField)); or cosArray.set(…) Results for approach 1 - By Overriding writeString The pdf generated by this code is not getting opened in PDF. I am able to open in word, But there is no format of original text. Results for approach 2 - By using cosArray.add or cosArray.set(…) I am seeing only boxes in generated PDF . Code for approach 1 - By Overriding writeString public void rewrite(String templatePDFPath) throws IOException { PDDocument document = null; Writer pdfWriter = null; try { File templateFile = new File(templatePDFPath); document = PDDocument.load(templateFile); this.setSortByPosition(true); this.setStartPage(0); this.setEndPage(document.getNumberOfPages()); pdfWriter = new PrintWriter(Utils.getFilePathWithTimeStamp(templatePDFPath).toString()); this.writeText(document, pdfWriter); } finally { if (document != null) { document.close(); } if (null != pdfWriter) pdfWriter.close(); // if (null != pdfWriter) // pdfWriter.close(); } } protected void writeString(String string, List<TextPosition> textPositions) throws IOException { for (int i = 0; i < textPositions.size(); i++) { TextPosition text = textPositions.get(i); String currentCharcter = text.getUnicode(); // System.out.println("String[" + text.getXDirAdj() + "," + // // text.getYDirAdj() + " fs=" + text.getFontSize() // + " xscale=" + // text.getXScale() + " height=" + // text.getHeightDir() + " // space=" // + // text.getWidthOfSpace() + " width=" + text.getWidthDirAdj() + // // "]" + // currentCharcter); } String replacedString = replaceFields(string.trim()); if (!(string.equals(replacedString))) { System.out.println("Field " + string + " is replaced by value " + replacedString); // super.writeString(replacedString, textPositions); super.writeString(replacedString); } } Code for approach 2 - By using cosArray.add or cosArray.set(…) public List<String> replaceFieldsInCosArray(COSArray cosArray) { List<String> replacedStrings = new ArrayList<String>(); String stringsOfCOSArray = ""; for (int cosArrayIndex = 0; cosArrayIndex < cosArray.size(); cosArrayIndex++) { Object cosObject = cosArray.get(cosArrayIndex); if (cosObject instanceof COSString) { COSString cosString = (COSString) cosObject; stringsOfCOSArray += cosString.getString(); } } stringsOfCOSArray = stringsOfCOSArray.trim(); //cosArray.clear(); String replacedField = this.replaceFields(stringsOfCOSArray); System.out.println("cosText:" + stringsOfCOSArray + ":replacedField:" + replacedField); cosArray.add(new COSString(replacedField)); if (!stringsOfCOSArray.equals(replacedField)) { replacedStrings.add(replacedField); }

