One reason for using GDI maybe speed and familiarity with GDI or having more
control over the printer.
Until now we have been selecting objects such as fonts and lines and then
drawing on a page, which is then printed out. Keep in mind that all the fonts
you can use within the .NET environment have to be TrueType fonts. Before
TrueType came along, there was something called PCL (Printer Control Language),
also known as bitmap fonts. So what's the difference? you may ask. It's simple:
A PCL or bitmap font is made up of patterns of dots that represent each letter.
The problem is that a different PCL font was required for every size of the letter
needed, such as 12, 14, and so on. Different PCL, fonts were needed even for
italic and bold versions! As you can imagine, it was necessary to have lots of
PCL fonts to maintain the flexibility we take for granted today.
TrueType fonts, on the other hand, are a lot more flexible. The reason is that
the fonts are mathematical representations of each letter rather than a pattern
of dots. If I decide I need a Times New Roman font at size 20, the font is
simply recalculated rather than just a different pattern of dots being loaded.
What happens if your printer does not support the TrueType font you have
selected? The only way to print it is to send what you want to print to the
printers as graphics, which can be time-consuming if you're creating large
printouts.
The code in Listing 14.3, you would be able to create detailed pages consisting
of multiple fonts and graphics. The nice thing is that they can all be created
by just sending text to the printer rather than using graphics command.
You may want to change the printer before you rest this code. The following line
of code specifies the printer:
PrintDirect.OpenPrinter (\\\\192.168.1.101\\hpl,ref lhPrinter, 0);
LISTING 14.3: Using GDI print functionality in a managed application
-
-
-
-
-
-
- using System;
- using System.Text;
- using System.Runtime.InteropServices;
-
- [StructLayout(LayoutKind.Sequential)]
- public struct DOCINFO {
- [MarshalAs(UnmangedType.LPWStr)]
- public string pDocName;
- [MarshalAs(UnmangedType.LPWStr)]
- public string pOutputFile;
- [MarshalAs(UnmangedType.LPWStr)]
- public string pDataType;
- }
-
- public class PrintDirect {
- [DllImport("winspool.drv",
- CharSet = CharSet.Unicode, ExactSpelling = false,
- CallingConvention = CallingConvention.StdCall)]
- public static extern long OpenPrinter(String pPrinterName,
- ref IntPtr phPrinter, int pDefault);
-
- [DllImport("winspool.drv",
- CharSet = CharSet.Unicode, ExactSpelling = false,
- CallingConvention = CallingConvention.StdCall)]
- public static extern long StartDocPrinter(Int hPrinter,
- int Level, ref DOCINFO pDocInfo);
-
- [DllImport("winspool.drv",
- CharSet = CharSet.Unicode, ExactSpelling = false,
- CallingConvention = CallingConvention.StdCall)]
- public static extern long StartPagePrinter(
- IntPtr hPrinter);
-
- [DllImport("winspool.drv",
- CharSet = CharSet.Unicode, ExactSpelling = false,
- CallingConvention = CallingConvention.StdCall)]
- public static extern long WritePrinter(IntPtr hPrinter,
- string data, int buf, ref int pcWrittern);
-
- [DllImport("winspool.drv",
- CharSet = CharSet.Unicode, ExactSpelling = false,
- CallingConvention = CallingConvention.StdCall)]
- public static extern long EndPrinter(IntPtr hPrinter);
-
- [DllImport("winspool.drv",
- CharSet = CharSet.Unicode, ExactSpelling = false,
- CallingConvention = CallingConvention.StdCall)]
- public static extern long EndDocPrinter(IntPtr hPrinter);
-
- [DllImport("winspool.drv",
- CharSet = CharSet.Unicode, ExactSpelling = false,
- CallingConvention = CallingConvention.StdCall)]
- public static extern long ClosePrinter(IntPtr hPrinter);
- }
-
- public static void Main() {
- System.IntPtr lhPrinter =
- new System.IntPtr();
- DOCINFO di = new DOCINFO();
- int pcWritten = 0;
- string st1;
-
-
- st1 = "This is an example of printing " +
- "directory to a printer\f";
- di.pDocName = "my test document";
- di.pDataType = "RAW";
-
-
- st1 = "\xlb*c600a6b0P\f";
-
-
- PrintDirect.OpenPrinter(\\\\192.168 .1 .101\\ hp1,
- ref lhPrinter, 0);
- PrintDirect.StartDocPrinter(lhPrinter, 1, ref di);
- PrintDirect.StartPagePrinter(lhPrinter);
-
- try {
-
-
-
-
- st1 = "\xlb*p900x600Y";
- PrintDirect.WritePrinter(lhPrinter,
- st1, st1.Length, ref pcWritten);
-
-
-
-
-
-
-
- st1 = "\xlb*c600a6b0P";
- PrintDirect.WritePrinter(lhPrinter,
- st1, st1.Length, ref pcWritten);
-
-
-
-
- st1 = "\xlb*c6a600b0P";
- PrintDirect.WritePrinter(lhPrinter,
- st1, st1.Length, ref pcWritten);
-
-
-
-
-
- st1 = "\xlb*p900x1200Y";
- PrintDirect.WritePrinter(lhPrinter,
- st1, st1.Length, ref pcWritten);
-
-
-
-
- st1 = "\xlb*c606a6b0P";
- PrintDirect.WritePrinter(lhPrinter,
- st1, st1.Length, ref pcWritter);
-
-
-
- st1 = "\xlb*p1500x600Y";
- PrintDirect.WritePrinter(lhPrinter,
- st1, st1.Length, ref pcWritten);
-
-
-
-
- st1 = "\xlb*c6a600b0P";
- PrintDirect.WritePrinter(lhPrinter,
- st1, st1.Length, ref pcWritter);
-
-
- st1 = "\f";
- PrintDirect.WritePrinter(lhPrinter,
- st1, st1.Length, ref pcWritten);
- } catch (Exception e) {
- Console.WriteLine(e.Message);
- }
-
- PrintDirect.EndPagePrinter(lhPrinter);
- PrintDirect.EndDocPrinter(lhPrinter);
- PrintDirect.ClosePrinter(lhPrinter);
- }
- }
Using this code will enable us to drive a printer at its maximum output rate.