Quản lý Siêu liên kết trong Bản trình chiếu .NET
Giới thiệu
Siêu liên kết là một tham chiếu đến một đối tượng, dữ liệu hoặc một vị trí nào đó. Đây là các siêu liên kết phổ biến trong Bản trình chiếu PowerPoint:
- Liên kết tới các trang web trong văn bản, hình dạng hoặc phương tiện
- Liên kết tới các slide
Aspose.Slides for .NET cho phép bạn thực hiện nhiều tác vụ liên quan đến siêu liên kết trong bản trình chiếu.
Thêm Siêu Liên Kết URL
Thêm Siêu Liên Kết URL vào Văn Bản
Đoạn mã C# này cho bạn thấy cách thêm siêu liên kết trang web vào văn bản:
using (Presentation presentation = new Presentation())
{
IAutoShape shape1 = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 600, 50, false);
shape1.AddTextFrame("Aspose: File Format APIs");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FontHeight = 32;
presentation.Save("presentation-out.pptx", SaveFormat.Pptx);
}
Thêm Siêu Liên Kết URL vào Hình Dạng hoặc Khung
Đoạn mã mẫu bằng C# này cho bạn thấy cách thêm siêu liên kết trang web vào một hình dạng:
using (Presentation pres = new Presentation())
{
IShape shape = pres.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 600, 50);
shape.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
shape.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
pres.Save("pres-out.pptx", SaveFormat.Pptx);
}
Thêm Siêu Liên Kết URL vào Phương Tiện
Aspose.Slides cho phép bạn thêm siêu liên kết vào hình ảnh, tệp âm thanh và video.
Đoạn mã mẫu này cho bạn thấy cách thêm siêu liên kết vào hình ảnh:
using (Presentation pres = new Presentation())
{
// Thêm hình ảnh vào bản trình chiếu
IPPImage image = pres.Images.AddImage(File.ReadAllBytes("image.png"));
// Tạo khung ảnh trên slide 1 dựa trên hình ảnh đã thêm trước đó
IPictureFrame pictureFrame = pres.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, image);
pictureFrame.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
pictureFrame.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
pres.Save("pres-out.pptx", SaveFormat.Pptx);
}
Đoạn mã mẫu này cho bạn thấy cách thêm siêu liên kết vào tệp âm thanh:
using (Presentation pres = new Presentation())
{
IAudio audio = pres.Audios.AddAudio(File.ReadAllBytes("audio.mp3"));
IAudioFrame audioFrame = pres.Slides[0].Shapes.AddAudioFrameEmbedded(10, 10, 100, 100, audio);
audioFrame.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
audioFrame.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
pres.Save("pres-out.pptx", SaveFormat.Pptx);
}
Đoạn mã mẫu này cho bạn thấy cách thêm siêu liên kết vào video:
using (Presentation pres = new Presentation())
{
IVideo video = pres.Videos.AddVideo(File.ReadAllBytes("video.avi"));
IVideoFrame videoFrame = pres.Slides[0].Shapes.AddVideoFrame(10, 10, 100, 100, video);
videoFrame.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
videoFrame.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
pres.Save("pres-out.pptx", SaveFormat.Pptx);
}
Mẹo
Bạn có thể muốn xem Quản lý OLE.Sử Dụng Siêu Liên Kết Để Tạo Mục Lục
Vì siêu liên kết cho phép bạn thêm tham chiếu tới các đối tượng hoặc vị trí, bạn có thể sử dụng chúng để tạo mục lục.
Đoạn mã mẫu này cho bạn thấy cách tạo mục lục với siêu liên kết:
using (var presentation = new Presentation())
{
var firstSlide = presentation.Slides[0];
var secondSlide = presentation.Slides.AddEmptySlide(firstSlide.LayoutSlide);
var contentTable = firstSlide.Shapes.AddAutoShape(ShapeType.Rectangle, 40, 40, 300, 100);
contentTable.FillFormat.FillType = FillType.NoFill;
contentTable.LineFormat.FillFormat.FillType = FillType.NoFill;
contentTable.TextFrame.Paragraphs.Clear();
var paragraph = new Paragraph();
paragraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
paragraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
paragraph.Text = "Title of slide 2 .......... ";
var linkPortion = new Portion();
linkPortion.Text = "Page 2";
linkPortion.PortionFormat.HyperlinkManager.SetInternalHyperlinkClick(secondSlide);
paragraph.Portions.Add(linkPortion);
contentTable.TextFrame.Paragraphs.Add(paragraph);
presentation.Save("link_to_slide.pptx", SaveFormat.Pptx);
}
Định Dạng Siêu Liên Kết
Màu Sắc
Với thuộc tính ColorSource trong giao diện IHyperlink, bạn có thể đặt màu cho siêu liên kết và cũng có thể lấy thông tin màu từ siêu liên kết. Tính năng này lần đầu được giới thiệu trong PowerPoint 2019, vì vậy các thay đổi liên quan đến thuộc tính này không áp dụng cho các phiên bản PowerPoint cũ hơn.
Đoạn mã mẫu này minh họa một thao tác trong đó các siêu liên kết với màu sắc khác nhau được thêm vào cùng một slide:
using (Presentation presentation = new Presentation())
{
IAutoShape shape1 = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 450, 50, false);
shape1.AddTextFrame("This is a sample of colored hyperlink.");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick.ColorSource = HyperlinkColorSource.PortionFormat;
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FillFormat.FillType = FillType.Solid;
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FillFormat.SolidFillColor.Color = Color.Red;
IAutoShape shape2 = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 200, 450, 50, false);
shape2.AddTextFrame("This is a sample of usual hyperlink.");
shape2.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
presentation.Save("presentation-out-hyperlink.pptx", SaveFormat.Pptx);
}
Âm Thanh
Aspose.Slides cung cấp các thuộc tính sau để cho phép bạn nhấn mạnh một siêu liên kết bằng âm thanh:
Thêm Âm Thanh Cho Siêu Liên Kết
Đoạn mã C# này cho bạn thấy cách thiết lập siêu liên kết phát âm thanh và dừng nó bằng một siêu liên kết khác:
using (Presentation pres = new Presentation())
{
// Thêm âm thanh mới vào bộ sưu tập âm thanh của bản trình chiếu
IAudio playSound = pres.Audios.AddAudio(File.ReadAllBytes("sampleaudio.wav"));
ISlide firstSlide = pres.Slides[0];
// Thêm hình dạng mới với siêu liên kết tới slide tiếp theo
IShape firstShape = firstSlide.Shapes.AddAutoShape(ShapeType.SoundButton, 100, 100, 100, 50);
firstShape.HyperlinkClick = Hyperlink.NextSlide;
// Kiểm tra siêu liên kết cho "No Sound"
if (!firstShape.HyperlinkClick.StopSoundOnClick && firstShape.HyperlinkClick.Sound == null)
{
// Đặt siêu liên kết phát âm thanh
firstShape.HyperlinkClick.Sound = playSound;
}
// Thêm slide trống
ISlide secondSlide = pres.Slides.AddEmptySlide(firstSlide.LayoutSlide);
// Thêm hình dạng mới với siêu liên kết NoAction
IShape secondShape = secondSlide.Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 100, 50);
secondShape.HyperlinkClick = Hyperlink.NoAction;
// Đặt cờ "Stop previous sound" cho siêu liên kết
secondShape.HyperlinkClick.StopSoundOnClick = true;
pres.Save("hyperlink-sound.pptx", SaveFormat.Pptx);
}
Trích Xuất Âm Thanh Từ Siêu Liên Kết
Đoạn mã C# này cho bạn thấy cách trích xuất âm thanh được sử dụng trong một siêu liên kết:
using (Presentation pres = new Presentation("hyperlink-sound.pptx"))
{
ISlide firstSlide = pres.Slides[0];
// Lấy siêu liên kết của hình dạng đầu tiên
IHyperlink link = firstSlide.Shapes[0].HyperlinkClick;
if (link.Sound != null)
{
// Trích xuất âm thanh siêu liên kết dưới dạng mảng byte
byte[] audioData = link.Sound.BinaryData;
}
}
Xóa Siêu Liên Kết Khỏi Bản Trình Chiếu
Xóa Siêu Liên Kết Khỏi Văn Bản
Đoạn mã C# này cho bạn thấy cách xóa siêu liên kết khỏi văn bản trong một slide của bản trình chiếu:
using (Presentation pres = new Presentation("pres.pptx"))
{
ISlide slide = pres.Slides[0];
foreach (IShape shape in slide.Shapes)
{
IAutoShape autoShape = shape as IAutoShape;
if (autoShape != null)
{
foreach (IParagraph paragraph in autoShape.TextFrame.Paragraphs)
{
foreach (IPortion portion in paragraph.Portions)
{
portion.PortionFormat.HyperlinkManager.RemoveHyperlinkClick();
}
}
}
}
pres.Save("pres-removed-hyperlinks.pptx", SaveFormat.Pptx);
}
Xóa Siêu Liên Kết Khỏi Hình Dạng hoặc Khung
Đoạn mã C# này cho bạn thấy cách xóa siêu liên kết khỏi một hình dạng trong một slide của bản trình chiếu:
using (Presentation pres = new Presentation("demo.pptx"))
{
ISlide slide = pres.Slides[0];
foreach (IShape shape in slide.Shapes)
{
shape.HyperlinkManager.RemoveHyperlinkClick();
}
pres.Save("pres-removed-hyperlinks.pptx", SaveFormat.Pptx);
}
Siêu Liên Kết Có Thể Thay Đổi
Lớp Hyperlink là mutable. Với lớp này, bạn có thể thay đổi giá trị của các thuộc tính sau:
Đoạn mã mẫu cho bạn thấy cách thêm một siêu liên kết vào slide và chỉnh sửa tooltip của nó sau đó:
using (Presentation presentation = new Presentation())
{
IAutoShape shape1 = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 600, 50, false);
shape1.AddTextFrame("Aspose: File Format APIs");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FontHeight = 32;
presentation.Save("presentation-out.pptx", SaveFormat.Pptx);
}
Các Thuộc Tính Được Hỗ Trợ Trong IHyperlinkQueries
Bạn có thể truy cập IHyperlinkQueries từ một bản trình chiếu, slide hoặc văn bản mà siêu liên kết được định nghĩa.
Lớp IHyperlinkQueries hỗ trợ các phương thức và thuộc tính sau:
- IHyperlinkQueries.GetHyperlinkClicks();
- IHyperlinkQueries.GetHyperlinkMouseOvers();
- IHyperlinkQueries.GetAnyHyperlinks();
- IHyperlinkQueries.RemoveAllHyperlinks();
Câu Hỏi Thường Gặp
Làm thế nào tôi có thể tạo điều hướng nội bộ không chỉ tới một slide, mà tới “phần” hoặc slide đầu tiên của một phần?
Các phần trong PowerPoint là các nhóm slide; điều hướng về mặt kỹ thuật vẫn nhắm tới một slide cụ thể. Để “đi tới một phần”, bạn thường liên kết tới slide đầu tiên của phần đó.
Tôi có thể gắn siêu liên kết vào các thành phần của slide chủ (master) để chúng hoạt động trên tất cả các slide không?
Có. Các thành phần của slide chủ và bố cục hỗ trợ siêu liên kết. Các liên kết này sẽ xuất hiện trên các slide con và có thể nhấp được trong khi trình chiếu.
Liệu siêu liên kết có được giữ lại khi xuất ra PDF, HTML, hình ảnh hoặc video không?
Trong PDF và HTML, có, các liên kết thường được giữ lại. Khi xuất ra hình ảnh và video, khả năng nhấp sẽ không được chuyển vì các định dạng này (khung raster/video) không hỗ trợ siêu liên kết.