PC: Windows 10 Pro
OpenOffice 4.1.10
My requirement is to search and replace Text Pattern ({{Customer_Photo}}) with a "graphicObjShape" for example. where this {{Customer_Photo}} exists multiple times in the document
Iterating through the multiple discovered instances (XIndexAccess) The (bitmapContainer.insertByName(ElementName, Img);) expects unique ElementNames and throws Exception // Exception com.sun.star.container.ElementExistException
Question:
What is Best Practice To Insert Images where the "ElementNames " in the document are repeated?
Some Code:
Code: Select all
// Set Search Pattern
xSearchDescriptor.setSearchString({{Customer_Photo}});
String ElementName = "Customer_Photo"
XTextCursor curTextView = xText.createTextCursor();
// Find All xInterfaceIndex Instances
XIndexAccess xInterfaceIndex = (XIndexAccess) xSearchable.findAll(xSearchDescriptor);
for (int z = 0; z < xInterfaceIndex.getCount(); z++) {
XInterface xInterface = (XInterface) xSearchable.findNext(curTextView, xSearchDescriptor);
if (xInterface != null) {
XTextRange xTextRange = (com.sun.star.text.XTextRange) UnoRuntime.queryInterface(
com.sun.star.text.XTextRange.class, xInterface);
try {
curTextView.gotoRange(xTextRange, true);
} catch (Exception u) {
continue;
}
}
XTextCursor xTextCursor = (com.sun.star.text.XTextCursor) UnoRuntime.queryInterface(
com.sun.star.text.XTextCursor.class, xInterface);
XTextDocument xTextDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xWriterComponent);
XController xController = xTextDoc.getCurrentController();
XTextViewCursorSupplier supTextViewCursor = (XTextViewCursorSupplier) UnoRuntime.queryInterface(XTextViewCursorSupplier.class, xController);
curTextView = supTextViewCursor.getViewCursor();
XMultiServiceFactory xMSFDoc = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
Object graphicObjShape = null;
graphicObjShape = xMSFDoc.createInstance("com.sun.star.drawing.GraphicObjectShape");
XShape shapeSettings = (XShape) UnoRuntime.queryInterface(XShape.class, graphicObjShape);
shapeSettings.setSize(new Size(1000, 1000));
// Creating bitmap container service
XNameContainer bitmapContainer = UnoRuntime.queryInterface(XNameContainer.class,
xMSFDoc.createInstance("com.sun.star.drawing.BitmapTable"));
// Inserting test image to the containerElementName
System.out.println("insertByName: ["+ElementName+"]");
bitmapContainer.insertByName(ElementName, signatureImg);
// [b]Exception com.sun.star.container.ElementExistException is thrown as expected[/b]
XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, graphicObjShape);
// Setting Signature image internal URL to graphic shape property
xPropSet.setPropertyValue("GraphicURL", bitmapContainer.getByName(ElementName));
// Convert graphic shape to the text content item
XTextContent xTextContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, graphicObjShape);
// Embed image into the document text with replacement
xText.insertTextContent(xTextCursor, xTextContent, true);
}
Best Regards