เพิ่มหรือแก้ไขไฮเปอร์ลิงก์
ไฮเปอร์ลิงก์ในเอกสาร Microsoft Word คือฟิลด์ HYPERLINK
ใน Aspose.Words ไฮเปอร์ลิงก์ถูกนำมาใช้ผ่านคลาส FieldHyperlink
แทรกไฮเปอร์ลิงก์
ใช้วิธี InsertHyperlink เพื่อแทรกไฮเปอร์ลิงก์ลงในเอกสาร วิธีนี้ยอมรับพารามิเตอร์สามตัว:
- ข้อความของลิงค์ที่จะแสดงในเอกสาร
- ปลายทางลิงก์ (URL หรือชื่อบุ๊คมาร์คภายในเอกสาร)
- พารามิเตอร์บูลีนที่ควรเป็น true หาก
URL
เป็นชื่อของบุ๊กมาร์กภายในเอกสาร
วิธีการ InsertHyperlink จะเพิ่มเครื่องหมายอะพอสทรอฟี่ที่จุดเริ่มต้นและจุดสิ้นสุดของ URL เสมอ
Font
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกไฮเปอร์ลิงก์ลงในเอกสารโดยใช้ DocumentBuilder:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.Write("Please make sure to visit "); | |
builder.Font.Color = Color.Blue; | |
builder.Font.Underline = Underline.Single; | |
builder.InsertHyperlink("Aspose Website", "http://www.aspose.com", false); | |
builder.Font.ClearFormatting(); | |
builder.Write(" for more information."); | |
doc.Save(ArtifactsDir + "AddContentUsingDocumentBuilder.InsertHyperlink.docx"); |
แทนที่หรือแก้ไขไฮเปอร์ลิงก์
ไฮเปอร์ลิงก์ในเอกสาร Microsoft Word คือช่อง ฟิลด์ในเอกสาร Word ดังที่เราได้กล่าวไว้ก่อนหน้านี้ เป็นโครงสร้างที่ซับซ้อนซึ่งประกอบด้วยหลายโหนดซึ่งรวมถึงจุดเริ่มต้นของฟิลด์ โค้ดฟิลด์ ตัวคั่นฟิลด์ ผลลัพธ์ของฟิลด์ และจุดสิ้นสุดของฟิลด์ ช่องต่างๆ สามารถซ้อนกัน มีเนื้อหาที่หลากหลาย และขยายหลายย่อหน้าหรือส่วนต่างๆ ในเอกสารได้
หากต้องการแทนที่หรือแก้ไขไฮเปอร์ลิงก์ จำเป็นต้องค้นหาไฮเปอร์ลิงก์ในเอกสารและแทนที่ข้อความ URL หรือทั้งสองอย่าง
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีค้นหาไฮเปอร์ลิงก์ทั้งหมดในเอกสาร Word และเปลี่ยน URL
และชื่อที่แสดง:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(MyDir + "Hyperlinks.docx"); | |
foreach (Field field in doc.Range.Fields) | |
{ | |
if (field.Type == FieldType.FieldHyperlink) | |
{ | |
FieldHyperlink hyperlink = (FieldHyperlink) field; | |
// Some hyperlinks can be local (links to bookmarks inside the document), ignore these. | |
if (hyperlink.SubAddress != null) | |
continue; | |
hyperlink.Address = "http://www.aspose.com"; | |
hyperlink.Result = "Aspose - The .NET & Java Component Publisher"; | |
} | |
} | |
doc.Save(ArtifactsDir + "WorkingWithFields.ReplaceHyperlinks.docx"); |