You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by BalaSubramanian Vetrivel <th...@gmail.com> on 2016/07/11 11:50:34 UTC

Not able to replace a text in PDF using PDFBox 2.0.2

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);

                                             }

Re: Not able to replace a text in PDF using PDFBox 2.0.2

Posted by Tilman Hausherr <TH...@t-online.de>.
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\u2019t find \u201cabc\u201d. Additionally, you can\u2019t replace \u201cc\u201d with \u201cd\u201d 
because it isn\u2019t part of the subset.

You could also have problems with ligatures, e.g. \u201cff\u201d, \u201cfl\u201d, \u201cfi\u201d, 
\u201cffi\u201d, \u201cffl\u201d, 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 \u201cContents\u201d 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  \u2026
>
> 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(\u2026)
>
>
> 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(\u2026)
>
> 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(\u2026)
>
> 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);
>
>                                               }
>