Printing a logo in an excel file, using D365FO

 

Amigos, 

Here is a small write-up that explains how to print a logo in an excel file, when you have created the Excel file through X++ code. The inherent problem with printing logo in an x++ generated files is: the logos stretch across several columns, and they are very hard to position to a particular location/cell, automatically. The following code, first gets the Logo image and then poses it at the start of the document, across first two cells.  

Here are the steps:


Before you begin, you need the following references:

using System.Drawing;

using OfficeOpenXml.Drawing;

a. Get the image container:

private container getCompanyImage()

    {

        CompanyInfo companyInfo     = CompanyInfo::find();

        return CompanyImage::findByRecord(companyInfo).Image;

    }

Here for example, you are trying to print the company logo.


b. Draw the logo and pose it across the first two columns of the file:

void drawImage(ExcelWorksheet   _workSheet)

        {

            Container image = this.getCompanyImage();

            if (image)

            {

                System.IO.MemoryStream MemoryStream = Binary::constructFromContainer(image).getMemoryStream();


                MemoryStream.Position = 0;


                System.Drawing.Image companyLogo = System.Drawing.Image::FromStream(MemoryStream);

                var excelImage = _workSheet.Drawings.AddPicture("image1",companyLogo);


            

            

                excelImage.EditAs = eEditAs::TwoCell;

                excelImage.SetPosition(0, 0, 0, 0);

            }

        

        }

Look at the code. After getting the image in a container variable, at first it draws a picture by invoking: addPicture method. And then, we are calling eEditAs method of OfficeOpenXml.Drawing class, that has the following enum values:

Absolute -- This will simply keep the image without doing anything.

OneCell -- This will keep it to the first cell only, but without cropping it.

TwoCell -- This is best choise, of letting the image know to rearrange itself, when the user rearranges the excel columns. 

And finally, we are posing the image to the first column by: 

excelImage.SetPosition(0, 0, 0, 0);

The image fists just in place, as follows:



So much for now, will continue more on Machine learning and Tensor flows with Python, using them in context of D365FO. 




Comments

Popular posts from this blog

X++ : mistakes which developers commit the most

Make your menu items visible on main menu, conditionally,, using this cool feature of D365FO

Speed up your execution performance by using SysGlobalCaches