จัดการไฮเปอร์ลิงก์ของงานนำเสนอใน Java

บทนำ

ไฮเปอร์ลิงก์คือการอ้างอิงถึงวัตถุหรือข้อมูลหรือสถานที่ในบางอย่าง ซึ่งเป็นไฮเปอร์ลิงก์ที่พบทั่วไปในงานนำเสนอ PowerPoint:

  • ลิงก์ไปยังเว็บไซต์ภายในข้อความ รูปร่าง หรือสื่อ
  • ลิงก์ไปยังสไลด์

Aspose.Slides for Java ช่วยให้คุณทำงานหลายอย่างที่เกี่ยวกับไฮเปอร์ลิงก์ในงานนำเสนอได้

เพิ่มไฮเปอร์ลิงก์ URL

เพิ่มไฮเปอร์ลิงก์ URL ไปยังข้อความ

โค้ด Java นี้แสดงวิธีการเพิ่มไฮเปอร์ลิงก์เว็บไซต์ลงในข้อความ:

Presentation presentation = new Presentation();
try {
	IAutoShape shape1 = presentation.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 600, 50, false);
	shape1.addTextFrame("Aspose: File Format APIs");
	
	IPortionFormat portionFormat = shape1.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat(); 
	portionFormat.setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
	portionFormat.getHyperlinkClick().setTooltip("More than 70% Fortune 100 companies trust Aspose APIs");
	portionFormat.setFontHeight(32);

	presentation.save("presentation-out.pptx", SaveFormat.Pptx);
} finally {
	if (presentation != null) presentation.dispose();
}

เพิ่มไฮเปอร์ลิงก์ URL ไปยังรูปร่างหรือเฟรม

โค้ดตัวอย่างใน Java นี้แสดงวิธีการเพิ่มไฮเปอร์ลิงก์เว็บไซต์ลงในรูปร่าง:

Presentation pres = new Presentation();
try {
	IShape shape = pres.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 600, 50);

	shape.setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
	shape.getHyperlinkClick().setTooltip("More than 70% Fortune 100 companies trust Aspose APIs");

	pres.save("pres-out.pptx", SaveFormat.Pptx);
} finally {
	if (pres != null) pres.dispose();
}

เพิ่มไฮเปอร์ลิงก์ URL ไปยังสื่อ

Aspose.Slides อนุญาตให้คุณเพิ่มไฮเปอร์ลิงก์ไปยังไฟล์รูปภาพ, เสียง, และวิดีโอ.

โค้ดตัวอย่างนี้แสดงวิธีการเพิ่มไฮเปอร์ลิงก์ไปยัง รูปภาพ:

Presentation pres = new Presentation();
try {
	// เพิ่มรูปภาพลงในงานนำเสนอ
    IPPImage picture;
    IImage image = Images.fromFile("image.png");
    try {
    picture = pres.getImages().addImage(picture);
    } finally {
          if (image != null) image.dispose();
    }
	// สร้างกรอบรูปบนสไลด์ 1 จากรูปภาพที่เพิ่มไว้ก่อนหน้า
	IPictureFrame pictureFrame = pres.getSlides().get_Item(0).getShapes().addPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, picture);

	pictureFrame.setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
	pictureFrame.getHyperlinkClick().setTooltip("More than 70% Fortune 100 companies trust Aspose APIs");

	pres.save("pres-out.pptx", SaveFormat.Pptx);
} catch(IOException e) {
} finally {
	if (pres != null) pres.dispose();
}

โค้ดตัวอย่างนี้แสดงวิธีการเพิ่มไฮเปอร์ลิงก์ไปยัง ไฟล์เสียง:

Presentation pres = new Presentation();
try {
	IAudio audio = pres.getAudios().addAudio(Files.readAllBytes(Paths.get("audio.mp3")));
	IAudioFrame audioFrame = pres.getSlides().get_Item(0).getShapes().addAudioFrameEmbedded(10, 10, 100, 100, audio);

	audioFrame.setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
	audioFrame.getHyperlinkClick().setTooltip("More than 70% Fortune 100 companies trust Aspose APIs");

	pres.save("pres-out.pptx", SaveFormat.Pptx);
} catch(IOException e) {
} finally {
	if (pres != null) pres.dispose();
}

โค้ดตัวอย่างนี้แสดงวิธีการเพิ่มไฮเปอร์ลิงก์ไปยัง วิดีโอ:

Presentation pres = new Presentation();
try {
	IVideo video = pres.getVideos().addVideo(Files.readAllBytes(Paths.get("video.avi")));
	IVideoFrame videoFrame = pres.getSlides().get_Item(0).getShapes().addVideoFrame(10, 10, 100, 100, video);

	videoFrame.setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
	videoFrame.getHyperlinkClick().setTooltip("More than 70% Fortune 100 companies trust Aspose APIs");

	pres.save("pres-out.pptx", SaveFormat.Pptx);
} catch(IOException e) {
} finally {
	if (pres != null) pres.dispose();
}

ใช้ไฮเปอร์ลิงก์เพื่อสร้างสารบัญ

เนื่องจากไฮเปอร์ลิงก์ทำให้คุณเพิ่มการอ้างอิงต่อวัตถุหรือสถานที่ได้ คุณจึงสามารถใช้มันสร้างสารบัญได้.

โค้ดตัวอย่างนี้แสดงวิธีสร้างสารบัญพร้อมไฮเปอร์ลิงก์:

Presentation pres = new Presentation();
try {
	ISlide firstSlide = pres.getSlides().get_Item(0);
	ISlide secondSlide = pres.getSlides().addEmptySlide(firstSlide.getLayoutSlide());

	IAutoShape contentTable = firstSlide.getShapes().addAutoShape(ShapeType.Rectangle, 40, 40, 300, 100);
	contentTable.getFillFormat().setFillType(FillType.NoFill);
	contentTable.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
	contentTable.getTextFrame().getParagraphs().clear();

	Paragraph paragraph = new Paragraph();
	paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
	paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
	paragraph.setText("Title of slide 2 .......... ");

	Portion linkPortion = new Portion();
	linkPortion.setText("Page 2");
	linkPortion.getPortionFormat().getHyperlinkManager().setInternalHyperlinkClick(secondSlide);

	paragraph.getPortions().add(linkPortion);
	contentTable.getTextFrame().getParagraphs().add(paragraph);

	pres.save("link_to_slide.pptx", SaveFormat.Pptx);
} finally {
	if (pres != null) pres.dispose();
}

จัดรูปแบบไฮเปอร์ลิงก์

สี

ด้วยคุณสมบัติ ColorSource ในอินเทอร์เฟซ IHyperlink คุณสามารถกำหนดสีให้กับไฮเปอร์ลิงก์และยังสามารถรับข้อมูลสีจากไฮเปอร์ลิงก์ได้ คุณลักษณะนี้ถูกนำมาใช้ครั้งแรกใน PowerPoint 2019 ดังนั้นการเปลี่ยนแปลงที่เกี่ยวกับคุณสมบัตินี้จะไม่ส่งผลกับเวอร์ชัน PowerPoint เก่า.

โค้ดตัวอย่างนี้สาธิตการดำเนินการที่เพิ่มไฮเปอร์ลิงก์หลายสีลงในสไลด์เดียวกัน:

Presentation pres = new Presentation();
try {
	IAutoShape shape1 = pres.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 450, 50, false);
	shape1.addTextFrame("This is a sample of colored hyperlink.");
	IPortionFormat portionFormat = shape1.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
	portionFormat.setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
	portionFormat.getHyperlinkClick().setColorSource(HyperlinkColorSource.PortionFormat);
	portionFormat.getFillFormat().setFillType(FillType.Solid);
	portionFormat.getFillFormat().getSolidFillColor().setColor(Color.RED);

	IAutoShape shape2 = pres.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 200, 450, 50, false);
	shape2.addTextFrame("This is a sample of usual hyperlink.");
	shape2.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat().setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));

	pres.save("presentation-out-hyperlink.pptx", SaveFormat.Pptx);
} finally {
	if (pres != null) pres.dispose();
}

ลบไฮเปอร์ลิงก์จากงานนำเสนอ

ลบไฮเปอร์ลิงก์จากข้อความ

โค้ด Java นี้แสดงวิธีลบไฮเปอร์ลิงก์ออกจากข้อความในสไลด์งานนำเสนอ:

Presentation pres = new Presentation();
try {
	ISlide slide = pres.getSlides().get_Item(0);
	for (IShape shape : slide.getShapes())
	{
		IAutoShape autoShape = (IAutoShape)shape;
		if (autoShape != null)
		{
			for (IParagraph paragraph : autoShape.getTextFrame().getParagraphs())
			{
				for (IPortion portion : paragraph.getPortions())
				{
					portion.getPortionFormat().getHyperlinkManager().removeHyperlinkClick();
				}
			}
		}
	}

	pres.save("pres-removed-hyperlinks.pptx", SaveFormat.Pptx);
} finally {
	if (pres != null) pres.dispose();
}

ลบไฮเปอร์ลิงก์จากรูปร่างหรือเฟรม

โค้ด Java นี้แสดงวิธีลบไฮเปอร์ลิงก์ออกจากรูปร่างในสไลด์งานนำเสนอ:

Presentation pres = new Presentation();
try {
	ISlide slide = pres.getSlides().get_Item(0);
	for (IShape shape : slide.getShapes())
	{
		shape.getHyperlinkManager().removeHyperlinkClick();
	}
	pres.save("pres-removed-hyperlinks.pptx", SaveFormat.Pptx);
} finally {
	if (pres != null) pres.dispose();
}

ไฮเปอร์ลิงก์ที่เปลี่ยนแปลงได้

คลาส Hyperlink สามารถเปลี่ยนแปลงค่าได้ ด้วยคลาสนี้ คุณสามารถเปลี่ยนค่าได้สำหรับคุณสมบัติดังต่อไปนี้:

ส่วนของโค้ดนี้แสดงวิธีเพิ่มไฮเปอร์ลิงก์ไปยังสไลด์และแก้ไข tooltip ของมันภายหลัง:

Presentation pres = new Presentation();
try {
	IAutoShape shape1 = pres.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 600, 50, false);
	shape1.addTextFrame("Aspose: File Format APIs");

	IPortionFormat portionFormat = shape1.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat(); 
	portionFormat.setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
	portionFormat.getHyperlinkClick().setTooltip("More than 70% Fortune 100 companies trust Aspose APIs");
	portionFormat.setFontHeight(32);

	pres.save("presentation-out.pptx", SaveFormat.Pptx);
} finally {
	if (pres != null) pres.dispose();
}

คุณสมบัติที่รองรับใน IHyperlinkQueries

คุณสามารถเข้าถึง IHyperlinkQueries จากงานนำเสนอ, สไลด์, หรือข้อความที่กำหนดไฮเปอร์ลิงก์ไว้.

คลาส IHyperlinkQueries รองรับเมธอดและคุณสมบัติดังต่อไปนี้:

คำถามที่พบบ่อย

ฉันจะสร้างการนำทางภายในไม่ใช่แค่ไปยังสไลด์ แต่ไปยัง “ส่วน” หรือสไลด์แรกของส่วนได้อย่างไร?

ส่วนใน PowerPoint คือการจัดกลุ่มสไลด์; การนำทางโดยเทคนิคมุ่งเป้าหมายที่สไลด์เฉพาะ เพื่อ “นำทางไปยังส่วน” คุณมักจะลิงก์ไปยังสไลด์แรกของส่วนนั้น.

ฉันสามารถแนบไฮเปอร์ลิงก์ไปยังองค์ประกอบสไลด์มาสเตอร์เพื่อให้ทำงานบนทุกสไลด์ได้หรือไม่?

ได้. องค์ประกอบสไลด์มาสเตอร์และเลย์เอาต์รองรับไฮเปอร์ลิงก์ ลิงก์เหล่านี้จะแสดงบนสไลด์ลูกและสามารถคลิกได้ระหว่างการนำเสนอ.

ไฮเปอร์ลิงก์จะถูกรักษาไว้เมื่อส่งออกเป็น PDF, HTML, รูปภาพ หรือวิดีโอหรือไม่?

ใน PDF และ HTML ใช่—ลิงก์จะถูกคงไว้โดยทั่วไป เมื่อส่งออกเป็น images และ video ความสามารถในการคลิกจะไม่ถูกรักษาไว้ เนื่องจากลักษณะของฟอร์แมตเหล่านั้น (เฟรมและวิดีโอแบบราสเตอร์ไม่รองรับไฮเปอร์ลิงก์).