Enhancing Office 2003 Research Services with Graphics and Fancy Fonts

 

Angela Wong
Microsoft Corporation

November 2004

Applies to:
    Microsoft Office Word 2003
    Microsoft Office Excel 2003
    Microsoft Office Outlook 2003
    Microsoft Office OneNote 2003
    Microsoft Office PowerPoint 2003
    Microsoft Office Publisher 2003
    Microsoft Office Visio Standard 2003
    Microsoft Internet Explorer

Summary: One way for developers to enhance a Microsoft Office 2003 research service is to display results with appropriate graphics and fancy fonts in the Research task pane. This article shows how an e-commerce research service effectively promotes the branding of its products by using these techniques. (11 printed pages)

Contents

Introduction
"Before" Scenario
"After" Scenario
Code Example
Conclusion
Additional Resources

Introduction

The Microsoft Office Research Service Software Development Kit (SDK) provides a platform for third-party developers to build rich, integrated, custom services for corporate, subscription, or general consumption. You can search these services by using the Microsoft Office 2003 Research feature.

A research service is an XML Web service. Each research service must define two Web methods: Registration and Query. When calling these Web methods, the client computer and the research service exchange XML packets as shown in Figure 1.

Figure 1. A client computer and a research service exchange XML request and response packets.

As a research service developer, you can enhance the user interface of your service by using graphics and special fonts. There is no specific Web method that lets you specify graphics or fonts in the Research task pane. But you can display graphics (and custom fonts as graphics) by embedding the names of these image files in the query response.

Note   While graphics and special fonts can enhance the display of research results, the Research task pane supports only text (not images) as hyperlinks.

IBuyXbox.com

This article describes a fictitious e-commerce Web site, IBuyXbox.com, which specializes in Microsoft Xbox games. The site provides a research service for customers to search and buy Xbox games online.

Consider two scenarios of a product search on the IBuyXbox.com research service. In the first scenario, the research service displays results in a standard format without using any special fonts or graphics. The second scenario shows research results in a customized font, including an image of the product. This article also includes a code example that generates the enhanced results.

"Before" Scenario

A customer types halo in the Research task pane. The IBuyXbox.com research service returns products in the catalog that match the keyword. See Figure 2.

Figure 2. Research results are displayed in a standard manner.

The IBuyXbox.com research service displays results in a standard manner without using any special fonts or graphics. Products are not associated with their marketed images; the customer has to carefully read the research results to look for the appropriate product.

"After" Scenario

To market its products more effectively, IBuyXbox.com decides to store in its catalog database an image file for the product package, and a font for the title that resembles that of the product. When displaying results, the research service first displays product titles in the corresponding font, as in Figure 3.

Figure 3. Research results are displayed in a font matching the product.

When the customer then clicks one of the products—for example, **Halo—**the customer sees the title and a picture of the package, and both match the marketed product image. See Figure 4.

Figure 4. Detailed search results include a title and a picture of the package, and both match the product image.

Now, customers can more easily identify desired products in the search results, making it easier to decide which version to purchase. The enhancement successfully reinforces the product image and helps to promote sales.

Code Example

The code example in this section helps to generate the results shown in Figure 4. For more information about developing third-party research services, see the Microsoft Office Research Service SDK.

The following steps illustrate the code example.

  1. The IBuyXboxService namespace contains the QueryService class and the StringImage class:
    • The QueryService class defines the Query Web method that returns a query response to the client computer.

    • The StringImage class defines a private StringImageFile method that converts a title to a specified font, puts it in a file, and returns the full path name to the file.

      Note   Generating the title in a specific font in real time may affect the overall response time of the research service. The alternative is to store the title in the desired font in the catalog database and write it to a file only in real time.

  2. The Query method calls the private method GetResults, which builds the query response in well-formed XML. The Query method then returns the packet as a query response.
  3. The GetResults method first calls the SearchCatalog method, which searches the catalog database given the customer's query, and returns the search results as an XmlDocument object.
  4. GetResults then uses an XmlTextWriter object to extract and process the following into well-formed XML:
    • It extracts the title and the font for the title, and calls the StringImageFile method to obtain the full path name of an image file that contains the title in the specified font.
    • It extracts the URL to an image for the package.
  5. The GetResults method writes the XmlTextWriter object into a string. GetResults then returns the string to the Query method.
using System;
using System.ComponentModel;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Configuration;
using System.Text;
using System.IO;
using System.Globalization;
using System.Drawing;
using System.Drawing.Imaging;

namespace IBuyXboxService
{
   [WebService(Namespace="urn:Microsoft.Search")]
   public class QueryService: System.Web.Services.WebService
   {
      .
      .
      .
      //Class level variables
      string httpPath = ConfigurationSettings.AppSettings["ServerPath"]
         + HttpContext.Current.Request.ApplicationPath + "/";
      .
      .
      .
      public string Query(string queryXml)
      {
         XmlDocument responseWrapper = new XmlDocument();
         string queryString = queryXml;

         // Code to validate and parse input query.
         .
         .
         .
         // Then assign parsed query to queryString.

         responseWrapper.InnerXml = GetResults (queryString);
         return responseWrapper.InnerXml.ToString();
      }

      private string GetResults(string queryString)
      {
         // string object that holds the query response  
         StringWriter queryResponse = new StringWriter();

         // object to write well-formed XML into a string
         XmlTextWriter writer = new XmlTextWriter(queryResponse);

         // search catalog to get results
         XmlDocument xmlResult = new XmlDocument();
         xmlResult = SearchCatalog (queryString);

         // Look for item and get details for item 
         .
         .
         .
         XmlNode item = xmlResult.DocumentElement.SelectSingleNode(
            "Details");
         
         // Start building results for Research task pane, 
         // including title and package
         .
         .
         .

         // Extract the title and title font of the item
         string sTitle = item.SelectSingleNode("ItemTitle").InnerText;
         string sFont = item.SelectSingleNode("ItemTitleFont").InnerText;

         // Build item title in corresponding font
         StringImage Graphic = new StringImage ();
         string sTitleGraphic = Graphic.StringImageFile(sTitle,
            queryString, sFont);
         if (sTitleGraphic != "")
         {
            writer.WriteStartElement("Image");
            writer.WriteAttributeString("source", httpPath + 
               sTitleGraphic);
            writer.WriteEndElement();
         }
         else // use default font
         {
            writer.WriteStartElement("Char");
            writer.WriteAttributeString("bold", "true");
            writer.WriteString(
               item.SelectSingleNode("ItemTitle").InnerText);
            writer.WriteEndElement(); 
         }
         
         // Item package
         writer.WriteStartElement("Image");
         writer.WriteAttributeString("source",
            item.SelectSingleNode("ItemPackage").InnerText);
         writer.WriteEndElement(); 

         // Finish building the rest of the results
         .
         .
         .
         writer.Flush();
         writer.Close();

         return queryResponse.ToString();
      }

      private XmlDocument SearchCatalog(string queryString)
      {
         XmlDocument xmlResult = new XmlDocument();
   
         // Search the e-commerce site catalog   
         xmlResult.Load ("<Specify URL to search the catalog with "+
            "the given query string>");
         
         return xmlResult;
      }
   }

   public class StringImage
   {

      // Given a title, a file identifier, and a font, returns the full 
      // path name for an image file that contains
      // the title in the specified font

      public string StringImageFile(string stringToImage, string filename, 
         string font)
      {
         Bitmap newBitmap = null;
         Graphics g = null;
         string imagePath = 
            ConfigurationSettings.AppSettings["ProductImages"] +"/";
         string stringToPrint;

         try 
         {
            Font fontText = new Font(font,10,FontStyle.Bold);
            
            //Wrap string
            int maxWidth = 30;
            if (stringToImage.Length > maxWidth)
            {
               string [] asSpaces = stringToImage.Split(
                  Convert.ToChar(" "));
               int currentWidth = 0;
               string tempString = "";

               for (int i = asSpaces.GetLowerBound(0); 
                  i <= asSpaces.GetUpperBound(0); i++ )
               {
                  if (currentWidth + asSpaces[i].Length > maxWidth)
                  {
                     tempString  = tempString + Environment.NewLine + 
                      asSpaces[i];
                     currentWidth = asSpaces[i].Length;
                  }
                  else
                  {
                     tempString = tempString + " " + asSpaces[i];
                     currentWidth = currentWidth  + asSpaces[i].Length;
                  }
               }
               stringToPrint = tempString.Trim();
            }
            else
            {
               stringToPrint = stringToImage;
            }
      
            // Calculate size of the string
            newBitmap = new Bitmap(1,1,PixelFormat.Format32bppArgb);
            g = Graphics.FromImage(newBitmap);
            SizeF stringSize = g.MeasureString(stringToPrint, fontText);
            int nWidth = (int)stringSize.Width;
            int nHeight = (int)stringSize.Height;
            g.Dispose();
            newBitmap.Dispose();
      
            newBitmap = new 
               Bitmap(nWidth,nHeight,PixelFormat.Format32bppArgb);
            g = Graphics.FromImage(newBitmap);
            g.FillRectangle(new SolidBrush(Color.White), 
               new Rectangle(0,0,nWidth,nHeight));

            g.DrawString(stringToPrint, fontText, 
               new SolidBrush(Color.DarkGreen), 0, 0);
            newBitmap.Save(AppDomain.CurrentDomain.BaseDirectory  + 
               imagePath + fileName + ".png", ImageFormat.Png);
         } 
         catch 
         {
            return "";
         }
         finally 
         {
            if (null != g) g.Dispose();
            if (null != newBitmap) newBitmap.Dispose();
         }
         return imagePath + fileName + ".png";
      }
   }
}

Conclusion

The Microsoft Office Research Service SDK enables developers to build rich, integrated research services. Using the technique described in this article, developers can use special graphics and fonts to customize and enhance their research services.

Additional Resources

Use the following links for additional information and resources for working with the Research service:

© Microsoft Corporation. All rights reserved.