Treballant amb imatges en presentacions — Aspose.Slides FOSS per a Java

Aspose.Slides FOSS for Java lets you embed images in a presentation’s shared image collection and display them on slides using PictureFrame formes. Les imatges també es poden utilitzar com a emplenaments de fons de forma a través de FillType.PICTURE.


Afegir una imatge des d’un fitxer

Carrega els bytes de la imatge des del disc i afegeix-los a la col·lecció d’imatges de la presentació amb prs.getImages().addImage(). A continuació, col·loca la imatge en una diapositiva com a PictureFrame:

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

try (Presentation prs = new Presentation()) {
    // Add the image to the shared collection
    byte[] imageData = Files.readAllBytes(Path.of("logo.png"));
    var img = prs.getImages().addImage(imageData);

    // Place it on the slide as a PictureFrame
    var slide = prs.getSlides().get(0);
    slide.getShapes().addPictureFrame(ShapeType.RECTANGLE, 50, 50, 300, 200, img);

    prs.save("with-image.pptx", SaveFormat.PPTX);
}

Els quatre arguments posicionals de addPictureFrame() són: x, y, width, height en punts.


Afegir una imatge des de bytes

Si ja tens els bytes de la imatge (p. ex., descarregats d’una URL o llegits d’una base de dades), passa’ls directament a addImage():

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

// Simulate having bytes in memory
byte[] imageBytes = Files.readAllBytes(Path.of("photo.jpg"));

try (Presentation prs = new Presentation()) {
    var img = prs.getImages().addImage(imageBytes);
    prs.getSlides().get(0).getShapes().addPictureFrame(ShapeType.RECTANGLE, 100, 80, 400, 250, img);
    prs.save("from-bytes.pptx", SaveFormat.PPTX);
}

Posicionament i dimensionament d’un PictureFrame

El PictureFrame retornat per addPictureFrame() hereta tots els Shape propietats de geometria i pot ser reubicada després de la creació:

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

try (Presentation prs = new Presentation()) {
    byte[] imageData = Files.readAllBytes(Path.of("photo.jpg"));
    var img = prs.getImages().addImage(imageData);

    var pf = prs.getSlides().get(0).getShapes().addPictureFrame(ShapeType.RECTANGLE, 0, 0, 100, 100, img);

    // Reposition and resize after creation
    pf.setX(50);
    pf.setY(100);
    pf.setWidth(350);
    pf.setHeight(250);

    prs.save("positioned.pptx", SaveFormat.PPTX);
}

Utilitzar una imatge com a emplenament de forma

Qualsevol forma (no només PictureFrame) pot utilitzar una imatge com a farciment de fons. Set FillType.PICTURE i assigna la imatge a getPictureFillFormat().getPicture().setImage():

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.FillType;
import org.aspose.slides.foss.PictureFillMode;
import org.aspose.slides.foss.export.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

try (Presentation prs = new Presentation()) {
    byte[] imageData = Files.readAllBytes(Path.of("background.png"));
    var img = prs.getImages().addImage(imageData);

    var slide = prs.getSlides().get(0);
    var shape = slide.getShapes().addAutoShape(ShapeType.ROUND_CORNER_RECTANGLE, 50, 50, 400, 250);
    shape.getFillFormat().setFillType(FillType.PICTURE);
    shape.getFillFormat().getPictureFillFormat().setPictureFillMode(PictureFillMode.STRETCH);
    shape.getFillFormat().getPictureFillFormat().getPicture().setImage(img);

    prs.save("picture-fill.pptx", SaveFormat.PPTX);
}

PictureFillMode.STRETCH escala la imatge per omplir tota la forma. Use TILE per a patrons repetitius.


Afegir diverses imatges a través de diapositives

Imatges afegides a prs.getImages() es comparteixen a totes les diapositives. El mateix IPPImage objecte es pot utilitzar en diverses diapositives sense duplicar les dades:

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

try (Presentation prs = new Presentation()) {
    byte[] logoData = Files.readAllBytes(Path.of("logo.png"));
    var logo = prs.getImages().addImage(logoData);

    // Add the same image to the first slide
    prs.getSlides().get(0).getShapes().addPictureFrame(ShapeType.RECTANGLE, 600, 10, 100, 40, logo);

    prs.save("shared-image.pptx", SaveFormat.PPTX);
}

Vegeu també

 Català