You posted your question at 4 different places:
- users
- dev
- commits
- stackoverflow
If you posted to @dev, then it means you're a pdfbox developer. Although I don't remember you being part of the team, you should remember about this:
https://pdfbox.apache.org/2.0/migration.html


     Why was the ReplaceText example removed?

The ReplaceText example has been removed as it gave the incorrect illusion that text can be replaced easily. Words are often split, as seen by this excerpt of a content stream:

|[ (Do) -29 (c) -1 (umen) 30 (tation) ] TJ |

Other problems will appear with font subsets: for example, if only the glyphs for a, b and c are used, these would be encoded as hex 0, 1 and 2, so you won’t find “abc”. Additionally, you can’t replace “c” with “d” because it isn’t part of the subset.

You could also have problems with ligatures, e.g. “ff”, “fl”, “fi”, “ffi”, “ffl”, which can be represented by a single code in many fonts. To understand this yourself, view any file with PDFDebugger and have a look at the “Contents” entry of a page.

See also https://stackoverflow.com/questions/35420609/pdfbox-2-0-rc3-find-and-replace-text



Tilman

Am 11.07.2016 um 13:50 schrieb BalaSubramanian Vetrivel:
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);

                                              }


Reply via email to