Free Support Forum - aspose.com

Word和excel文件里面的smartart转换成图片

word文件excel文件里面有smartart,怎么把smartart转换成图片?有可以参考的代码吗?

@dropin,

谢谢你的询问。 请在此处压缩并上传您输入的Word文档Excel文件以进行测试。 我们将在最后调查该方案并为您提供更多信息。

smartart.zip (63.1 KB)
@ awais.hafeez 文件如上

@dropin,

您可以使用以下Aspose.Words for .NET代码将Word文档中的这些形状转换为图像。

Document doc = new Document("D:\\Temp\\SmartArt\\in.docx");

ArrayList list = new ArrayList();
foreach(Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    if (shape.HasChart || !shape.HasImage)
    {
        ShapeRenderer renderer = shape.GetShapeRenderer();
        using (MemoryStream ms = new MemoryStream())
        {
            renderer.Save(ms, new ImageSaveOptions(SaveFormat.Png));
            DocumentBuilder builder = new DocumentBuilder(doc);
            builder.MoveTo(shape);
            builder.InsertImage(ms);
            list.Add(shape);
        }
    }
}

foreach (Shape shape in list)
    shape.Remove();

doc.Save("D:\\temp\\SmartArt\\18.8.docx"); 

我们正在处理与Aspose.Cells相关的查询。 我们会尽快回复您。

@dropin,

您可以使用以下Aspose.Cells for .NET代码将Ms Excel文档中的这些形状转换为图像。
e.g
Sample code:

//Load source Excel file
            Workbook wb = new Workbook("e:\\test2\\smartart+d-+¦f.xlsx");

            //Access first worksheet
            Worksheet ws = wb.Worksheets[0];

            foreach (Shape smShape in ws.Shapes)
            {
                //Check if this shape is smart art shape
                if (smShape.IsSmartArt == true)
                {
                    //Convert the SmartArt shape to image.
                    smShape.ToImage("e:\\test2\\smartartgrouped1.png", new ImageOrPrintOptions() { ImageType = ImageType.Png });
                    //Convert it to group shape and render all the indvidual shapes images.
                    Shape[] shapes = smShape.GetResultOfSmartArt().GetGroupedShapes();
                    int i = 0;
                    foreach (Shape s in shapes)
                    {
                        s.ToImage("e:\\test2\\out1" + i.ToString() + ".png", new ImageOrPrintOptions() { ImageType = ImageType.Png });
                        i++;
                    }
                }//end if
            }

有使用java版的代码示例吗?谢谢了

有使用Java的代码示例吗?谢谢了。

@dropin,

请使用Aspose.Cells for Java查看等效的Java代码:
e.g
Sample code:

	    //Load source Excel file
        Workbook wb = new Workbook("e:\\test2\\smartart+d-+¦f.xlsx");

        //Access first worksheet
        Worksheet ws = wb.getWorksheets().get(0);

        ImageOrPrintOptions options = new ImageOrPrintOptions();
        options.setImageType(ImageType.PNG);
        
        for (int i=0; i<ws.getShapes().getCount(); i++) 
        { 
        	Shape smShape = ws.getShapes().get(i);
        
            //Check if this shape is smart art shape
            if (smShape.isSmartArt())
            {
                //Convert the SmartArt shape to image.
                smShape.toImage("e:\\test2\\smartartgrouped1.png", options);
                //Convert it to group shape and render all the indvidual shapes images.
                Shape[] shapes = smShape.getResultOfSmartArt().getGroupedShapes();
                for (int j=0; j<shapes.length; j++) 
                { 
                	Shape s = shapes[j];
                	s.toImage("e:\\test2\\out1" + j + ".png", options);
                    j++;
                }
            }//end if
        }

@dropin,

请参阅以下Aspose.Words for Java代码:

Document doc = new Document("D:\\SmartArt\\in.docx");

ArrayList list = new ArrayList();
for (Shape shape : (Iterable<Shape>) doc.getChildNodes(NodeType.SHAPE, true)) {
    if (shape.hasChart() || !shape.hasImage()) {
        ShapeRenderer renderer = shape.getShapeRenderer();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        renderer.save(baos, new ImageSaveOptions(SaveFormat.PNG));

        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.moveTo(shape);
        builder.insertImage(baos.toByteArray());
        list.add(shape);
    }
}


for (Shape shape : (Iterable<Shape>) list)
    shape.remove();

doc.save("D:\\SmartArt\\awjava-18.8.docx");