Check Whether Two Documents Have Identical Text

How to tell whether two documents contain the same text: exact versus normalized comparison, what ignoring formatting hides, and the invisible characters that cause false mismatches.

Two documents can look the same on screen and still differ in the characters they store. They can also differ in appearance, through fonts, bold, spacing or layout, while holding exactly the same words. Before you can say whether two documents are identical, you need to decide which of those differences counts. This guide explains the choice, what each comparison method actually checks, and where a match or mismatch can mislead you.

Is a comparison that ignores formatting right for your purpose?

A comparison that ignores formatting is usually the right choice when you care about the wording, not the presentation. Examples include confirming that a pasted copy of an article matches the original, checking that a revised draft changed only its styling, or testing whether text survived export from one format to another. The main benefit is fewer false alarms. Line wrapping, font changes, extra spaces and different line endings stop showing up as differences.

The tradeoff is that every rule that ignores something can also hide a real change. If you ignore case, a change from "Shall" to "shall" disappears, and in some contracts and style guides that distinction matters. If you ignore whitespace, changed indentation in source code or verse goes unnoticed. If you strip formatting, you lose bold or italic text that marks a defined term or a quotation.

An exact, unnormalized comparison of the plain text, or of the files themselves, is preferable in these situations:

  • You need to prove that a file is an unaltered copy, for example for a legal, archival or audit record.
  • The content is code, configuration, data or anything else where whitespace and case carry meaning.
  • You plan to report the result to someone else and need to state exactly what was compared.
  • You suspect deliberate tampering, such as hidden or look-alike characters.

A practical approach is to run the exact check first. If it passes, the documents are identical and you are finished. If it fails, run a normalized comparison to learn whether the differences are only cosmetic, and then review the remaining differences by eye.

What "identical" can mean

People use "identical" for several different checks, and they give different answers on the same pair of documents:

Level | What is compared | Ignores
Identical files | Every byte of the file | Nothing
Identical extracted text | The character sequence after text is pulled out of the file format | Fonts, styles, layout and file metadata
Normalized text | Extracted text after rules such as Unicode normalization, case folding and whitespace collapsing | Whatever the chosen rules remove
Same meaning | Human judgment of content | Wording, which software cannot check reliably

Two word-processor files with the same text almost never match at the file level, because they store creation dates, editing data and internal structure. For most documents, the useful question is about the extracted text, with or without normalization.

What ignoring formatting actually changes

A text comparison works on stored characters, not on what you see. The important distinction is between differences in the stored characters and differences that exist only in how the text is displayed.

Presentation-only differences

Fonts, bold, italics, colors, sizes, line wrapping and styling applied by a word processor or by CSS on a web page do not change the underlying characters. When you extract the plain text from such documents, these differences disappear on their own. If two documents differ only in this way, their extracted text will match without any special rules.

Bidirectional display falls into the same category. Arabic and Hebrew text is stored in logical order, the order in which it is read, and is displayed right to left. Brackets and similar characters are drawn mirrored in a right-to-left context, but the stored character does not change. Two documents can show the same right-to-left text in different visual arrangements and still store the same characters.

Stored-character differences that look like formatting

Some differences look like styling but are changes to the stored characters. These cause mismatches that surprise people:

  • So-called fancy font text: bold, italic, script or double-struck letters made from Unicode mathematical alphanumeric symbols are separate characters, not styled ordinary letters. To a comparison, a word typed this way does not match the plain word.
  • Fullwidth letters and ligature characters, such as a single character standing for "fi", which some PDF extraction produces.
  • Composed versus decomposed accents: "é" can be stored as one code point or as "e" followed by a combining acute accent. They look the same and compare as different.
  • Non-breaking spaces, which look like ordinary spaces.
  • Curly quotation marks and apostrophes versus straight ones, and different dash characters.
  • Invisible characters such as zero-width spaces, soft hyphens, byte order marks and bidirectional control characters.
  • Line endings: Windows-style CRLF versus Unix-style LF.

"Mirror" and "reverse" are also ambiguous. Reversed text, where the order of characters or words is flipped, is a real change to the stored text and will not match the original. Text that only appears mirrored because of right-to-left layout or a visual effect has not changed. When someone says a document uses a different "font", check whether they mean real font styling, which disappears on extraction, or substituted Unicode characters, which do not.

What normalization rules do and do not fix

Unicode defines standard normalization forms. NFC composes accented letters into a single code point where possible, which resolves the composed-versus-decomposed problem. NFKC goes further and applies compatibility mappings. It converts mathematical bold and italic letters, fullwidth letters and ligature characters back to their ordinary forms, and it turns a non-breaking space into a regular space. NFKC does not remove zero-width spaces or bidirectional controls, and it does not convert curly quotes to straight ones. Those need explicit rules if you want to ignore them.

Because NFKC folds styled characters into plain ones, it also hides the fact that one document used them. That is fine for a wording check. It is not fine if the styled characters themselves are the issue, for example when they hurt accessibility or search.

How to run the comparison

The steps below use widely available command-line tools and Python. Online comparison tools perform similar steps, but their documentation is the only reliable guide to which rules they apply and what happens to uploaded text. A tool's appearance or marketing does not tell you how it handles or retains data. Check its stated policy before pasting confidential documents.

1. Check for identical files

If the hashes match, the files are byte-for-byte identical. If they differ, that tells you only that something differs, possibly just metadata.

sha256sum original.txt copy.txt

cmp -s original.txt copy.txt && echo "identical" || echo "different"

2. Extract plain text

Save or export both documents as plain text in UTF-8. Be aware that extraction from PDFs can join or split lines differently, insert hyphens where words were broken across lines, or change the order of columns and footnotes. A mismatch after PDF extraction may reflect the extraction rather than the content.

3. Compare with chosen whitespace and case rules

GNU diff has options that state exactly what to ignore. -q reports only whether the files differ. -b ignores changes in the amount of whitespace. -w ignores all whitespace. -B ignores blank lines. -i ignores case. --strip-trailing-cr ignores Windows line endings.

diff -q original.txt copy.txt

diff -w -B --strip-trailing-cr original.txt copy.txt

4. Normalize Unicode before comparing

diff does not apply Unicode normalization. This short Python script applies NFKC, removes common invisible characters, optionally folds case, and collapses all whitespace before comparing. Edit the INVISIBLE set and the casefold line to match the differences you have decided to ignore.

import sys
import unicodedata

INVISIBLE = {"\u200b", "\u200c", "\u200d", "\u2060", "\ufeff", "\u00ad",
             "\u200e", "\u200f", "\u202a", "\u202b", "\u202c", "\u202d", "\u202e"}

def normalize(text, fold_case=False):
    text = unicodedata.normalize("NFKC", text)
    text = "".join(ch for ch in text if ch not in INVISIBLE)
    if fold_case:
        text = text.casefold()
    return " ".join(text.split())

with open(sys.argv[1], encoding="utf-8") as f1, open(sys.argv[2], encoding="utf-8") as f2:
    a, b = normalize(f1.read()), normalize(f2.read())

print("identical after normalization" if a == b else "different")

5. Find out what differs

A yes-or-no answer is rarely enough. When documents still differ, view a line-by-line or word-by-word diff. To find invisible or look-alike characters, print the code points around the reported difference, for example by looping over the characters in Python and printing the hex value of each. A difference that appears to involve no visible change almost always comes from one of the characters listed above.

Does the text stay comparable after copying or sharing?

Copying and pasting is a common source of differences between documents that started out the same. Depending on the source and destination, pasting can convert straight quotes to curly ones, replace spaces with non-breaking spaces, carry hidden characters along, drop formatting, or change line endings. Behavior varies between applications and versions, so a paste that kept the text intact in one setting does not prove it will in another. If the result matters, test the specific route you use, then compare the output with the source.

Text styled with substituted Unicode characters has broader usability problems. Search and find features typically will not match it against ordinary letters. Spell checkers may flag it. Screen readers may announce such characters individually, by name, or not at all, depending on the software and its settings. The supplied evidence does not include tests of specific screen readers or platforms, so this guide makes no claim about any particular one. If a document must remain searchable, editable and accessible, keep the plain-text version as the reference copy and treat styled characters as decoration on a copy.

Bidirectional control characters need particular care. They are invisible but can change the visible order of text, so two documents can look different while storing nearly identical characters, or look the same while storing different ones. When you compare documents from untrusted sources, report these characters rather than silently removing them.

Gaps in this guide

This guide covers general, verifiable behavior of Unicode normalization and standard command-line tools. It does not evaluate any specific online comparison service. It does not report tests of particular word processors, PDF readers, browsers or assistive technologies, and it makes no claims about how named products handle pasted text or retain data. Where those details matter for your workflow, check the product's own documentation or run a small test with your own files.

Summary

To check whether two documents are identical, first decide whether you mean identical files, identical text, or identical text after specific differences are ignored. Run the exact check first. If it fails, apply only the normalization rules you can justify, such as Unicode NFKC, whitespace collapsing, invisible-character removal and, where appropriate, case folding. Then review whatever differences remain. Write down the rules you used, because a result of "identical after normalization" means something only when the reader knows what was ignored.