I need to replace images in an Excel header based on the file name.
I can’t find a way to retrieve the file name from the picture header script command (“&G”) .
Is there any way to achieve this using Aspose.Cells for Java with version 24.12?
I am able to get the picture using worksheet.getPageSetup().getPicture(true, idx)
. The method “getSourceFullName” always returns null and I can’t find another suitable method.
Many thanks in advance!
@infra.corix
The images in the header and footer do not contain the file names of the pictures, they contain picture data. If you need to replace the picture, please use the Picture.setData method to directly replace the picture data. Please refer to the following example code. Please refer to the attachment. header.zip (37.3 KB)
Workbook workbook = new Workbook(filePath + "test.xlsx");
Worksheet sheet = workbook.getWorksheets().get(0);
Picture picture = sheet.getPageSetup().getPicture(true, 0);
byte[] imageData = readImageToByteArray(filePath + "b.png");
picture.setData(imageData);
workbook.save(filePath + "out_java.xlsx");
public static byte[] readImageToByteArray(String imagePath) {
File file = new File(imagePath);
ByteArrayOutputStream byteArrayOutputStream = null;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
}
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
try {
if (fileInputStream != null) {
fileInputStream.close();
}
if (byteArrayOutputStream != null) {
byteArrayOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Hope helps a bit.
1 Like
@infra.corix,
This is known limitation of MS Excel for headers/footers pictures. Please note, the &G
script command in Excel headers typically doesn’t preserve the original file name after the image is embedded in the document header. That is why getSourceFullName()
returns null because the filename information simply isn’t stored in the Excel file format itself.
I guess you may try some alternative approach for your needs.
e.g.,
When adding the image initially, store the filename in document properties:
workbook.getWorksheets().getCustomDocumentProperties().add("HeaderImage1", "original_filename.jpg");
Then, later on retrieve it:
String originalName = workbook.getWorksheets().getCustomDocumentProperties().get("HeaderImage1").toString();
Now evaluate if you need to change the header picture based on above data.
The custom properties approach (mentioned above) can work well if you control the file creation with header picture settings. But, if you’re working with some existing files created elsewhere, there is no good way to accomplish the task although you may evaluate the retrieved picture based on image size or contents, etc.
1 Like
Thanks for your input!
Unfortunately, I don’t have control over the file creation.
Your answes is still very helpful! I just wanted to make sure there is no way of retrieving the file name before I try to figure out another solution.
Thanks for your reply!
I will use this approach for replacing the image.
@infra.corix
Thank you for your feedback. You are welcome. Please take your time to try the suggested solutions. Hopefully, your issue will be sorted out. Please let us know your feedback.