Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

     

Add Hyperlinks to a PowerPoint Document in Java

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:

  1. Add hyperlink to text
  2. Add hyperlink to image
  3. 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>
Enter fullscreen modeExit fullscreen mode

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);}}
Enter fullscreen modeExit fullscreen mode

Output:
Alt Text

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);}}
Enter fullscreen modeExit fullscreen mode

Output:
Alt Text

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);}}
Enter fullscreen modeExit fullscreen mode

Output:
Alt Text

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Your Office Development Master
  • Location
    Chengdu, SiChuan, China
  • Work
    Dev at E-iceblue
  • Joined

More fromE-iceblue Product Family

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp