Editing PDF text using Java and Itext

2019-01-20 20:41发布

问题:

Is there a way I can edit a PDF document text? like find and replace specific text ?

I have a PDF document which contains placeholders for text that I need to identify and be replaced or just delete that text.

I am able to edit the pdf with a specific coordinates (x, y) but unable to identify and replace. All the libraries that I saw created PDF from scratch and small editing functionality. Is there anyway I can edit above explained using itext? please advise...thank you!

**Example : A pdf document contains following paragaph. In this paragraph, I need to identify DATE: and FROM: as a text and replace it with something else.

The oldest classical Greek and Latin writing had little or no spaces between words or other ones, and could be written in boustrophedon (alternating directions). Over time, text direction (left to right) became standardized, and word dividers and terminal punctuation became common. **DATE: FROM: The first way to divide sentences into groups was the original paragraphos, similar to an underscore at the beginning of the new group -----------------------------------------------------------**

回答1:

Allow me to copy the intro of chapter 6 of my book:

When I wrote the first book about iText, the publisher didn’t like the subtitle “Creating and Manipulating PDF.” He didn’t like the word manipulating because of some of its pejorative meanings. If you consult the dictionary on Yahoo! education, you’ll find the following definitions:

  • To influence or manage shrewdly or deviously
  • To tamper with or falsify for personal gain

Obviously, that’s not what the book is about. The publisher suggested “Creating and Editing PDF” as a better subtitle. I explained that PDF isn’t a document format well suited for editing. PDF is an end product. It’s a display format. It’s not a word processing format.

In a word processing format, the content is distributed over different pages when you open the document in an application, not earlier. This has some disadvantages: if you open the same document in different applications, you can end up with a different page count. The same text snippet can be on page X when looked at in Microsoft Word, and on page Y when viewed in Open Office. That’s exactly the kind of problem you want to avoid by choosing PDF.

In a PDF document, every character or glyph on a PDF page has its fixed position, regardless of the application that’s used to view the document. This is an advantage, but it also comes with a disadvantage. Suppose you want to replace the word “edit” with the word “manipulate” in a sentence, you’d have to reflow the text. You’d have to reposition all the characters that follow that word. Maybe you’d even have to move a portion of the text to the next page. That’s not trivial, if not impossible.

If you want to “edit” a PDF, it’s advised that you change the original source of the document and remake the PDF. If the original document was written using Microsoft Word, change the Word document, and make the PDF from the new version of the Word document. Don’t expect any tool to be able to edit a PDF file the same way you’d edit a Word document.

This being said, the verb “to manipulate” also means

  • To move, arrange, operate, or control by the hands or by mechanical means, especially in a skillful manner

That’s exactly what you’re going to do in this chapter. Using iText, you’re going to manipulate the pages of a PDF file in a skillful manner. You’re going to treat a PDF document as if it were made of digital paper.

In your question, you say: "All the libraries that I saw created PDF from scratch and small editing functionality."

Well, that's only normal. It's inherent to the document format you've chosen. Your design that involves "placeholders for text that you need to identify and replace or just delete" is seriously flawed. It suffers from a wrong choice of document format. You should have chosen a format that is suited for editing. PDF isn't such a format.



标签: java itext