In Microsoft PowerPoint, you can add hyperlinks to text or images to link to websites, email addresses or even link to a place in the same document such as a specific slide. In this article, I will introduce how to add hyperlinks to a PowerPoint document programmatically in Java by using Free Spire.Presentation for Java API.
Contents Summary:
- Add hyperlink to text
- Add hyperlink to image
- Add hyperlink to link to a specific slide
Add Dependencies
First of all, you need to add needed dependencies for including Free Spire.Presentation for Java into your Java project. There are two ways to do that.
If you use maven, you need to add the following code to your project’s pom.xml file.
<repositories><repository><id>com.e-iceblue</id><name>e-iceblue</name><url>http://repo.e-iceblue.com/nexus/content/groups/public/</url></repository></repositories><dependencies><dependency><groupId>e-iceblue</groupId><artifactId>spire.presentation.free</artifactId><version>2.6.1</version></dependency></dependencies>
For non-maven projects, download Free Spire.Presentation for Java pack fromthis website, unzip the package and add Spire.Presentation.jar in the lib folder into your project as a dependency.
Add hyperlink to text
importcom.spire.presentation.*;importcom.spire.presentation.drawing.FillFormatType;importjava.awt.geom.Rectangle2D;publicclassTextHyperlink{publicstaticvoidmain(String[]args)throwsException{//create a Presentation instancePresentationpresentation=newPresentation();//add a shape to the first slideRectangle2D.Doublerec=newRectangle2D.Double(presentation.getSlideSize().getSize().getWidth()/2-255,120,500,280);IAutoShapeshape=presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE,rec);shape.getFill().setFillType(FillFormatType.NONE);shape.getLine().setFillType(FillFormatType.NONE);//add some paragraphs with hyperlinks to the shapeParagraphExpara1=newParagraphEx();PortionExtr1=newPortionEx();tr1.setText("Click to visit Google.");//link to a websitetr1.getClickAction().setAddress("https://www.google.com");para1.getTextRanges().append(tr1);shape.getTextFrame().getParagraphs().append(para1);shape.getTextFrame().getParagraphs().append(newParagraphEx());ParagraphExpara2=newParagraphEx();PortionExtr2=newPortionEx();tr2.setText("Contact Google via email.");//link to an email addresstr2.getClickAction().setAddress("mailto:contactxxx@google.com");para2.getTextRanges().append(tr2);shape.getTextFrame().getParagraphs().append(para2);shape.getTextFrame().getParagraphs().append(newParagraphEx());//loop through the paragraphs in the shape, set font name and height for the text in each paragraphfor(Objectpara:shape.getTextFrame().getParagraphs()){ParagraphExparagraph=(ParagraphEx)para;Stringtext=paragraph.getText();if(text!=null&&text.length()!=0){paragraph.getTextRanges().get(0).setLatinFont(newTextFont("Lucida Sans Unicode"));paragraph.getTextRanges().get(0).setFontHeight(20);}}//save the documentpresentation.saveToFile("AddHyperlinktoText.pptx",FileFormat.PPTX_2010);}}
Add hyperlink to image
importcom.spire.presentation.*;importjava.awt.geom.Rectangle2D;publicclassImageHyperlink{publicstaticvoidmain(String[]args)throwsException{//create a Presentation instancePresentationpresentation=newPresentation();//get the first slideISlideslide=presentation.getSlides().get(0);//add an image to the slideRectangle2D.Doublerect=newRectangle2D.Double(100,150,200,100);IEmbedImageimage=slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE,"logo.png",rect);//add hyperlink to the imageClickHyperlinkhyperlink=newClickHyperlink("https://www.google.com");image.setClick(hyperlink);//save the documentpresentation.saveToFile("AddHyperlinkToImage.pptx",FileFormat.PPTX_2010);}}
Add hyperlink to link to a specific slide
importcom.spire.presentation.*;importcom.spire.presentation.drawing.FillFormatType;importjava.awt.*;publicclassSlideHyperlink{publicstaticvoidmain(String[]args)throwsException{//create a Presentation instancePresentationpresentation=newPresentation();//append a new slidepresentation.getSlides().append();//add a shape to the new added slideRectanglerec=newRectangle((int)presentation.getSlideSize().getSize().getWidth()/2-250,120,400,100);IAutoShapeshape=presentation.getSlides().get(1).getShapes().appendShape(ShapeType.RECTANGLE,rec);shape.getFill().setFillType(FillFormatType.NONE);shape.getLine().setFillType(FillFormatType.NONE);shape.getTextFrame().setText("Jump to the first slide.");//add a hyperlink to the shape to link to the first slideClickHyperlinkhyperlink=newClickHyperlink(presentation.getSlides().get(0));shape.setClick(hyperlink);shape.getTextFrame().getTextRange().setClickAction(hyperlink);//save the documentpresentation.saveToFile("AddHyperlinkToSlide.pptx",FileFormat.PPTX_2010);}}
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse