Hyperlink (4)
Hyperlinks in PowerPoint are clickable objects that enable you to jump to another slide in the same/different PowerPoint document or to a specific website. They are a great way to add additional resources to your slides, and they can also make the document more interactive. In addition to adding hyperlinks to PowerPoint documents, Spire.Presentation for Java also supports modifying and removing the existing hyperlinks. This article is going to introduce how to achieve the above two functions.
Install Spire.Presentation for Java
First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.presentation</artifactId> <version>9.4.5</version> </dependency> </dependencies>
Modify Hyperlinks in PowerPoint
With Spire.Presentation for Java, you are allowed to set a new hyperlink address and display text for the existing hyperlink. The following are the detailed steps to modify a hyperlink in PowerPoint.
- Create a Presentation object and load a PowerPoint document using Presentation.loadFromFile() method.
- Get a specified slide using Presentation.getSlides().get() method.
- Get the shapes of the specified slide using getShapes() method under ISlide interface, and then get the specified shape that contains the hyperlink using ShapeList.get() method.
- Get the text range of the existing hyperlink using IAutoShape.getTextFrame().getTextRange() method, and then set a new hyperlink display text for the text range using PortionEx.setText() method.
- Get the existing clickable hyperlink of the text range using TextCharacterProperties.getClickAction() method, and then set a new hyperlink address using ClickHyperlink.setAddress() method.
- Save the document to file using Presentation.saveToFile() method.
- Java
import com.spire.presentation.*; public class modifyHyperlink { public static void main(String[] args) throws Exception { //Create a Presentation object and load a PowerPoint document Presentation presentation = new Presentation(); presentation.loadFromFile("test.pptx"); //Get the shape that contains the hyperlink IAutoShape shape = (IAutoShape)presentation.getSlides().get(0).getShapes().get(0); //Edit the hyperlink text and address shape.getTextFrame().getTextRange().setText("Spire.Presentation for Java"); shape.getTextFrame().getTextRange().getClickAction().setAddress("https://www.e-iceblue.com/Introduce/presentation-for-java.html"); //Save to file. presentation.saveToFile("ModifyHyperlink.pptx", FileFormat.PPTX_2013); } }
Remove Hyperlinks in PowerPoint
When a hyperlink needs to be removed for some reason, Spire.Persentation for Java provides the TextCharacterProperties.setClickAction() method. And by setting its value to null, you could remove the hyperlink easily in PowerPoint. The detailed steps are as follows:
- Create a Presentation object and load a PowerPoint document using Presentation.loadFromFile() method.
- Get a specified slides using Presentation.getSlides().get() method.
- Get the shapes of the specified slide using getShapes() method under ISlide interface, and then get the specified shape that contains the hyperlink using ShapeList.get() method.
- Get the text range of the existing hyperlink using IAutoShape.getTextFrame().getTextRange() method, and then remove the hyperlink by setting the value of the TextCharacterProperties.setClickAction() method to null.
- Save the document to file using Presentation.saveToFile() method.
- Java
import com.spire.presentation.*; public class removeHyperlink { public static void main(String[] args) throws Exception { //Create a Presentation object and load a PowerPoint document Presentation presentation = new Presentation(); presentation.loadFromFile("test.pptx"); //Get the shape that contains the hyperlink. IAutoShape shape = (IAutoShape)presentation.getSlides().get(0).getShapes().get(0); //Set the click action to null to remove the hyperlink. shape.getTextFrame().getTextRange().setClickAction(null); //Save to file. presentation.saveToFile("RemoveHyperlink.pptx", FileFormat.PPTX_2013); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
This article demonstrates how to add a hyperlink that links to a specific slide within the presentation by using Spire.Presnetation for Java.
import com.spire.presentation.*; import com.spire.presentation.drawing.FillFormatType; import java.awt.geom.Rectangle2D; public class LinkToSpecificSlide { public static void main(String[] args) throws Exception { //Create a Presentation object Presentation presentation = new Presentation(); presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9); //Append a slide to it (there are two slides in the presentation including the default one) presentation.getSlides().append(); //Get the first slide ISlide firstSlide = presentation.getSlides().get(0); //Add a shape to it IAutoShape shape = firstSlide.getShapes().appendShape(ShapeType.RECTANGLE,new Rectangle2D.Float(10, 50, 200, 50)); shape.getFill().setFillType(FillFormatType.NONE); shape.getLine().setFillType(FillFormatType.NONE); //Add text to shape shape.getTextFrame().setText("Jump to the second slide"); //Set a hyperlink for the shape, linking to the second slide ClickHyperlink hyperlink = new ClickHyperlink(presentation.getSlides().get(1)); shape.setClick(hyperlink); shape.getTextFrame().getTextRange().setClickAction(hyperlink); //Save to another file presentation.saveToFile("LinkToSlide.pptx", FileFormat.PPTX_2013); } }
This article demonstrates how to add a hyperlink to an image in a PowerPint slide using Spire.Presentation for Java.
import com.spire.presentation.*; import com.spire.presentation.drawing.FillFormatType; import java.awt.geom.Rectangle2D; public class AddHyperlinkToImage { public static void main(String[] args) throws Exception { //create a Presentation object Presentation presentation = new Presentation(); //get the first slide ISlide slide = presentation.getSlides().get(0); //add an image to slide String imaPath = "C:\\Users\\Administrator\\Desktop\\logo.png"; Rectangle2D.Float rect = new Rectangle2D.Float(50, 50, 220, 60); IEmbedImage image = slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE, imaPath, rect); //set the line of image shape to none image.getLine().setFillType(FillFormatType.NONE); //add a hyperlink to image ClickHyperlink hyperlink = new ClickHyperlink("https://www.e-iceblue.com"); image.setClick(hyperlink); //save the file presentation.saveToFile("output/ImageHyperLink.pptx", FileFormat.PPTX_2013); } }
This article demonstrate how to add hyperlinks to a PowerPoint slide using Spire.Presentation for Java.
import com.spire.presentation.*; import com.spire.presentation.drawing.FillFormatType; import java.awt.geom.Rectangle2D; public class AddHyperlink { public static void main(String[] args) throws Exception { //Create a Presentation instance Presentation presentation = new Presentation(); //Add a shape Rectangle2D.Double rec = new Rectangle2D.Double(presentation.getSlideSize().getSize().getWidth() / 2 - 255, 120, 300, 150); IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rec); shape.getFill().setFillType(FillFormatType.NONE); shape.getLine().setWidth(0); shape.getTextFrame().getParagraphs().clear(); //Add some paragraphs with hyperlinks to the shape ParagraphEx para1 = new ParagraphEx(); PortionEx tr1 = new PortionEx(); tr1.setText("Spire.Presentation for Java"); tr1.getClickAction().setAddress("https://www.e-iceblue.com/Introduce/presentation-for-java.html"); tr1.setLatinFont(new TextFont("Lucida Sans Unicode")); tr1.setFontHeight(20); para1.getTextRanges().append(tr1); shape.getTextFrame().getParagraphs().append(para1); shape.getTextFrame().getParagraphs().append(new ParagraphEx()); ParagraphEx para2 = new ParagraphEx(); PortionEx tr2 = new PortionEx(); tr2.setText("Spire.Presentation for .NET"); tr2.getClickAction().setAddress("https://www.e-iceblue.com/Introduce/presentation-for-net-introduce.html"); tr2.setLatinFont(new TextFont("Lucida Sans Unicode")); tr2.setFontHeight(20); para2.getTextRanges().append(tr2); shape.getTextFrame().getParagraphs().append(para2); //Save the document presentation.saveToFile("AddHyperlink.pptx", FileFormat.PPTX_2010); } }
Output: