Ikuti panduan ini tentang cara menambahkan hyperlink ke PowerPoint dengan Java REST API. Anda akan belajar menyisipkan hyperlink di PowerPoint secara otomatis dengan Java RESTful Service menggunakan Cloud SDK berbasis Java yang mendukung semua fitur lanjutan untuk bekerja dengan presentasi. Ini akan membantu Anda dalam memilih slide dan bentuk tertentu untuk menambahkan hyperlink khusus.
Prasyarat
Unduh Aspose.Slides Cloud SDK for Java for inserting hyperlinks
Siapkan proyek Java dengan SDK di atas untuk menambahkan tautan ke suatu bentuk
Langkah-langkah Menambahkan Tautan ke PowerPoint dengan API berbasis Java
- Buat instance SlidesApi dengan kunci API dan rahasia untuk autentikasi
- Baca file input ke dalam aliran memori dan unggah
- Mengatur indeks slide dan bentuk untuk menambahkan hyperlink (indeks berbasis 1)
- Buat objek bentuk dengan hyperlink
- Update bentuk pada slide yang ditentukan dengan hyperlink
- Cetak URL hyperlink yang diperbarui ke konsol untuk verifikasi
- Unduh presentasi yang diperbarui dari cloud sebagai aliran dan simpan di disk
Langkah-langkah ini menjelaskan cara menambahkan hyperlink di PowerPoint dengan Java REST API. Anda dapat membuat Bentuk dan menambahkan hyperlink dengan menentukan jenis tindakan dan URL eksternal. Terakhir, perbarui bentuk target pada slide yang diinginkan menggunakan bentuk yang baru dibuat dan simpan hasilnya pada disk jika diperlukan.
Kode untuk Menyisipkan Tautan di PowerPoint dengan Java Low Code API
import com.aspose.slides.ApiException; | |
import com.aspose.slides.api.SlidesApi; | |
import com.aspose.slides.model.*; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.StandardCopyOption; | |
public class Example_AddHyperlinkInPresentation { | |
protected static SlidesApi presentationApi; | |
public Example_AddHyperlinkInPresentation() { | |
if (presentationApi == null) { | |
presentationApi = new SlidesApi("appSid", "appKey"); | |
} | |
} | |
public void addHyperlinkInSlide() throws ApiException, IOException { | |
String localPath = "/home/downloads/"; | |
String fileName = "PresentationWithHyperlink.pptx"; | |
String storageFolderName = "TempTests"; | |
// Index of the slide and shape where the target shape is located (1-based index). | |
int slideIndex = 2, shapeIndex = 2; | |
Shape shape = new Shape(); // Create a shape object with a hyperlink | |
Hyperlink hyperlink = new Hyperlink(); | |
hyperlink.setActionType(Hyperlink.ActionTypeEnum.HYPERLINK);// Set the action type as a hyperlink. | |
hyperlink.setExternalUrl("https://docs.aspose.cloud/slides"); // The URL for the hyperlink. | |
shape.setHyperlinkClick(hyperlink); | |
// Update the shape on the specified slide with the hyperlink | |
ShapeBase updatedShpWithHyperlink = presentationApi.updateShape(fileName, slideIndex, shapeIndex, shape, | |
null, storageFolderName, null, null); | |
// Print the updated hyperlink's URL to the console for verification. | |
System.out.println(updatedShpWithHyperlink.getHyperlinkClick().getExternalUrl()); | |
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+fileName, null, null); | |
// Copy the downloaded presentation with new image shape to the local directory | |
copyFile(presentationFile, new File(localPath, fileName)); | |
System.out.println("Presentation slide with image shape is copied to: " + localPath + fileName); | |
} | |
public static byte[] readFileToByteArray(String filePath) throws IOException { | |
Path path = new File(filePath).toPath(); | |
return Files.readAllBytes(path); | |
} | |
private void copyFile(File sourceFile, File targetFile) throws IOException { | |
if (sourceFile == null || !sourceFile.exists()) { | |
throw new IOException("Source file does not exist: " + sourceFile); | |
} | |
// Ensure the target directory exists | |
Path targetPath = targetFile.toPath(); | |
Files.createDirectories(targetPath.getParent()); | |
// Copy the file | |
Files.copy(sourceFile.toPath(), targetPath, StandardCopyOption.REPLACE_EXISTING); | |
} | |
} |
Kode ini menunjukkan cara menyisipkan hyperlink di PowerPoint dengan Java REST Interface. Anda dapat mengatur berbagai properti hyperlink seperti tanda untuk mengaktifkan/menonaktifkan hyperlink, tooltip, riwayat, sorot klik, dan menghentikan suara saat klik. Perhatikan bahwa semua properti ini tersedia dengan mengarahkan kursor ke hyperlink, bukan mengkliknya.
Artikel ini mengajarkan kita untuk membuat hyperlink di PowerPoint dengan Java Low Code API. Untuk menambahkan SmartArt di slide, lihat artikel di Tambahkan SmartArt ke PowerPoint dengan Java REST API.