HI i would like to add to slides and note, the ability i have in word and cell or pdf to extract image as a byte array and replace it with png image byte array
example for pdf
for (com.aspose.pdf.ImageFragment image : pdfDoc.getPages().get_Item(1).getResources().getImages()) {
// Extract the image as a byte array
ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
image.save(imageStream, ImageFormat.getPng());
// Replace the original image with the PNG image
com.aspose.pdf.Image newImage = new com.aspose.pdf.Image();
newImage.setImageStream(new ByteArrayInputStream(imageStream.toByteArray()));
// Get the position and size of the original image
Rectangle rect = image.getRectangle();
float x = rect.getLLX();
float y = rect.getLLY();
float width = rect.getWidth();
float height = rect.getHeight();
// Replace the original image with the new PNG image
pdfDoc.getPages().get_Item(1).getContents().replaceWithNewImage(counter, x, y, width, height, newImage);
You can extract image bytes array using the Image.getBytes() property and then save to different type by yourselves. See the document with example code for your reference.
We are sorry but your issue/required feature is not resolved/supported yet. We will check if we could provide an ETA on it. We will get back to you soon.
Could you please try using the following workaround solution for your requirements:
e.g. Sample code:
String newImagePath = "";
String documentPath = "";
String outputDocumentPath = "";
Image newImage = new Image(newImagePath);
Document document = new Document(documentPath);
Image image = document.getChildNodes(Image.class).get(0);
CompositeNode<IPageChildNode> parent = (CompositeNode<IPageChildNode>) image.getParentNode();
int imagePosition = indexOf((Iterable) parent, image);
parent.removeChild(image);
parent.insertChild(imagePosition, newImage);
document.save(outputDocumentPath);
public static int indexOf(Iterable<Node> source, Image v) {
int i = 0;
for (Node node : source) {
try {
Image imageNode = (Image) node;
if (imageNode.equals(v)) {
return i;
}
} catch(ClassCastException e) {
++i;
}
++i;
}
return -1;
}
If this solution is not sufficient for you, please provide more details about the scenario in which the enhancement will be used. Additionally, please note that simply adding setBytes is not enough as there may be other parameters that could change, such as, FileName, FilePath, Height, and Width.
It would be helpful to know if there is a specific preference to update individual data in the existing image or if it would be more convenient to update all the image information as a whole.
We look forward to hearing back from you and understanding your requirements better.