Hyperlinks issue document _INPUT.docx (60.9 KB)
Redlined_Hyperlinks_issue_document__OUTPUT.docx (61.8 KB)
Revised_Hyperlinks_issue_document__OUTPUT.docx (55.9 KB)
ISSUE: Able to insert hyperlink but missing styles of hyperlinks fields (both external and internal) in redlining document. But no issue with revised document.
ASPOSE VERSION : 25.7.0
CODE :
Code to insert hyperlink and apply styles :
def style_hyperlink_field(self, field: aw.fields.Field, style: Dict[str, Any]) -> bool:
"""
Apply style to all display-text runs of a hyperlink field:
all runs between field.separator and field.end.
Returns True if at least one run was updated.
"""
if field is None:
return False
# Check field type defensively
is_hl = False
try:
is_hl = (field.type == aw.fields.FieldType.FIELD_HYPERLINK)
except Exception:
is_hl = False
if not is_hl:
try:
code = field.get_field_code() or ""
is_hl = code.strip().upper().startswith("HYPERLINK")
except Exception:
is_hl = False
if not is_hl:
return False
sep = field.separator
end = field.end
if sep is None or end is None:
return False
node = sep.next_sibling
updated = False
while node is not None and node != end:
if node.node_type == aw.NodeType.RUN:
self.apply_style_to_font(node.as_run().font, style)
updated = True
node = node.next_sibling
return updated
URL_PATTERN = re.compile(
r'\b(?:https?://|www\.)?'
r'(?:[a-zA-Z0-9-]+\.)+'
r'(?:[a-zA-Z]{2,})'
r'(?:[:0-9]*)?'
r'(?:/[^\s]*)?', re.IGNORECASE
)
tokens = ['this is teest link', ' ', 'https://www.google.com', ' ', 'to test links formatting.']
orig_token_styles = [('this is teest link', {'text': 'this is teest link', 'bold': False, 'italic': False, 'underline': 'Underline.NONE', 'font_name': 'Arial', 'font_size': 10.0, 'color': None, 'highlight_color': None, 'shading_color': None, 'all_caps': False, 'strike_through': False, 'superscript': False, 'subscript': False, 'hyperlink': False, 'hyperlink_url': None, 'reference_type': None, 'reference_target': None, 'reference_field_code': None, 'footnote': False}), (' ', {}), ('https://www.google.com', {'text': 'https://www.google.com', 'bold': True, 'italic': True, 'underline': 'Underline.SINGLE', 'font_name': 'Times New Roman', 'font_size': 11.0, 'color': 'RGB(0, 0, 255)', 'highlight_color': 'RGB(255, 255, 0)', 'shading_color': None, 'all_caps': False, 'strike_through': False, 'superscript': False, 'subscript': False, 'hyperlink': True, 'hyperlink_url': 'https://www.google.com', 'reference_type': None, 'reference_target': None, 'reference_field_code': 'HYPERLINK "https://www.google.com"', 'footnote': False}), (' ', {'text': ' ', 'bold': True, 'italic': True, 'underline': 'Underline.NONE', 'font_name': 'Times New Roman', 'font_size': 11.0, 'color': 'RGB(112, 48, 160)', 'highlight_color': None, 'shading_color': None, 'all_caps': False, 'strike_through': False, 'superscript': False, 'subscript': False, 'hyperlink': False, 'hyperlink_url': None, 'reference_type': None, 'reference_target': None, 'reference_field_code': None, 'footnote': False}), ('to test links formatting.', {'text': 'to test links formatting.', 'bold': False, 'italic': False, 'underline': 'Underline.NONE', 'font_name': 'Arial', 'font_size': 10.0, 'color': None, 'highlight_color': None, 'shading_color': None, 'all_caps': False, 'strike_through': False, 'superscript': False, 'subscript': False, 'hyperlink': False, 'hyperlink_url': None, 'reference_type': None, 'reference_target': None, 'reference_field_code': None, 'footnote': False})]
k = 0
while k < len(tokens):
token = tokens[k]
style = orig_token_styles[k][1] if k < len(orig_token_styles) else {}
run_hyperlink = style.get("hyperlink", False)
hyperlink_url = style.get("hyperlink_url")
field_code = style.get("reference_field_code")
run_macro = style.get("is_macrobutton", False)
# External hyperlink handling
if run_hyperlink and hyperlink_url and URL_PATTERN.match(str(hyperlink_url)):
external_link_field = builder.insert_hyperlink(token, hyperlink_url, False)
self.style_hyperlink_field(external_link_field, style)
k += 1
continue
Code to generate redlined and revised document :
doc.save(str(clean_doc_path))
print('Saved revised document')
revised_doc = aw.Document(str(clean_doc_path))
revised_doc.accept_all_revisions()
# Compare and produce redlined version
redlined_doc = aw.Document(str(input_doc_path)) # Reload to preserve original for redlining
redlined_doc.accept_all_revisions()
opts = aw.comparing.CompareOptions()
opts.granularity = aw.comparing.Granularity.WORD_LEVEL
opts.compare_moves = True
opts.ignore_formatting = False
opts.ignore_case_changes = False
opts.ignore_comments = False
opts.ignore_tables = False
opts.ignore_fields = False
opts.ignore_footnotes = False
opts.ignore_textboxes = False
opts.ignore_headers_and_footers = True # include header/footer TEXT changes.
# Advanced: suppress spurious logo/shape/SDT ID churn in headers/footers
adv = opts.advanced_options
adv.ignore_dml_unique_id = True
adv.ignore_store_item_id = True
# Compare documents — this adds revisions to original_doc
redlined_doc.compare(revised_doc, "Redlining", datetime.datetime.now(), opts)
ro = redlined_doc.layout_options.revision_options
ro.show_in_balloons = aw.layout.ShowInBalloons.NONE # renders insert, delete and format inline.
ro.inserted_text_color = aw.layout.RevisionColor.CLASSIC_BLUE
ro.deleted_text_color = aw.layout.RevisionColor.CLASSIC_RED
ro.show_revision_bars = True
redlined_doc.update_page_layout()
redlined_doc.save(str(redlined_doc_path))
