Inside visio file excel content forground color not shown

Hi,
We have visio file and that file have ‘.xlsx’ file. So we collect the ‘.xlsx’ shapes then set Forgroundcolor. FInally save the Visio file, but Still the foreground color not updated .

private void TestVisioExcelBold(string strInputFile, string strOutputPath)
    {
        FileStream _licenseStream = new FileStream("Aspose.Total.lic", FileMode.Open);
        License licDiagram = new License();
        licDiagram.SetLicense(_licenseStream);
       
        _licenseStream.Position = 0;
        Aspose.Cells.License licCell = new Aspose.Cells.License();
        licCell.SetLicense(_licenseStream);
        {
	        Diagram diagram = new Diagram(strInputFile, LoadFileFormat.VSDX);

	        foreach(Page diagramPage in diagram.Pages)
	        {
		        foreach(Shape OLE_Shape in diagramPage.Shapes)
		        {
			        if(OLE_Shape.Type == TypeValue.Foreign)
			        {
				        if(OLE_Shape.ForeignData.ForeignType != ForeignType.Object) continue;
				        if(OLE_Shape.ForeignData.ObjectData == null) continue;
				        Stream Ole_stream = new MemoryStream(OLE_Shape.ForeignData.Value);
				        // Get format of the OLE file object
				        FileFormatInfo info = FileFormatUtil.DetectFileFormat(Ole_stream);
				        if(info.LoadFormat == LoadFormat.Xlsx || info.LoadFormat == LoadFormat.Excel97To2003)
				        {
					        // Modify an OLE object
					        Workbook workbook = new Workbook(new MemoryStream(OLE_Shape.ForeignData.Value));


					        foreach(Cell cell in workbook.Worksheets[0].Cells)
					        {
						        var style = cell.GetStyle();
						        style.ForegroundColor = System.Drawing.Color.LightGreen;
						        style.Pattern = BackgroundType.Solid;
						        cell.SetStyle(style);
					        }
                            ImageOrPrintOptions options = new ImageOrPrintOptions
					                                      {
						                                      OnePagePerSheet = true,
						                                      ImageType = ImageType.Png,
						                                      OnlyArea = true,
						                                      IsCellAutoFit = true,
						                                      Transparent = true,

						                                      HorizontalResolution = 200,
						                                      VerticalResolution = 200
					                                      };
					        SheetRender sr = new SheetRender(workbook.Worksheets[0], options);
					        sr.ToImage(0, "outputConvertWorksheettoImageFile.emf");

					        using(FileStream fs = new FileStream("outputConvertWorksheettoImageFile.emf", FileMode.OpenOrCreate, FileAccess.ReadWrite))
					        {
						        byte[] buff = new byte[fs.Length];
						        fs.Read(buff, 0, (int) fs.Length);

						        MemoryStream streamEmf;
						        using(streamEmf = new MemoryStream(buff))
						        {
							        // Replace picture shape
							        OLE_Shape.ForeignData.ImageData = streamEmf.ToArray();
						        }
					        }

					        MemoryStream outStream = new MemoryStream();
					        workbook.Save(outStream, SaveFormat.Xlsx);
					        OLE_Shape.ForeignData.ObjectData = outStream.ToArray();

					        workbook.Save("C:\\Users\\VenkadachalapathyKal\\OneDrive - M&S\\Desktop\\Style\\" + "Test" + ".xlsx", SaveFormat.Xlsx);
				        }
			        }
		        }
	        }

	        ImageSaveOptions opt = new ImageSaveOptions(SaveFileFormat.PNG)
	                               {
		                               SmoothingMode = SmoothingMode.HighQuality,
		                               CompositingQuality = CompositingQuality.HighQuality,
		                               InterpolationMode = InterpolationMode.HighQualityBicubic,
		                               PixelOffsetMode = PixelOffsetMode.HighQuality,
		                               EnlargePage = false,
		                               Resolution = 96,
		                               PageIndex = 0
	                               };
	        // Save Visio diagram
	        diagram.Save(strOutputPath + $"{Path.GetFileNameWithoutExtension(strInputFile)}_{DateTime.Now.ToString("yyyyMMddHHmmss")}_out.vsdx", SaveFileFormat.VSDX);
	        diagram.Save(strOutputPath + $"{Path.GetFileNameWithoutExtension(strInputFile)}_{DateTime.Now.ToString("yyyyMMddHHmmss")}_out.png", opt);
        }
    }

TestVisio_20230330120652_out.zip (77.3 KB)
TestVisio_20230330120653_out.png (3.7 KB)

Could you please help us to resolve this.

@msindia,

I think your query/issue is related to Aspose.Diagram, so I am moving your thread to respective forum where our fellow colleagues will assist you soon.

@amjad.sahi,

Thank you.

@msindia
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): DIAGRAMNET-53121

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@msindia
Please replace the image data of the shape, as shown in the following code,then it’s color will be green:

               ImageOrPrintOptions options = new ImageOrPrintOptions
                {
                    OnePagePerSheet = true,
                    ImageType = ImageType.Emf,
                    OnlyArea = true,
                    IsCellAutoFit = true,
                    Transparent = true,
                    HorizontalResolution = 200,
                    VerticalResolution = 200
                };
                SheetRender sr = new SheetRender(worksheet, options);
                sr.ToImage(0, "outputConvertWorksheettoImageFile.emf");

                using (FileStream fs = new FileStream("outputConvertWorksheettoImageFile.emf", FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    byte[] buff = new byte[fs.Length];
                    fs.Read(buff, 0, (int)fs.Length);

                    MemoryStream streamEmf;
                    using (streamEmf = new MemoryStream(buff))
                    {
                        // Replace picture shape
                        shape.ForeignData.ImageData = streamEmf.ToArray();

                    }
                }

Thanks.

@philip.zhou,

Thanks for your Answer. I have tried with your code. It works fine. But the final output is not valid. I have attached the output and codes.

 private void TestVisioExcelBold(string strInputFile, string strOutputPath)
    {
        FileStream _licenseStream = new FileStream("Aspose.Total.lic", FileMode.Open);
        License licDiagram = new License();
        licDiagram.SetLicense(_licenseStream);
       
        _licenseStream.Position = 0;
        Aspose.Cells.License licCell = new Aspose.Cells.License();
        licCell.SetLicense(_licenseStream);
        {
	        Diagram diagram = new Diagram(strInputFile, LoadFileFormat.VSDX);

	        foreach(Page diagramPage in diagram.Pages)
	        {
		        foreach(Shape OLE_Shape in diagramPage.Shapes)
		        {
			        if(OLE_Shape.Type == TypeValue.Foreign)
			        {
				        if(OLE_Shape.ForeignData.ForeignType != ForeignType.Object) continue;
				        if(OLE_Shape.ForeignData.ObjectData == null) continue;
				        Stream Ole_stream = new MemoryStream(OLE_Shape.ForeignData.Value);
				        // Get format of the OLE file object
				        FileFormatInfo info = FileFormatUtil.DetectFileFormat(Ole_stream);
				        if(info.LoadFormat == LoadFormat.Xlsx || info.LoadFormat == LoadFormat.Excel97To2003)
				        {
					        // Modify an OLE object
					        Workbook workbook = new Workbook(new MemoryStream(OLE_Shape.ForeignData.Value));


					        foreach(Cell cell in workbook.Worksheets[0].Cells)
					        {
						        var style = cell.GetStyle();
						        style.ForegroundColor = System.Drawing.Color.LightGreen;
						        style.Pattern = BackgroundType.Solid;
						        cell.SetStyle(style);
					        }
                            ImageOrPrintOptions options = new ImageOrPrintOptions
					                                      {
						                                      OnePagePerSheet = true,
						                                      ImageType = ImageType.Png,
						                                      OnlyArea = true,
						                                      IsCellAutoFit = true,
						                                      Transparent = true,

						                                      HorizontalResolution = 200,
						                                      VerticalResolution = 200
					                                      };
					        SheetRender sr = new SheetRender(workbook.Worksheets[0], options);
					        sr.ToImage(0, "outputConvertWorksheettoImageFile.emf");

					        using(FileStream fs = new FileStream("outputConvertWorksheettoImageFile.emf", FileMode.OpenOrCreate, FileAccess.ReadWrite))
					        {
						        byte[] buff = new byte[fs.Length];
						        fs.Read(buff, 0, (int) fs.Length);

						        MemoryStream streamEmf;
						        using(streamEmf = new MemoryStream(buff))
						        {
							        // Replace picture shape
							        OLE_Shape.ForeignData.ImageData = streamEmf.ToArray();
						        }
					        }

					        MemoryStream outStream = new MemoryStream();
					        workbook.Save(outStream, SaveFormat.Xlsx);
					        OLE_Shape.ForeignData.ObjectData = outStream.ToArray();

					        workbook.Save("C:\\Users\\VenkadachalapathyKal\\OneDrive - M&S\\Desktop\\Style\\" + "Test" + ".xlsx", SaveFormat.Xlsx);
				        }
			        }
		        }
	        }

	        ImageSaveOptions opt = new ImageSaveOptions(SaveFileFormat.PNG)
	                               {
		                               SmoothingMode = SmoothingMode.HighQuality,
		                               CompositingQuality = CompositingQuality.HighQuality,
		                               InterpolationMode = InterpolationMode.HighQualityBicubic,
		                               PixelOffsetMode = PixelOffsetMode.HighQuality,
		                               EnlargePage = false,
		                               Resolution = 96,
		                               PageIndex = 0
	                               };
	        // Save Visio diagram
	        diagram.Save(strOutputPath + $"{Path.GetFileNameWithoutExtension(strInputFile)}_{DateTime.Now.ToString("yyyyMMddHHmmss")}_out.vsdx", SaveFileFormat.VSDX);
	        diagram.Save(strOutputPath + $"{Path.GetFileNameWithoutExtension(strInputFile)}_{DateTime.Now.ToString("yyyyMMddHHmmss")}_out.png", opt);
        }
    }

Input File VSDX : TestVisio.zip (54.0 KB)
OutPut File VSDX: TestVisio_20230403102251_out.zip (50.6 KB)
Output PNG: TestVisio_20230403102251_out.png (17.2 KB)
OutPut Excel: TestExcel.zip (7.5 KB)

See in the PNG file the output is not clear.

Please resolve this issue.
Thanks for your support.

@msindia
Please change ImageType = ImageType.Png to ImageType = ImageType.Emf
Then the object in the output can be clicked.
The issue of output image is not clear, please change the size of the emf image to be the same as the original size.
Thanks.

@philip.zhou,
Thanks for the update. I have changed the ImageType = ImageType.Emf.

Please change the size of the emf image to be the same as the original size.

How to do the above point. Please help me.

@msindia
We are sorry that we are not very familiar with the settings of cells, please check the documentation of cells, or you can try aspose.image .
Thanks.

@philip.zhou, @asad.ali ,

Any other way to resolve this issue?

Thanks

@msindia
We will look further into it.
Thanks.