I have a docx file(lets call it main file) with another docx file(lets call it second) embedded into the main file. What I’m trying to do is to take the second file and swap it with another(third) docx file the third docx file should be at the exact same place and size as the “second” file appears in the main file.
here is the code i have:
public void trying(Document document, JSONObject config, Shape shape) throws Exception {
// Get the OleFormat of the shape
OleFormat oleFormat = shape.getOleFormat();
// Check if it's a Word document
if (oleFormat.getProgId().equals("Word.Document.12")) {
// Get the raw data of the OleObject
byte[] oleData = oleFormat.getRawData();
// Write the data to a file
File outputFile = new File("/home/gal/Desktop/" + shape.getName() + ".docx");
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
fos.write(oleData);
}
// Now you can process the Word document as needed
// For example, sanitize the document using the CDR function
String sanitizedFilePath = "/home/gal/Desktop/second.docx";
// After sanitizing, you need to create a new shape with the sanitized OLE object
// First, remove the original shape
shape.remove();
// Then, create a new shape and add it to the document
Shape newShape = new Shape(document, ShapeType.OLE_OBJECT);
// Set the OLE data of the new shape
newShape.getOleFormat().setRawData(Files.readAllBytes(Paths.get(sanitizedFilePath)));
// Add the new shape to the document
document.getFirstSection().getBody().getFirstParagraph().appendChild(newShape);
}
}
i couldnt find a way to do it in java so i wrote : newShape.getOleFormat().setRawData(Files.readAllBytes(Paths.get(sanitizedFilePath)));
even though i know that there is no such function as setRawData i wrote it like that in order to make it easier to understand what I’m trying to do please help to implement this code.