เพิ่มหรือแก้ไขไฮเปอร์ลิงก์
ไฮเปอร์ลิงก์ในเอกสาร Microsoft Word คือฟิลด์ HYPERLINK
ใน Aspose.Words ไฮเปอร์ลิงก์ถูกนำมาใช้ผ่านคลาส FieldHyperlink
การแทรกไฮเปอร์ลิงก์
ใช้วิธี insert_hyperlink เพื่อแทรกไฮเปอร์ลิงก์ลงในเอกสาร วิธีนี้ยอมรับพารามิเตอร์สามตัว:
- ข้อความของลิงค์ที่จะแสดงในเอกสาร
- ปลายทางลิงก์ (URL หรือชื่อบุ๊คมาร์คภายในเอกสาร)
- พารามิเตอร์บูลีนที่ควรเป็น true หาก
URL
เป็นชื่อของบุ๊กมาร์กภายในเอกสาร
วิธีการ InsertHyperlink จะเพิ่มเครื่องหมายอะพอสทรอฟี่ที่จุดเริ่มต้นและจุดสิ้นสุดของ URL เสมอ
Font
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกไฮเปอร์ลิงก์ลงในเอกสารโดยใช้ DocumentBuilder:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
builder.write("Please make sure to visit ") | |
builder.font.color = drawing.Color.blue | |
builder.font.underline = aw.Underline.SINGLE | |
builder.insert_hyperlink("Aspose Website", "http:#www.aspose.com", False) | |
builder.font.clear_formatting() | |
builder.write(" for more information.") | |
doc.save(docs_base.artifacts_dir + "AddContentUsingDocumentBuilder.insert_hyperlink.docx") |
แทนที่หรือแก้ไขไฮเปอร์ลิงก์
ไฮเปอร์ลิงก์ในเอกสาร Microsoft Word คือช่อง เขตข้อมูลในเอกสาร Word เป็นโครงสร้างที่ซับซ้อนซึ่งประกอบด้วยหลายโหนดซึ่งรวมถึงการเริ่มต้นเขตข้อมูล โค้ดเขตข้อมูล ตัวคั่นเขตข้อมูล ผลลัพธ์ของเขตข้อมูล และจุดสิ้นสุดของเขตข้อมูล ช่องต่างๆ สามารถซ้อนกัน มีเนื้อหาที่หลากหลาย และขยายหลายย่อหน้าหรือส่วนต่างๆ ในเอกสารได้
หากต้องการแทนที่หรือแก้ไขไฮเปอร์ลิงก์ จำเป็นต้องค้นหาไฮเปอร์ลิงก์ในเอกสารและแทนที่ข้อความ URL หรือทั้งสองอย่าง
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีค้นหาไฮเปอร์ลิงก์ทั้งหมดในเอกสาร Word และเปลี่ยน URL
และชื่อที่แสดง:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Hyperlinks.docx") | |
for field in doc.range.fields : | |
if field.type == aw.fields.FieldType.FIELD_HYPERLINK: | |
hyperlink = field.as_field_hyperlink() | |
# Some hyperlinks can be local (links to bookmarks inside the document), ignore these. | |
if hyperlink.sub_address != None : | |
continue | |
hyperlink.address = "http:#www.aspose.com" | |
hyperlink.result = "Aspose - The .net & Java Component Publisher" | |
doc.save(docs_base.artifacts_dir + "WorkingWithFields.replace_hyperlinks.docx") |