Mapping xml in a PICTURE sdt inserts a blank image

Hey there.

I’m using ASpose words for Java.

When I try to insert an SDT and then add an XML mapping, some node is automatically inserted into the content. I have an example with a PICTURE sdt, but it has the same behavior with a rich text.

Here’s an example of the code I’m using:

final var p2 = builder.insertParagraph();
final var sdt = new StructuredDocumentTag(doc, SdtType.PICTURE, MarkupLevel.INLINE);
p2.appendChild(sdt);

final var customXml = doc.getCustomXmlParts().add("01", "<root><picture-sdt></picture-sdt></root>");
sdt.getXmlMapping().setMapping(customXml, "/root[1]/picture-sdt[1]", null);

final var b64Image = "iVBORw0KGgoAAAANSUhEUgAAANQAAAApBAMAAAC//BrsAAAAMFBMVEXr6+uZmZmlpaXX19exsbHNzc3Hx8fh4eG3t7e8vLzm5uarq6ufn5/c3NzCwsLS0tKxgbZRAAABJElEQVR4Xu3SsUrDUBjF8UOamyYdNF9RA1YxClIQCr33CVIkoJ0uWV2igzhJ0sWlQwURx1KKdBR1c9FJECxOdlHIIyiCs4IPoEUXx0LG7/cCfw4cTIYxxhhjl9SUOKwkG1Cwg5SoH1vrVM6IusAjJNUkzeIIOdg5G0qr6gQNrdAewdDmT8p3Oh4AsQKFgQKKT0kOqUhDmjGyzZHC/u7/lPlV/01Z73EOKeumKU297IYDhWrrL0WVjKiOluhKupfkGgc+clAaSvul4IbnvQLRh6Ht8aoteAC2aV6JawVIWsqh1MeJFM/CDafLTmx1DD11d/XmG3qcquFYYVUBp9jL5YG3Eq8LOkQvzYqeoUuNmQtfrBEF4gGRQvRJc4tIE0yCMcYYY9+y3T7mY1+w0gAAAABJRU5ErkJggg==";
final var imageBytes = Base64.getDecoder().decode(b64Image);
final var sdtShape = (Shape) sdt.getFirstChild();
sdtShape.setAspectRatioLocked(false);
sdtShape.setWidth(192f);
sdtShape.setHeight(42f);
sdtShape.getImageData().setImage(new ByteArrayInputStream(imageBytes));

With that code, the document is unreadable in Word, and if I recover it, I have this result:

How could I insert an image in a XML mapped picture SDT without breaking the doc (and the control) ?

@concord_tech

It seems that you are encountering an issue with inserting a Structured Document Tag (SDT) of type PICTURE and binding it to a custom XML part, which results in a blank image being inserted into your document. This behavior can occur due to several reasons related to how the SDT is set up and how the image data is being handled.

Here are some steps and considerations to help you troubleshoot and resolve the issue:

  1. Ensure Proper XML Mapping: Make sure that the XML mapping you are setting for the SDT is correct. The XPath expression you are using should accurately point to the node in your custom XML part that contains the image data. In your case, you are mapping to "/root[1]/picture-sdt[1]". Verify that this path corresponds to the correct node in your XML structure.

  2. Image Data Handling: When setting the image data for the SDT, ensure that the image bytes are correctly decoded from Base64 and that the Shape object is properly configured. You are currently using:

    final var imageBytes = Base64.getDecoder().decode(b64Image);
    sdtShape.getImageData().setImage(new ByteArrayInputStream(imageBytes));
    

    Make sure that the b64Image string is a valid Base64 representation of an image. If the image data is not valid, it may result in a blank image.

  3. Check SDT Type: Ensure that the SDT type is correctly set to PICTURE. If you are also using rich text SDTs, confirm that the correct type is being used for each SDT you are working with.

  4. Document Structure: After inserting the SDT and setting the image, check the overall structure of the document. Sometimes, issues can arise from how the document is structured or if there are conflicting elements.

  5. Testing with Simplified Code: To isolate the issue, consider testing with a simplified version of your code. For example, try creating a new document with just the SDT and the image setting code to see if the problem persists.

Here is a simplified example of how to insert a PICTURE SDT and set an image:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.PICTURE, MarkupLevel.INLINE);
builder.insertNode(sdt);

String b64Image = "iVBORw0KGgoAAAANSUhEUgAAANQAAAApBAMAAAC//BrsAAAAMFBMVEXr6+uZmZmlpaXX19exsbHNzc3Hx8fh4eG3t7e8vLzm5uarq6ufn5/c3NzCwsLS0tKxgbZRAAABJElEQVR4Xu3SsUrDUBjF8UOamyYdNF9RA1YxClIQCr33CVIkoJ0uWV2igzhJ0sWlQwURx1KKdBR1c9FJECxOdlHIIyiCs4IPoEUXx0LG7/cCfw4cTIYxxhhjl9SUOKwkG1Cwg5SoH1vrVM6IusAjJNUkzeIIOdg5G0qr6gQNrdAewdDmT8p3Oh4AsQKFgQKKT0kOqUhDmjGyzZHC/u7/lPlV/01Z73EOKeumKU297IYDhWrrL0WVjKiOluhKupfkGgc+clAaSvul4IbnvQLRh6Ht8aoteAC2aV6JawVIWsqh1MeJFM/CDafLTmx1DD11d/XmG3qcquFYYVUBp9jL5YG3Eq8LOkQvzYqeoUuNmQtfrBEF4gGRQvRJc4tIE0yCMcYYY9+y3T7mY1+w0gAAAABJRU5ErkJggg==";
byte[] imageBytes = Base64.getDecoder().decode(b64Image);
Shape sdtShape = (Shape) sdt.getFirstChild();
sdtShape.getImageData().setImage(new ByteArrayInputStream(imageBytes));

doc.save("output.docx");

By following these steps and ensuring that each part of your implementation is correct, you should be able to resolve the issue with the blank image in your SDT. If the problem persists, consider checking the Aspose documentation or community forums for additional insights.

If you have further questions or need more assistance, feel free to ask!

Thanks for trying dear robot. Unfortunately, the issue happens when there is an XML mapping on the sdt. The provided code doesn’t fix the issue as I want to add a mapping to this control.

The code I shared add my image correctly AND a blank square image in the SDT. I want that blank image gone and only my image showing (and the docx file working in Word)

@concord_tech When SDT is bind to XML, it always uses the value specified in the XML. So the image inserted in your code is not shown. So, the image must be stored in your XML. Unfortunately, I did not find a way to insert a picture from mapped XML into picture SDT in MS Word. If possible, create an expected output in MS Word and attach it here.

Hey Alexei.

Thanks for your answer.

Actually, I found the solution by opening the expected output file with a text editor. It works when inserting the base 64 string directly into the mapped XML element.

Here’s the custom XML part from the zip:

    <root><picture-sdt>iVBORw0KGgoAAAANSUhEUgAAANQAAAA...AABJRU5ErkJggg==</picture-sdt></root>

@concord_tech Could you please attach DOCX document where picture SDT mapped to XML works as expected?

@concord_tech I managed to make is work using the following code:

String b64Image = "iVBORw0KGgoAAAANSUhEUgAAACYAAAAjCAYAAAD48HgdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAOxAAADsQBlSsOGwAAA3NJREFUWIXtl9tvFGUYxn8zszvd3TpgpEiNWRvp9qISibDKHZq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AvZX5BonO7upWLOag03bqVy3tsCz7J9viUSCnr+HsEyLfbvqbKFWtB4DHGlSsGG/zoYAAAAASUVORK5CYII=";
        
Document doc = new Document();
        
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.PICTURE, MarkupLevel.INLINE);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(sdt);
    
CustomXmlPart customXml = doc.getCustomXmlParts().add("01", "<root><picture-sdt>" + b64Image + "</picture-sdt></root>");
sdt.getXmlMapping().setMapping(customXml, "/root[1]/picture-sdt[1]", null);
        
doc.save("C:\\Temp\\out.docx");

That’s the spirit !

Thanks :slight_smile:

1 Like