We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to export range of such objects into image and replace it

Dear, I have a complex problem here. There is a set of shapes, I want convert them into one image and replace these shapes with this image. How can I do this?63bd2a363f3cb00009bfe55e.docx (207.6 KB)

image.png (41.1 KB)

I think we can collect these shapes into one group and then convert it? So, how can I get this shape in Python code? and set all of them into one group?

@ZZZ21321 I am afraid there is no way to achieve this using Aspose.Words. The possible way is render whole page and then crop the required area. However, dues to the defect WORDSNET-24828 it is not possible to determine the required area using LayoutCollector and LayoutEnumerator.
Once the issue is resolved it will be possible to use code like this to render re required area:

import aspose.words as aw
import aspose.pydrawing as pydraw
from PIL import Image

doc = aw.Document("C:\\Temp\\in.docx")

# Use LayoutCollector and LayoutEnumerator to calculate coordinates of the shapes.
collector = aw.layout.LayoutCollector(doc)
enumerator = aw.layout.LayoutEnumerator(doc)

rect = pydraw.RectangleF()

# Get the shapes.
shapes = doc.get_child_nodes(aw.NodeType.SHAPE, True)
for s in shapes :
    shape = s.as_shape()
    # skip shapes from header/footer.
    if shape.get_ancestor(aw.NodeType.BODY) == None :
        continue

    # process only toplevel, floating shapes
    if not shape.is_top_level or shape.is_inline :
        continue
        
    enumerator.current = collector.get_entity(shape)
    # Process only the first page.
    if enumerator.page_index>1 :
        break

    if rect.is_empty :
        rect = enumerator.rectangle
    else :
        rect = pydraw.RectangleF.union(rect, enumerator.rectangle)

# Now render the first page to image.
resolution = 300
imgSaveOptions = aw.saving.ImageSaveOptions(aw.SaveFormat.JPEG)
imgSaveOptions.horizontal_resolution = resolution
imgSaveOptions.vertical_resolution = resolution
doc.save("C:\\Temp\\tmp.jpg", imgSaveOptions)

# Now get the area of the image we have detected earlier.
img = Image.open("C:\\Temp\\tmp.jpg")
left = aw.ConvertUtil.point_to_pixel(rect.left, resolution)
top = aw.ConvertUtil.point_to_pixel(rect.top, resolution)
right = aw.ConvertUtil.point_to_pixel(rect.right, resolution)
bottom = aw.ConvertUtil.point_to_pixel(rect.bottom, resolution)
crop_rectangle = (left, top, right, bottom)
cropped_im = img.crop(crop_rectangle)

cropped_im.save("C:\\Temp\\cropped.jpg")

but currently due to the mentioned defect, the code fails on this line enumerator.current = collector.get_entity(shape)

With synthetically specified bounding rectangle (calculated using the similar code but in .NET version of Aspose.Words) I get the following image: cropped.jpg (249.6 KB)

We will keep you informed and let you know once the issue is resolved.