WorkBook Save Fails

I use Workbook book = new Workbook(path); to open an existing Excel file.
I use book.Save(outpath); to save with the extension changed from xslx to pdf.

This results in a PDF file always on my dev machine, and fine in production until the Excel file contains images.

Then in DEV it works, and in PROD it throws an exception : “Shape to Image Error”.
I suspect it may be having access issues to some folder where it wants to write temporary files.

Can you tell me where it writes temp files and if I can change that ?

Thanks,

Dave

@dwgrooms,

Thanks for providing us some details.

I guess you might not be using the same Aspose.Cells for .NET version on both environments (DEV and PROD). Please make sure that you are using same version (as per DEV) on PROD. On a safer side, kindly print out the version by adding a line to your code segment:
e.g
Sample code:

.......
Console.WriteLine(CellsHelper.GetVersion());
.........

Apparently it does not write any template file when rendering to PDF. Anyways, you should also make sure that you are running your application in full trust permissions set, see the document for your reference:

The DLLS are both 17.3.0.0
Its not running in an ASP .NET server its running as an Azure Web Job.
There’s plenty of disk available and the file isn’t that large,
Is there some way to diagnose this ?

@dwgrooms,

Could you provide us the source file, so we could try to find the error on our end?

I have a test project and file that demonstrates the issue.
To get it to fail you’ll need to set up an Azure Web Job to host it.
That requires an Azure App Service and a storage account.
You;ll need to edit the app.config file to point to your storage account, get a publish profile for your App Service and then publish the project as a Web Job using your publish profile.

At that point you can just run the WebJob from the Azure console and see the job succeeded since the code is catching the exception, but it will display the 'Shape to Image; exception in the Web Job Log files,
Project File Link :
https://1drv.ms/u/s!AnyvjDo8SqXza-KxVd7R_C5MF54

@dwgrooms,

Thanks for providing us sample project with template file and details.

I have logged an investigation ticket with an id “CELLSNET-45704” for your issue. The issue is logged as following:

  • CELLSNET-45704 - Workbook.Save() fails with an exception “Shape to Image Error” when using Aspose.Cells as an Azure web job

Please spare us some time. We will look into your issue soon.

Once we have an update on it, we will let you know here.

@dwgrooms,

We need to know the complete information/details about the exception to investigate your issue thoroughly. Please also give us the log file.

Here’s the contents of the log :
Your site won’t let me upload a .txt file

[10/17/2017 12:21:33 > 02108d: SYS INFO] Status changed to Initializing
[10/17/2017 12:21:36 > 02108d: SYS INFO] Run script ‘CellsTest.exe’ with script host - ‘WindowsScriptHost’
[10/17/2017 12:21:36 > 02108d: SYS INFO] Status changed to Running
[10/17/2017 12:21:41 > 02108d: INFO] Exception: Shape to image Error!
[10/17/2017 12:21:41 > 02108d: INFO] Exception: at ? .(?? , Boolean )
[10/17/2017 12:21:41 > 02108d: INFO] at ? .(? , Boolean )
[10/17/2017 12:21:41 > 02108d: INFO] at ? .(? )
[10/17/2017 12:21:41 > 02108d: INFO] at ? .()
[10/17/2017 12:21:41 > 02108d: INFO] at ? .()
[10/17/2017 12:21:41 > 02108d: INFO] at ? .??? ?(?? )
[10/17/2017 12:21:41 > 02108d: INFO] at .??? ?(?? )
[10/17/2017 12:21:41 > 02108d: INFO] at ?? .(PdfSaveOptions )
[10/17/2017 12:21:41 > 02108d: INFO] at ?? .(Stream , PdfSaveOptions )
[10/17/2017 12:21:41 > 02108d: INFO] at ?? .(Stream , SaveOptions )
[10/17/2017 12:21:41 > 02108d: INFO] at ?.(Object )
[10/17/2017 12:21:41 > 02108d: INFO] at ?.(MethodBase , Boolean )
[10/17/2017 12:21:41 > 02108d: INFO] at ?.?( )
[10/17/2017 12:21:41 > 02108d: INFO] at ?. ()
[10/17/2017 12:21:41 > 02108d: INFO] at ?.()
[10/17/2017 12:21:41 > 02108d: INFO] at ?.(Object )
[10/17/2017 12:21:41 > 02108d: INFO] at ?. ( )
[10/17/2017 12:21:41 > 02108d: INFO] at ?. ()
[10/17/2017 12:21:41 > 02108d: INFO] at ?.()
[10/17/2017 12:21:41 > 02108d: INFO] at ?.(Object )
[10/17/2017 12:21:41 > 02108d: INFO] at ?. ()
[10/17/2017 12:21:41 > 02108d: INFO] at ?. ( )
[10/17/2017 12:21:41 > 02108d: INFO] at ?. ()
[10/17/2017 12:21:41 > 02108d: INFO] at ?.()
[10/17/2017 12:21:41 > 02108d: INFO] at ?.(Object )
[10/17/2017 12:21:41 > 02108d: INFO] at ?. ()
[10/17/2017 12:21:41 > 02108d: INFO] at ?.(Object , UInt32 )
[10/17/2017 12:21:41 > 02108d: INFO] at ?.()
[10/17/2017 12:21:41 > 02108d: INFO] at ?.()
[10/17/2017 12:21:41 > 02108d: INFO] at ?.()
[10/17/2017 12:21:41 > 02108d: INFO] at Aspose.Cells.Workbook.Save(String fileName, SaveOptions saveOptions)
[10/17/2017 12:21:41 > 02108d: INFO] at Aspose.Cells.Workbook.Save(String fileName)
[10/17/2017 12:21:41 > 02108d: INFO] at CellsTest.Program.Main(String[] args)
[10/17/2017 12:21:41 > 02108d: SYS INFO] Status changed to Success

Dave

@dwgrooms,

Well, you may zip the text file or other file format and then upload in the forums. Anyways, thanks for the log file contents. This will help us to evaluate your issue precisely.

@dwgrooms,

Please try executing the following code using our latest version/fix: Aspose.Cells for .NET v17.10:

then give us info/ details if there is an error.
e.g
Sample code:

string path = Assembly.GetExecutingAssembly().Location;
int tail = path.LastIndexOf(“\”);
path = path.Substring(0, tail);
string srcpath = path + “\data\AsposeTest.xlsx”;
Workbook book = new Workbook(srcpath);
Aspose.Cells.Rendering.ImageOrPrintOptions op = new Aspose.Cells.Rendering.ImageOrPrintOptions();

Worksheet sheet = book.Worksheets[0];
for (int j = 0; j < sheet.Shapes.Count; j++)
{
Aspose.Cells.Drawing.Shape s = sheet.Shapes[j];
string img = sheet.Name + “Shape[" + j + “].” + s.Name.Replace(“/”, "”) + “.png”;
img = img.Replace(“:”, “”);
s.ToImage(path + “\data\” + img, op);
}

I made the suggested changes and downloaded the new DLL.
Results:

[10/18/2017 13:59:34 > 02108d: SYS INFO] Status changed to Initializing
[10/18/2017 13:59:34 > 02108d: SYS INFO] Job directory change detected: Job file ‘CellsTest.exe’ timestamp differs between source and working directories.
[10/18/2017 13:59:35 > 02108d: SYS INFO] Run script ‘CellsTest.exe’ with script host - ‘WindowsScriptHost’
[10/18/2017 13:59:35 > 02108d: SYS INFO] Status changed to Running
[10/18/2017 13:59:38 > 02108d: INFO] Exception: Shape to image Error!
[10/18/2017 13:59:38 > 02108d: INFO] Exception: at Aspose.Cells.Workbook.Save(String fileName, SaveOptions saveOptions)
[10/18/2017 13:59:38 > 02108d: INFO] at Aspose.Cells.Workbook.Save(String fileName)
[10/18/2017 13:59:38 > 02108d: INFO] at CellsTest.Program.Main(String[] args)
[10/18/2017 13:59:38 > 02108d: SYS INFO] Status changed to Success

Interesting enough, the .png produced by the sample code was 4k when I ran in debug mode, only 151 bytes when I ran as web job.
Maybe that’s a clue.

Dave

@dwgrooms,

Thanks for sharing the results.

I have logged the results and findings against your issue “CELLSNET-45704” into our database. Our concerned developer from product team will evaluate your issue further soon.

@dwgrooms,

Please try executing the following lines of code on Azure and check that the file ‘Result.emf’ is correct:
e.g
Sample code:

    RectangleF dstRectF = new RectangleF(0, 0, 673f, 360f);
    Bitmap dummyBitmap = new Bitmap(1, 1);
    Graphics dummyGfx = Graphics.FromImage(dummyBitmap);
    hdc = dummyGfx.GetHdc();

    MemoryStream msOut = new MemoryStream();
    Metafile metafile = new Metafile(msOut, hdc, dstRectF, MetafileFrameUnit.Pixel, EmfType.EmfPlusDual);
    Graphics g = Graphics.FromImage(metafile);

    byte[] Data = SaveImage(path +"\\image1.emf");
    Stream Ms = new MemoryStream(Data);
    Image Ima = Image.FromStream(Ms,true);

    g.DrawImage(Ima, dstRectF);
    g.Dispose();

    msOut.Seek(0, SeekOrigin.Begin);
    Image img = Image.FromStream(msOut);
    img.Save(path + "\\Result.emf", ImageFormat.Emf); //or img.Save(path + "\\Result.png", ImageFormat.Png);

Note: ‘image1.emf’ is the picture in Excel.
If no error is occurred. Please provide two files that you generated in debug run and network job(4k\151bytes).

Can you provide me with a working example ?

The fragment is incomplete, I don’t have a method called SaveImage, so the fragment doesn’t even compile.

Dave

@dwgrooms,

We will check if we could provide you the exact lines of code written in “SaveImage” custom method, so you may compile the code segment seamlessly. But, the underlying method only gets the image file into byte array, so you may easily replace the following line with your own code to read the contents of the image file into array of bytes by yourself:

byte[] Data = SaveImage(path +"\\image1.emf");

In Azure
[10/23/2017 15:03:47 > 1c59f5: SYS INFO] Status changed to Initializing
[10/23/2017 15:03:47 > 1c59f5: SYS INFO] Job directory change detected: Job file ‘CellsTest.exe’ timestamp differs between source and working directories.
[10/23/2017 15:03:48 > 1c59f5: SYS INFO] Run script ‘CellsTest.exe’ with script host - ‘WindowsScriptHost’
[10/23/2017 15:03:48 > 1c59f5: SYS INFO] Status changed to Running
[10/23/2017 15:03:48 > 1c59f5: INFO] basepath :D:\local\Temp\jobs\triggered\CellsTest\4idojlqp.gir
[10/23/2017 15:03:51 > 1c59f5: INFO] OutPath :D:\local\Temp\jobs\triggered\CellsTest\4idojlqp.gir\data\Sheet1Shape[0].Object 1.png
[10/23/2017 15:03:51 > 1c59f5: INFO] Exception: A generic error occurred in GDI+.
[10/23/2017 15:03:51 > 1c59f5: INFO] Exception: at System.Drawing.Imaging.Metafile…ctor(Stream stream, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit, EmfType type, String description)
[10/23/2017 15:03:51 > 1c59f5: INFO] at System.Drawing.Imaging.Metafile…ctor(Stream stream, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit, EmfType type)
[10/23/2017 15:03:51 > 1c59f5: INFO] at CellsTest.Program.Main(String[] args)
[10/23/2017 15:03:51 > 1c59f5: SYS INFO] Status changed to Success

After a bit of research, it looks like the GDI+ calls for MetaFile usage are not supported in an Azure WebJob.

Is there any way to accomplish the image transfer without these APIs ?

Otherwise I may have to rethink my hosting of this service

Thanks,
Dave

@dwgrooms,

That might be the reason it fails for image/shape rendering. I have logged your log contents and findings against your issue “CELLSNET-45704” into our database. Our concerned developer from product team will evaluate it further and we will get back to you soon.

@dwgrooms,

Well, Metafile(e.g. emf, wmf) images are widely used in Microsoft Excel. A solution is that we can add an option to let you set to draw metafile to bitmap, but this may cause the output image a little blurred in the output PDF.

Could you try to check whether the following code works on your Azure web job:
e.g
Sample code:

using (Image srcImage = Image.FromFile("image1.emf"))
            {
                using (Bitmap destImage = new Bitmap(srcImage.Width, srcImage.Height))
                {
                    destImage.SetResolution(srcImage.HorizontalResolution, srcImage.VerticalResolution);
                    using (Graphics g = Graphics.FromImage(destImage))
                    {
                        g.DrawImage(srcImage, new RectangleF(0, 0, srcImage.Width, srcImage.Height));
                    }

                    using (MemoryStream ms = new MemoryStream())
                    {
                        destImage.Save(ms, ImageFormat.Png);

                        //test save to file
                        using (FileStream fs = new FileStream("imageOut.png", FileMode.Create))
                        {
                            byte[] imageData = ms.ToArray();
                            fs.Write(imageData, 0, imageData.Length);
                        }
                        
                    }
                }
            }

if the code works and you accept the solution, we will add an option for you.

Please note, “image1.emf” is extracted from your Excel file.
image1.zip (21.1 KB)