Printing Duplex In Micro Focus Visual COBOL For .NET

Micro Focus released VisualCOBOL R4 earlier this year and it has turned out to be an awesome addition to Visual Studio 2010. The development teams have done a great job in delivering a very usable product. To help you learn about VisualCOBOL and COBOL.NET in a managed environment we’d like to provide some example solutions. Now there are great samples that come with VisualCOBOL but they don’t define or detail one specific control, they show a finished application and the user has to read through the code to figure out how the code was created.

Our examples will focus on a single control. We’ll show you how to manipulate the properties of that control programmatically. We may have to include one additional control but the focus on each project will be on a single control with a supporting role for the additional control. This article is about the DateTimePicker control.

Micro Focus has always tried to provide a full, in-depth development environment for COBOL developers. In previous releases of Micro Focus NetExpress it was possible to send files to the printer and have them print in duplex mode. The technique used was to call a Micro Focus supplied process using the following format.

format

and passing it a series of parameters similar to the following,

parameters

Unfortunately this process applies to a non-managed environment. More and more clients are moving to a managed environment under .NET. How then does one translate the above Micro Focus specific implementation of duplex printing to managed-COBOL?

The following article shows how to use the properties available in the Print Document class to enable duplex printing in .NET and adapt the Microsoft approach to handling printers.

Print Document Class

Before we dig into the specifics of implementing duplex printing we need to review the Print Document class. According to Microsoft the Print Document class “Defines a reusable object that sends output to a printer, when printing from a Windows Forms application.” I used this link to obtain information on the class. It is a part of the System.Drawing.Printing namespace.

The class we are going to be most concerned with is ‘PrinterSettings’ and the link for the properties to the class in MSDN. The PrinterSettings class, as the name implies, enables control of the settings related to a printer. Reviewing the class you’ll see there are over 20 properties you can access and alter. We are going to be concentrating on the ‘Duplex’ property.

If you click on the ‘Duplex’ property in the above link you’ll be redirected to a page that explains the Duplex property contains an enumeration of available settings to enable duplexing. Wonderful! But what are these values? In the description of the Duplex property is a link to the enumerated values and selecting it yields a table with the following values:

value

By default “most” printers are set to simplex. To enable duplex printing you simply need to readjust the setting prior to sending a file to the printer. Let’s see how to do that.

WinForm

If you’re familiar with my articles you know I like to use a WinForm as a container for the information to be presented. The information in this article however can be used without a WinForm by simply instantiating the classes found in our example. Yes I still used a WinForm because it does lend a way to walk through the information and present the ideas.

idea

Our form is a very simple screen with a label and button. The button has a ‘Click’ event that will begin our process of setting up the environment to enable duplex printing.

Preparation

For our example we are going to read in a file that has a sufficient number of pages to generate a multi-page printout. We will need to setup a StreamReader to read the file in. The StreamReader is actually only used to get our data for printing, it isn’t necessary to enable the duplex printing. We also need to establish some variables that are going to be used by the PrintDocument methods when invoked. The variables we will need apply to the font to be used when the document is printed as well as what color the document will be printed in. These variables will be established as ‘global’ variables for the class, established in the class working-storage section as:

Preparation

Notice the use of the ‘type’ identifier? This construct enables us to define variables as different object types as used in .NET. Distributed developers have been using this technique for a long time and Micro Focus has enabled this technique in Visual COBOL.

Button Click Event

Our process is started when the user clicks on the button, or a button-click-event. When this occurs the first thing we are going to do is a bit more housekeeping to prepare for the actual print event. The housekeeping details we are going to take care of are select the file we are going to print, set the font to be used for printing and set the font color. These three tasks are accomplished with the following code:

code

We next need to set the duplexing mode for the document to be printed. This is done in the ‘printDocument’ object we added to the form. Taking a look at the properties for this control show we have used the default name of ‘printDocument1’.

properties

The properties we are going to set are not visible in the standard properties window. This is because the properties are actually part of an inherited class (PrinterSettings) the printDocument object uses. Let’s look at the code to set the duplex mode and then explain.

Before we enable the duplex mode for your printer, let’s make sure the printer you’ve chosen can indeed print in duplex mode. To do that we are going to check the ‘CanDumplex’ property. The CanDuplex property contains a Boolean value of ‘true’ or ‘false’. If the printer selected can indeed print in duplex mode we will then set it to vertical. If the printer cannot print in duplex then we well verify the duplex mode is turned off by setting it to ‘simplex’ mode. Using an ‘if’ statement the code to check for the ability to duplex and then setting the proper mode looks like:

code

We see our printDocument object, followed by the PrinterSettings class. As we enter the command intellisense provides us with the options available.

option

Selecting Duplex we can see we can either get the setting for the current duplex mode or set it. We are going to set it so we select Duplex and continue with adding the rest of our line. Remember, the Duplex property takes an enumeration as a value. To set it we simply use the ‘type’ construct and point to the enumerator value we would like. In our case this is ‘System.Drawing.Printing.Duplex::Vertical’.

Now that we have set our duplex mode we next need to tell the runtime system what printer to send the output to. There are a number of ways to accomplish this. I chose to hard code a local printer in my environment that supports duplex mode,

duplex

To update the sample to work in your environment, simply change the value in quotes from “HPD9C84D (HP Officeject Pro 8600)” to a printer that supports duplexing in your environment.

Time to Print!

We have set the properties to enable duplex printing, provided a file to print and printer to print to. The only left is to invoke the print function. This is done via printDocument control with the following line of code.

code

When this line is executed the ‘PrintPage’ event is invoked for each page to be printed without invoking any user dialog. While the overall process may seem cumbersome once you understand it and have a working sample it can be re-used.

To begin we will define some variables that will control positioning of our page:

variables

The variables are defined as:

 Variable  Description
 ypos  Position along the ‘y’ axis where we want to print the line.
 topMargin  As the name implies, the top margin of the page.
 line-count  How may lines have we printed.
 lines-per-page How many lines do we want to print per page.
 printLine The actual line of text to be printed.

For our sample we have set the following values for each of the variables,

variables

Notice how we set the value for topMargin? We used the information that was passed into the method about the current printer.

We have wrapped our printing in a ‘perform’ loop. The loop will print a line a text until the number of lines printed exceeds the number of lines per page. The first line of code reads in a line to print from the file we defined earlier.

code

We then check using an ‘if’ statement if the line is null (empty) or if there is something to print. If there is something to print we need to determine where on the page we are by calculating the ypos value. The statement begins with the value of the top margin, adds in the number of lines printed times the height of the font being used.

code

The formula is from the PrintDocument Print method on MSDN found at the following location.
The actual line pf code to print the line is,

code

We are invoking the DrawString method from the Graphics class. At the end of each page we need to determine if there is more to print. Remember, the method only prints a page of text at a time and needs to loop through if there is more to print. To check if there is more to print we check to see if the line of code we just read is not blank (or null). If it is not then we set the Boolean property ‘HasMorePages’ to true with the following code,

code

There really isn’t anything to show as the outcome other than the printed file! Download the sample and review it, update it to work at your location and you’ll see the process really isn’t that bad to enable duplex printing at your location if your printers support it. While you’re working on that try some of the other updates (color, font) to the print stream and see how you can tailor your printed output. Once you understand how you can then see you can tailor specific lines based on the content of the line. The possibilities are endless. Experiment, learn and most of all… have fun doing it!

Wrap-Up

Duplex printing in .NET can be accomplished thanks to the framework and the classes available. Understanding which class to use and how to manipulate it will take some time but once you get the hang of it you’ll see as a COBOL developer you can accomplish the same tasks in COBOL as others can in C# or VB.NET or any of the other .NET languages.

The ZIP file has all the necessary source code for you to follow along and see how to update the properties we’ve described in the article. Read through the code; learn from it and use it in your projects. Even though we used a WinForm to present the information, the same concepts apply to WPF or WebForms. Yes there may be differences but this should at least serve as a starting point to help you figure out how to manipulate similar properties in the other presentation environments. Also, if you have a property you’re not sure how to work with send it to us. Chances are if you’re having questions someone else is also and we’d like to help you figure it out and expand our examples!!

If you’d like to try out the samples and don’t have a copy of Micro Focus Visual COBOL, you can download the Personal Edition of Visual COBOL for free from the following link.


Similar Articles