DYMO Label Inside Out

April 30, 2010

The DymoAddin Object

Filed under: SDK Samples — CK Hsu @ 2:43 am
Tags: , , ,

In the last blog, I started out with a simple label printing application using the DLS SDK. If you are planning on using the SDK, you’ll need to become familiar with two objects that will do the bulk of label printing work in your application. The first one the DymoAddin object, the other is the DymoLabels object. We will examine on the DymoAddin object in this post.

Background

The DymoAddin object got its name from the time when DYMO needed to create addins for 3rd party applications. Instead of re-writing all the label printing code in a separate library, then using the library to implement the 3rd party addins (like the DLS MS Office Addins, DLS ACT! Addin, DLS QuickBooks Addin), we simply added a COM interface to the DLS application so we can leverage the label printing functionality from the DLS application. The first interface exposed was called the IDymoAddin interface, and the object that implemented the interface was called the DymoAddin object.

There are many methods exposed in the IDymoAddin interface, however, only a part of it is really intended for the SDK. The methods that don’t fall in this group are there because we (i.e. DYMO) needed it to implement some functionality for the addins we write for other applications. Call it lack of planning or oversight, these methods and properties that are really meant for internal DYMO use are lumped together in the same IDymoAddin interface that is used for SDK applications. For the most part, you should avoid using these methods and properties in your application. But as you sit down to understand the DLS SDK and design your own application, you’ll likely find that these methods and properties are too specialized and are not needed for your SDK application.

But what are these functions and methods that you shouldn’t use? Instead of listing them here, I will instead focus on the methods that are meant to be used by SDK applications.

Main Functionality

In the last blog, I outlined the three main concepts used in the DLS SDK. The first is the concept of a label file. The main functionality of the DymoAddin object is to implement the IDymoAddin interface, which provides operations you can perform on a label file, including opening, saving, and printing a label file. Because printing is part of the functionality, the IDymoAddin interface also provides methods to list and select DYMO printers for use in your SDK application. I will explore these methods and their usage (in C#) below.

File Operations

The Open(), Save(), and SaveAs() methods combined to give you the ability to open and save labels on your computer’s local file system. The same operations can be used on network storage if the storage location is addressable using UNC (i.e. Universal Naming Convention).

The following example shows how to open a label file and then save it with a different file name:

 1 private void FileOperatoinsExample()
 2 {
 3     DymoAddInClass _dymoAddin = new DymoAddInClass();
 4 
 5     // open a label file using absolute path, relative path
 6     // or UNC (for network accessible locations)
 7     if (_dymoAddin.Open(@"..\Labels\Sample.label"))
 8     {
 9         _dymoAddin.SaveAs(@"..\Labels\SampleCopy.label");
10     }
11 
12     if (_dymoAddin.Open(@"C:\Users\ckhsu\Labels\Sample.label"))
13     {
14         _dymoAddin.SaveAs(@"C:\Users\ckhsu\labels\SampleCopy.label");
15     }
16 
17     if (_dymoAddin.Open(@"\\File Server\Shared\Labels\Sample.label"))
18     {
19         _dymoAddin.SaveAs(@"\\File Server\Shared\Labels\SampleCopy.label");
20     }
21 }
22 

Web Applications

Sometimes the label file you need is located in the cloud and you need to access it using an URL. The OpenURL() method is created for this purpose.

Proxy Server and Proxy Bypass

The OpenURL() method is implemented using WinInet. This means that the user’s proxy settings in IE is used when calling the OpenURL(). But depending on your application, this might not be desirable. You can use the following methods and properties to control proxy settings used for the OpenURL() method. These settings are good for the lifetime of the DymoAddinObject, but are not persistent beyond that. So if you free and create another instance of the DymoAddinObject, you will need to setup the custom proxy settings again for the next OpenURL call.

  • ProxyBypass property – set to True to bypass all proxy servers. When this property is set to True, it overrides all other proxy settings.
  • SetupProxySettings() method – setup custom proxy settings.
  • ClearProxySettings() method – clear all proxy settings and revert back to using IE’s proxy settings.

The following example shows how to open a label file using an URL.

 1 private void URLFileOperatoinsExample()
 2 {
 3     DymoAddInClass _dymoAddin = new DymoAddInClass();
 4 
 5     // use custom proxy setup.
 6     // by default, the OpenURL() method uses the same proxy
 7     // settings as IE
 8     _dymoAddin.proxyBypass = false;
 9     _dymoAddin.SetupProxySettings("http", "dymo_proxy_server", 80, "*.internal.dymo.*", "", "");
10 
11     // open a label file using URL
12     if (_dymoAddin.OpenURL("http://www.mylabelprinter.com/labels/shipping.lwl"))
13     {
14         _dymoAddin.SaveAs(@"Labels\SampleCopy.label");
15     }
16 }
17 

Database Operations

If your application uses a database to store label files, you can use the OpenStream() and SaveStream() methods to open and save label files using memory buffers.

The following example shows how to open and save label files into memory buffers:

 1 private void OpenAndSaveStreamExample()
 2 {
 3     DymoAddInClass _dymoAddin = new DymoAddInClass();
 4 
 5     // this is the buffer that will hold an array of bytes
 6     object buf = null;
 7 
 8     if (_dymoAddin.Open(System.IO.Path.Combine(
 9                             System.IO.Directory.GetCurrentDirectory(),
10                             @"DatabaseLabel.label")))
11     {
12         // get an array of bytes from Save Stream
13         buf = _dymoAddin.SaveStream();
14 
15         // at this point, you can store buf as a blob
16         // in your database
17         byte[] blob = (byte[])buf;
18     }
19 
20     // to open a label file from a blob in your database
21     // first, read the blob into into an array of bytes
22     // then pass the array of bytes to OpenStream
23     System.IO.MemoryStream ms = new System.IO.MemoryStream();
24 
25     // the following line is psuedo code aimed to illustrate how one
26     // might read a blob into an array of bytes
27     !!!Database.Table("Labels Table").Field("Shipping Label").SaveToStream(ms);
28 
29     // at this point, you can read the bytes
30     _dymoAddin.OpenStream(ms.GetBuffer());
31 }
32 

Printer Related Operations

The DLS SDK (and therefore your SDK applications) can only be used with LabelWriter printers. Because of this, we added printer listing and selection methods to filter out printers that are not compatible with the DLS SDK so your application will not need extra code to deal with printer compatibility and selection issues.

The following example shows how to get a list of LabelWriter printers installed on the computer, and selects a printer named “My LabelWriter” to print a label file:

 1 private void SelectPrinterAndPrintLabelExample()
 2 {
 3     DymoAddInClass _dymoAddin = new DymoAddInClass();
 4 
 5     string[] printers = _dymoAddin.GetDymoPrinters().Split(new char[] { '|' });
 6     foreach (var printer in printers)
 7     {
 8         // find the printer we want to print to
 9         if (printer == "My LabelWriter")
10         {
11             // select it first
12             _dymoAddin.SelectPrinter(printer);
13 
14             // then print the currently open label
15             if (_dymoAddin.IsTwinTurboPrinter(printer))
16             {
17                 // print one copy of label to the left tray
18                 _dymoAddin.Print2(1, false, 0);
19             }
20             else
21             {
22                 // print one copy of label to the only tray
23                 _dymoAddin.Print(1, false);
24             }
25             break;
26         }
27     }
28 }
29 

A new method worth mentioning is the IsPrinterOnline() method, which return the online/offline status of a printer.

Conclusion

Although the DymoAddin object’s name may not invoke any ideas of the functionality it provides, it is an important part of the because of the rich features it provides. I hope this blogs gives you a good idea of the DymoAddin object’s capabilities and how to best use the capabilities for your specific application.

April 3, 2010

First SDK Application

Filed under: SDK Samples — CK Hsu @ 3:10 am
Tags: , ,

So you’ve read somewhere that DYMO LabelWriter printers come with a software developer’s kit, commonly referred to as the DLS SDK. But you might be wondering how DLS SDK can help you and how much time you have to spend learning this SDK before you can start using it. Hopefully this blog entry gives you an idea on how easy it is to get started and also help answer some commonly asked questions by developers.

Before we get too far, let me first explain what the DLS SDK is: it is a programming interface designed specifically for label printing. Using the programming interface (API), one can build a label printing application with minimum amount of work.

Create a label printing application

The following section walks you through the steps of creating a label printing application using the DLS SDK. At the end of the section, you should have an understanding of the basic concepts and the working parts of the DLS SDK.

Step 1. Get the tools you need.

  1. The latest DLS 8 Installer (build 996). It’s important to install the latest version.
  2. The latest DLS SDK Samples. The samples installers installs documentation and technical references to the DLS SDK along with samples to give you an idea of the types of application you can create using the SDK.
  3. Visual Studio 2005 or 2008. I will be using VS2008, but what I do there can easily be done in VS2005.
  4. A PC with a LabelWriter printer connected to it and the latest DLS 8 software installed.

Step 2. Understanding the basic concepts in the DLS SDK

  1. Label File: A label file is a document that contains address, text, barcode, and/or images that you want to print on a label. You can perform various operations on a label file, like Open, Print, SaveAs, etc.
  2. Label Objects: The address, text, and barcode data that are stored inside a label file are logically separated into different label objects. Each object has its own set of properties that control how the object is printed on the label, and the DLS SDK provides access to programmatically create label objects and change label object properties.
  3. Data: The data is a general term for data (i.e. text, address, or image) that you would like to print on a label. The data source can be from user input, a file, or a database.

The DLS SDK ties these three concepts together to enable you to start printing labels in your application with a few lines of code.

Step 3. Defining your application.

To keep the blog short, I will define a simple application that lets users type in a list of to-dos for the day, and print out the list on a label to take with them for the day.

Step 4. Designing a label the application will print (download it here).

  1. start DLS 8, select a shipping label, then change the label orientation to portrait
  2. add a date-time label object on the label
  3. add a text object as header on the label
  4. add a text object as the area to list to-do’s on the label
  5. name the to-do’s text object “todos”
  6. save the label

This video shows you the steps to create the above label.

Step 5. Create an SDK application project in VS2008 (download it here). The important things to note are:

  • make sure the project is targeted for x86 platform. The reason is because the current DSL SDK works only in 32-bit mode (i.e. x86), so if your application is targeted for x64 or AnyCPU, you will not be able to use the DLS SDK.
  • Add a reference to the “DLS 7 Compatibility COM Type Library” in the project. Doing so allows you to use the COM objects that are available in the DLS SDK.

This video shows you the steps to setup the above project.

Step 6. Adding code to take in a list of to-do items from the user, then printing it on label (see listing below).

DymoAddinClass and DymoLabelsClass are the two main objects available from the High-Level COM interface in the DLS SDK. Combined, they provide methods and properties for you to open a label file, set data on label objects in the label file, and print the label. Keep in mind that the DLS SDK is much more extensive than just these two objects, and we will take time to explore them in future blog entries.

1 private void printBtn_Click(object sender, EventArgs e)
2 {
3     DymoAddInClass _dymoAddin = new DymoAddInClass();
4     DymoLabelsClass _dymoLabel = new DymoLabelsClass();
5 
6     // open the to-do-list label we created earlier
7     if (_dymoAddin.Open(@"..\..\..\To-Do's List.label"))
8     {
9         // this call returns a list of objects on the label
10         string[] objNames = _dymoLabel.GetObjectNames(false).Split(new Char [] {'|'});
11 
12         // verify that our text object is on the label
13         if (objNames.Contains("todos"))
14         {
15             // take the to-do's list entered by the user
16             // and put in on the label
17             if (_dymoLabel.SetField("todos", todoEdit.Text))
18             {
19                 // let's print to the first LabelWriter available
20                 // on the pc, if the printer is a TwiTurbo, print
21                 // to the left tray
22                 string[] printers = _dymoAddin.GetDymoPrinters().Split(new char[] { '|' });
23                 if (printers.Count() > 0)
24                 {
25                     if (_dymoAddin.SelectPrinter(printers[0]))
26                     {
27                         if (_dymoAddin.IsTwinTurboPrinter(printers[0]))
28                         {
29                             _dymoAddin.Print2(1, false, 0);
30                         }
31                         else
32                         {
33                             _dymoAddin.Print(1, false);
34                         }
35                     }
36                 }
37             }
38         }
39     }
40 }
41 
(you can download the complete project here).

Done! As you can see, it didn’t take much to create a simple application that prints labels using the DLS SDK

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 60 other followers