Replace text inside a PDF file using iText

后端 未结 3 1716
不思量自难忘°
不思量自难忘° 2020-12-19 05:37

Im using iText(5.5.13) library to read a .PDF and replace a pattern inside the file. The problem is that the pattern is not being found because somehow some wei

3条回答
  •  醉梦人生
    2020-12-19 06:12

    A PDF file is not a Word Processing file. What you see are explicit placement of characters that are kerned together and/or many other things. your dream to "replace" text in such a way is not possible or better said, not likely if not impossible.

    A PDF is a binary file with byte offsets. It have many parts. Like this is at this byte offset and read this, then go that that byte offset and read that.

    You cannot just replace "foo" with "foobar" and think that it will work. It would disrupt all byte offsets and break the file completely.

    Try it yourself before even asking.

    In your example you have above, open the file in some editor and change the string in what you posted from this:

    This is a
    

    to this:

    WOW Let me change this data around for the content "This is a"
    

    Save that file and try an open it. Even that, which is a set string of content not crossing the boundaries you identified will not work. Because it is not a Word Processing file. It is not a text file. It is a binary file that you cannot manipulate as you think you can.

提交回复
热议问题