В Microsoft PowerPoint вы можете добавлять гиперссылки к тексту или изображениям для ссылки на веб-сайты, адреса электронной почты или даже ссылки на место в том же документе, например на определенный слайд. В этой статье я расскажу, как программно добавлять гиперссылки к документу PowerPoint на Java с помощью Free Spire. Презентация для Java API.
Краткое содержание:
- Добавить гиперссылку к тексту
- Добавить гиперссылку на изображение
- Добавить гиперссылку для ссылки на определенный слайд
Добавление зависимостей
Прежде всего, вам нужно добавить необходимые зависимости для включения Free Spire. Презентация для Java в ваш Java-проект. Есть два способа сделать это. Если вы используете maven, вам необходимо добавить следующий код в свой проект pom.xml файл.
com.e-iceblue e-iceblue http://repo.e-iceblue.com/nexus/content/groups/public/ e-iceblue spire.presentation.free 2.6.1
Для проектов, не связанных с maven, скачайте бесплатный Spire. Презентация для пакета Java с этого веб-сайта , распакуйте пакет и добавьте Spire.Presentation.jar в папке lib в вашем проекте в качестве зависимости.
Добавить гиперссылку к тексту
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.geom.Rectangle2D;
public class TextHyperlink {
public static void main(String[] args) throws Exception {
//create a Presentation instance
Presentation presentation = new Presentation();
//add a shape to the first slide
Rectangle2D.Double rec = new Rectangle2D.Double(presentation.getSlideSize().getSize().getWidth() / 2 - 255, 120, 500, 280);
IAutoShape shape = 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 shape
ParagraphEx para1 = new ParagraphEx();
PortionEx tr1 = new PortionEx();
tr1.setText("Click to visit Google.");
//link to a website
tr1.getClickAction().setAddress("https://www.google.com");
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("Contact Google via email.");
//link to an email address
tr2.getClickAction().setAddress("mailto:contactxxx@google.com");
para2.getTextRanges().append(tr2);
shape.getTextFrame().getParagraphs().append(para2);
shape.getTextFrame().getParagraphs().append(new ParagraphEx());
//loop through the paragraphs in the shape, set font name and height for the text in each paragraph
for (Object para : shape.getTextFrame().getParagraphs()) {
ParagraphEx paragraph = (ParagraphEx) para;
String text = paragraph.getText();
if (text != null && text.length() != 0) {
paragraph.getTextRanges().get(0).setLatinFont(new TextFont("Lucida Sans Unicode"));
paragraph.getTextRanges().get(0).setFontHeight(20);
}
}
//save the document
presentation.saveToFile("AddHyperlinktoText.pptx", FileFormat.PPTX_2010);
}
}
Выход:
Добавить гиперссылку на изображение
import com.spire.presentation.*;
import java.awt.geom.Rectangle2D;
public class ImageHyperlink {
public static void main(String[] args) throws Exception {
//create a Presentation instance
Presentation presentation = new Presentation();
//get the first slide
ISlide slide = presentation.getSlides().get(0);
//add an image to the slide
Rectangle2D.Double rect = new Rectangle2D.Double(100, 150, 200, 100);
IEmbedImage image = slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE, "logo.png", rect);
//add hyperlink to the image
ClickHyperlink hyperlink = new ClickHyperlink("https://www.google.com");
image.setClick(hyperlink);
//save the document
presentation.saveToFile("AddHyperlinkToImage.pptx", FileFormat.PPTX_2010);
}
}
Выход:
Добавить гиперссылку для ссылки на определенный слайд
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
public class SlideHyperlink {
public static void main(String[] args) throws Exception {
//create a Presentation instance
Presentation presentation = new Presentation();
//append a new slide
presentation.getSlides().append();
//add a shape to the new added slide
Rectangle rec = new Rectangle((int)presentation.getSlideSize().getSize().getWidth() / 2 - 250, 120, 400, 100);
IAutoShape shape = 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 slide
ClickHyperlink hyperlink = new ClickHyperlink(presentation.getSlides().get(0));
shape.setClick(hyperlink);
shape.getTextFrame().getTextRange().setClickAction(hyperlink);
//save the document
presentation.saveToFile("AddHyperlinkToSlide.pptx", FileFormat.PPTX_2010);
}
}
Выход:
Оригинал: “https://dev.to/eiceblue/add-hyperlinks-to-a-powerpoint-document-in-java-28k4”