Nov 17

This post is a follow up to some of my previous posts about the Send to Word/Excel features of Dynamics NAV. As you know, it uses a stylesheet (xslt file), that is being read into memory and the codeunit 403 inserts values into the document.

This is definitely one of the cool demo features, when presenting NAV. At this point you dont realize that the Cronus logo is hardcoded into the stylesheets, but think it is just using the logo in the Company Information setup.

My previous post gave you some steps on how to change this. But this can be pretty cumbersome to say it at least. So when having to do it for the I dont know what time again, i decided to create a function that could automatically insert the picture from the Company Information.

In order to do this, there are a few things you need to take into consideration:

  • The picture must be encoded to Base64
  • The size/resolution of the picture in Company Information might be too big to show at regular size in Word
  • We need to calculate the width/height of the picture to maintain the aspect ratio when resizing it

Luckily most of these issues have already been solved, and by doing a quick search on either MIBUSO or Dynamicsuser, you will find the solutions i have used to accomplish this.

Lets start by creating the function for inserting the picture into the stylesheet, and look at its supporting functions later on. In this example i have tried to work with streams only, so we dont have to rely on any write access to filesystems etc. Here we go:

Create a new function in Codeunit 403 Application Launch Management, call it InsertCompanyLogo:

InsertCompanyLogo(StyleSheet : Record "Style Sheet")
Width := GetBitmapWidth(InStr);
Height := GetBitmapHeight(InStr);
IF Width > 300 THEN BEGIN
  Height := ROUND(Height / (Width / 300),1);
  Width := 300;

First we get the Company Information and do a CALCFIELDS to retrieve the Picture BLOB. Next we want to read the content into a InStream variable, which we will use to calculate the dimensions of the picture. As you can see we are using the CREATEINSTREAM twice. This is to reset the pointer, as it would otherwise continue reading from our last read operation. The documentation on streams says that all read operations are done sequantially, so that explains it.

You can see i adjust the size of the logo if it is too big, this might or might not work with your logo.

The 2 functions to get the dimensions are just for reading some specific bytes (see BMP definition here

GetBitmapWidth(inStr : InStream) Width : Integer
Width := value[19];
Width += value[20] * 256;
Width += value[21] * 256 * 256;
Width += value[22] * 256 * 256 * 256;

GetBitmapHeight(inStr : InStream) Height : Integer
Height := value[23];
Height += value[24] * 256;
Height += value[25] * 256 * 256;
Height += value[26] * 256 * 256 * 256;

So far so good. We have the Picture BLOB available, and we have figured out the dimensions of the picture. Next step, is to convert the picture data to Base64. I found a simple function for this on MIBUSO. Performance wise it is not very good, but it is simple and done in pure C/AL, so i choose it instead of the automation that is available in Commerce Gateway. You can find the original thread here: BASE64 Coding. The code continued in InsertCompanyLogo() is:


We need 2 variables pointing to our Company Information, one of them just a temporary to store our Base64 encoded value.

Next we need to open the actual stylesheet, and start reading it line by line. We are reading it line by line, since we are searching for specific tags in the file, and need to replace the Base64 encoded lines, which are stored with a certain size and linebreaks. There is a supporting function to read a textline from the Stylesheet called ReadTextLineFromStream, which will be shown later.

The tags we are looking for is the start and end of the w:binData tag.

StyleSheet.CALCFIELDS("Style Sheet");
StyleSheet."Style Sheet".CREATEINSTREAM(InStr);
StyleSheet2."Style Sheet".CREATEOUTSTREAM(OutStr);
 TextLine := ReadTextLineFromStream(InStr);
 IF (STRPOS(TextLine,'<w:binData') <> 0) THEN
   StartFound := TRUE;
 IF (STRPOS(TextLine,'<v:shape id') <> 0) THEN
   EndFound := TRUE;
 IF NOT (StartFound OR EndFound) THEN

We have some control variables to figure out where we are in the original stylesheet, and to control whether or not we should transfer the line to the “new” stylesheet.

When we found the end of our Base64 encoded data, we have to write our own Base64 encoded image with its tags in there, and also the tag that actually displays it. This tag also need the dimensions of the picture.

   OutStr.WRITETEXT('<w:binData w:name="wordml://03000001.png">');
   UNTIL Base64InStream.EOS;
     STRSUBSTNO('<v:shape id="_x0000_i1025" type="#_x0000_t75" style="width:%1px;height:%2px">',Width,Height));
   StartFound := FALSE;
   EndFound := FALSE;

Then just modify the new Stylesheet record, and we have saved our new stylesheet with the picture from Company Information.

Here is the 2 other supporting functions:

ReadTextLineFromStream(VAR inStr : InStream) TextLine : Text[1024]
c10 := 10;
c13 := 13;
Stop := FALSE;
 IF (Char = c10) AND (LastChar = c13) THEN
   Stop := TRUE;
 TextLine += FORMAT(Char);
 LastChar := Char;
UNTIL Stop OR inStr.EOS;

Base64EncodeStream(VAR inStr : InStream;VAR outStr : OutStream)
txt64Chars += 'abcdefghijklmnopqrstuvwxyz';
txt64Chars += '0123456789+/';
 nRead := inStr.READ(b);
 IF nRead < 3 THEN
   b[3] := 0;
 IF nRead < 2 THEN
   b[2] := 0;
 c[1] := b[1] DIV 4;
 c[2] := ((b[1] MOD 4) * 16) + (b[2] DIV 16);
 c[3] := ((b[2] MOD 16) * 4) + (b[3] DIV 64);
 c[4] := b[3] MOD 64;
 c[1] := txt64Chars + 1];
 c[2] := txt64Chars + 1];
 IF nRead > 1 THEN
   c[3] := txt64Chars + 1]
   c[3] := '=';
 IF nRead > 2 THEN
   c[4] := txt64Chars + 1]
   c[4] := '=';
 nWritten += 1;
 IF nWritten = 18 THEN BEGIN
   c[1] := 13;
   c[1] := 10;
   nWritten := 0;

To implement it i have just placed this function in the routine for InsertStyleSheet() (v5) and InsertFormStyleSheet()/InsertPageStyleSheet() (Version 2009):

InsertStyleSheet(ProgID : Text[38];Name : Text[250];FileName2 : Text[100];FormID : Integer)
FileName := APPLICATIONPATH + 'StyleSheets\' + FileName2;
 StyleSheet."Style Sheet ID" := CREATEGUID;
 StyleSheet."Object ID" := FormID;
 StyleSheet."Object Type" := StyleSheet."Object Type"::Form;
 StyleSheet."Program ID" := ProgID;
 StyleSheet.Name := Name;
 StyleSheet.Date := TODAY;
 StyleSheet."Style Sheet".IMPORT(FileName);

 // >> GOTCAL.SN
 // << GOTCAL.SN


Hope you enjoy this little customization, nothing rocket science, but a great time saver when setting up the standard stylesheets. Not to forget the introduction to Base64 encoding and reading bitmap information. Notice, if you have very large bitmaps, the Base64 encoding might take a while, just wait for it :). And again, this is a process that only needs to be run once to update the stylesheets.

For some great information about getting Stylesheets to work in 2009, you might want to visit this thread on MIBUSO: Does anyone know if this has been changed with the R2 release?

I will add the files to download section shortly, if anyone is interested.

8 Responses to “Replacing CRONUS logo with CompanyInformation Picture in Stylesheets”

  1. Nico says:

    That’s cool.

  2. Mirko says:

    I’m trying to follow the post, but…
    Maybe it’s stupid question, but I have to ask it. Which datatype are variables “value” and “b” (from inStr.READ(value);).

  3. Juro says:

    you wrote inyour posting:

    GetBitmapHeight(inStr : InStream) Height : Integer
    Height := value[23];
    Height += value[24] * 256;
    Height += value[25] * 256 * 256;
    Height += value[26] * 256 * 256 * 256;

    My questions:
    In both functions GetBitmapHeight and GetBitmapWidth:

    Is value an array variable? With 22 rsp. 26 dimensions?
    Or how else is it defined?

  4. Skriker says:

    Did you prepare downloads with the code/fobs?


  5. SIPA says:

    Thank you very much for this clever tutorial!
    I’ve extended my Excel Buffer to store picture etc, but the scaling needed to determine the pictures height and width dynamically.

    Without your code this wouldn’t be possible 🙂

Leave a Reply

preload preload preload