Urgent.News

What's breaking now, across thousands of outlets.

Tech

Why Comparing PDFs Is Still Surprisingly Difficult

If you've ever had someone send you two PDF files and ask "what changed?", you probably know how this usually goes. Open the first file. Open the second one. Put them next to each other. Start scrolling. After a few pages, you realize that you're not actually comparing the documents anymore. You're comparing your memory of what you just saw with what you're looking at now. It gets worse when the…

Comparing two PDF files to determine what has changed can be a challenging task. When you open the first file and then the second one, it's easy to lose track of the comparison process. The documents may appear nearly identical, but that doesn't mean there aren't any changes. For instance, a 200-page contract with a single altered sentence is more difficult to review than two entirely different documents. The less difference there is between the PDFs, the more challenging it becomes to spot the important changes.

A PDF file isn't a typical text document. It contains various elements such as text, fonts, images, vector graphics, tables, headers and footers, page numbers, positioned elements, embedded metadata, annotations, and rendering instructions. Even if the text remains the same, the visual appearance of the pages can differ. Conversely, two PDFs might look almost identical while containing a meaningful textual modification. This distinction makes comparing PDFs more complex than comparing regular text files.

One possible solution is to extract the text from both PDFs and perform a standard text comparison. For example, using the `pdftotext` command, you can convert the PDFs to text files and then use the `diff` command to identify the differences. This method works well for simple documents, but it has limitations. It doesn't compare the actual PDFs; instead, it compares the extracted text, which may not always align with the visual changes in the PDFs.

For instance, if someone moves a table or replaces a chart with an image, the text comparison may not detect these changes.

Visual comparison through rendering each page as an image and then comparing the images is another approach. By rendering the pages and performing a pixel comparison, you can identify visual differences that the text comparison might miss. This technique can detect changes like a logo replacement, table movement, font modification, paragraph reflow, image resizing, footer relocation, page break alterations, and signature block disappearance.

However, visual comparison also has its drawbacks. Even a minor rendering difference can result in a large number of changed pixels, and different PDF rendering engines may produce slightly different outputs. Additionally, anti-aliasing can make otherwise identical text appear different at the pixel level, leading to false positive changes.

In reality, PDF comparison involves two distinct problems: text comparison and visual comparison. Text comparison helps answer questions such as whether a specific sentence, number, paragraph, or clause has changed. Visual comparison, on the other hand, addresses questions like whether the layout, images, tables, page breaks, or annotations have changed.

Neither approach can completely replace the other. For serious document review, having both text and visual comparison views is more beneficial than choosing just one. The "tiny change" problem highlights the importance of focusing on the modified pages and regions rather than presenting a long list of unchanged material. A useful PDF comparison tool should bring attention to the actual changes, making it easier to inspect the surrounding context and understand the significance of the differences.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Friday 4 September →