이 가이드를 따라 Java REST API로 PowerPoint에 그림을 추가하는 방법을 알아보세요. Java 기반 Cloud SDK를 사용하여 Java Low Code API로 PowerPoint에 그림을 추가하는 방법을 알아보세요. 이 문서에서는 슬라이드에 그림을 추가하기 전에 그림을 사용자 정의하는 다양한 속성에 대해 설명합니다.
필수 조건
다운로드 Aspose.Slides Cloud SDK for Java for inserting images into the slides
위 SDK를 사용하여 Java 프로젝트를 설정하여 슬라이드에 이미지를 삽입합니다.
Java 기반 API를 사용하여 PowerPoint에 그림을 추가하는 단계
- SlidesApi 객체를 인스턴스화하여 슬라이드에 그림을 삽입합니다.
- 이미지를 추가할 대상 프레젠테이션 파일을 업로드하세요.
- 필요한 형식으로 이미지 데이터를 준비하세요
- 슬라이드에 배치할 PictureFrame 객체를 만듭니다.
- CreateShape 메서드를 호출하여 특정 슬라이드에 이미지를 삽입합니다.
- 이미지를 추가한 후 파일을 다운로드하세요
이 단계는 Java REST Interface로 PowerPoint에 그림을 추가하는 방법을 요약한 것입니다. 소스 프레젠테이션을 클라우드 스토리지에 업로드하고 Convert 네임스페이스에서 ToBase64String() 메서드를 사용하여 이미지를 준비하고 이 이미지를 사용하여 PictureFrame 객체를 만듭니다. 마지막으로 CreateShape() 메서드를 호출하여 지정된 슬라이드에 그림을 추가하고 업데이트된 프레젠테이션을 다운로드합니다.
Java REST API를 사용하여 PowerPoint 슬라이드에 그림을 추가하는 코드
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; | |
import java.util.Base64; | |
public class Example_AddPictureInPresentation { | |
protected static SlidesApi presentationApi; | |
public Example_AddPictureInPresentation() { | |
if (presentationApi == null) { | |
presentationApi = new SlidesApi("appSid", "appKey"); | |
} | |
} | |
public void addPictureInSlide() throws ApiException, IOException { | |
String localPath = "/home/downloads/"; | |
String fileName = "Sample.pptx"; | |
String imageFileName = "ShapeImage.png"; | |
String storageFolderName = "TempTests"; | |
presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(localPath + fileName),null); | |
PictureFrame imageFrame = new PictureFrame(); | |
imageFrame.setX(50.0); | |
imageFrame.setY(50.0); | |
imageFrame.setWidth(350.0); | |
imageFrame.setHeight(250.0); | |
PictureFill pictureFill1 = new PictureFill(); | |
pictureFill1.setPictureFillMode(PictureFill.PictureFillModeEnum.STRETCH); | |
pictureFill1.setBase64Data(Base64.getEncoder().encodeToString(readFileToByteArray(localPath + imageFileName))); | |
imageFrame.setPictureFillFormat(pictureFill); | |
// Add the image to the third slide of the presentation. | |
ShapeBase shapeResponse = presentationApi.createShape(fileName, 3, imageFrame, null, null, | |
null,storageFolderName, null, null); | |
// Output the URI of the newly added image shape. | |
System.out.println("Image added at: "+ shapeResponse.getSelfUri().getHref()); | |
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); | |
} | |
} |
이 샘플 코드는 Java RESTful Service로 PowerPoint에 그림을 넣는 방법을 보여줍니다. PictureFrame 객체에서 X 및 Y 속성을 설정하여 그림의 시작 위치를 정의하고, PictureFill 클래스 객체를 사용하여 그림 채우기 모드를 정의할 수 있습니다. PictureFrame 속성은 원래 크기에 관계없이 슬라이드의 이미지 크기를 정의합니다.
이 글에서는 사진을 추가하는 방법을 알려드렸습니다. 프레젠테이션에 메모를 추가하려면 Java REST API를 사용하여 PowerPoint 슬라이드에 메모 추가의 글을 참조하세요.