When importing some PDF files, parts of the text are corrupted. It mostly affects numbers and punctuation, while the rest of the same text object can remain correct.
It doesn’t look like a straightforward missing-font issue: a single text object can contain both correct and corrupted characters.
For example, Rhino imported this text as:
ARCHITECTS U P XC X IS PROHIBITED X
The intended text is:
ARCHITECTS, P.C. IS PROHIBITED.
In the same document:
ì í î ï ð ñ ò ó ô õrepresent0 1 2 3 4 5 6 7 8 9.rsometimes represents a hyphen.Usometimes represents a comma.Xsometimes represents a period.
These mappings are ambiguous. For example, the X in EXPRESS is a real X, while the standalone X in RIGHTS RESERVED X should be a period. The same applies to the U in USE, which must remain a U.
Rhino also inserts actual ASCII spaces between some of the corrupted characters. For example:
ì î r î ì r î ì î òshould be02-20-2026.A î X î ìshould beA2.20.M ð rRF ì î ìshould beM4-RF020.
I have a PDF-import command in my plug-in, so I added a postprocessing pass immediately after import. It scans the imported TextEntity objects, applies the unambiguous number mapping, and uses context to make an educated guess for punctuation and the inserted spaces.
This workaround has handled my recent conversions successfully, but it is still only a heuristic. It cannot resolve every ambiguous case without access to the PDF’s original font encoding.
Standalone Rhino 9 Python workaround
Run this in Rhino’s Script Editor immediately after importing the PDF. It scans all text objects in the active document.
import re
import Rhino
DIGIT_MARKERS = "ìíîïðñòóôõ"
DIGIT_MAP = dict(zip(DIGIT_MARKERS, "0123456789"))
MARKER_SET = frozenset(DIGIT_MARKERS)
PUNCTUATION_MAP = {
"r": "-",
"X": ".",
"U": ",",
}
def repair_pdf_text(text):
if not text:
return text
marker_class = re.escape(DIGIT_MARKERS)
# Rhino sometimes inserts one extra space before each corrupted digit.
# Removing exactly one preserves a real space when two are present.
repaired_text = re.sub(
rf"[ \t](?=[{marker_class}])",
"",
text,
)
# Avoid guessing punctuation in strings containing unknown control
# characters. The distinctive digit mapping can still be applied.
has_unknown_control_character = any(
(ord(character) < 32 and character not in "\r\n\t")
or 0x7F <= ord(character) <= 0x9F
for character in repaired_text
)
if not has_unknown_control_character:
# P XC X -> P.C.
repaired_text = re.sub(
r"(?<!\S)([A-Z])[ \t]+X([A-Z])[ \t]+X(?=[ \t]|$)",
r"\1.\2.",
repaired_text,
)
# Standalone U and X commonly represent commas and periods.
repaired_text = re.sub(
r"[ \t]+U(?=[ \t]|$)",
",",
repaired_text,
)
repaired_text = re.sub(
r"[ \t]+X(?=[ \t]|$)",
".",
repaired_text,
)
# Punctuation next to corrupted numbers.
repaired_text = re.sub(
rf"[ \t]+X(?=[{marker_class}])",
".",
repaired_text,
)
repaired_text = re.sub(
rf"[ \t]+r(?=[{marker_class}])",
"-",
repaired_text,
)
repaired_text = re.sub(
rf"(?<=[{marker_class}])[ \t]+r(?=[A-Z{marker_class}])",
"-",
repaired_text,
)
# FLOOR PLAN r DIMENSION -> FLOOR PLAN - DIMENSION
repaired_text = re.sub(
r"(?<=[ \t])r(?=[ \t])",
"-",
repaired_text,
)
# Replace corrupted digits and punctuation directly adjacent to them.
repaired_characters = []
for index, character in enumerate(repaired_text):
repaired_digit = DIGIT_MAP.get(character)
if repaired_digit is not None:
repaired_characters.append(repaired_digit)
continue
punctuation = PUNCTUATION_MAP.get(character)
previous_is_marker = (
index > 0
and repaired_text[index - 1] in MARKER_SET
)
next_is_marker = (
index + 1 < len(repaired_text)
and repaired_text[index + 1] in MARKER_SET
)
if punctuation is not None and (
previous_is_marker or next_is_marker
):
repaired_characters.append(punctuation)
else:
repaired_characters.append(character)
return "".join(repaired_characters)
def main():
doc = Rhino.RhinoDoc.ActiveDoc
changed_count = 0
undo_record = doc.BeginUndoRecord(
"Repair corrupted PDF text"
)
try:
annotation_objects = doc.Objects.GetObjectList(
Rhino.DocObjects.ObjectType.Annotation
)
for rhino_object in annotation_objects:
geometry = rhino_object.Geometry
if not isinstance(
geometry,
Rhino.Geometry.TextEntity,
):
continue
original_text = str(geometry.PlainText)
repaired_text = repair_pdf_text(original_text)
if repaired_text == original_text:
continue
replacement = geometry.Duplicate()
replacement.PlainText = repaired_text
# Assigning PlainText can reset the font to the annotation
# style font, so restore the imported font afterward.
replacement.Font = geometry.Font
if doc.Objects.Replace(
rhino_object.Id,
replacement,
):
changed_count += 1
finally:
doc.EndUndoRecord(undo_record)
if changed_count:
doc.Views.Redraw()
Rhino.RhinoApp.WriteLine(
f"Repaired {changed_count} PDF text "
f"object{'' if changed_count == 1 else 's'}."
)
main()
It looks as though Rhino may be importing some PDF font character codes directly instead of resolving them through the font’s encoding or Unicode mapping, but that is only my guess.
Is there a way for Rhino’s PDF importer to recover the intended characters from the embedded font information instead of relying on this kind of heuristic?
This snapshot shows the PDF in a viewer on the left and the imported drawing in Rhino on the right:





