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

  1. //PrintDirect.cs
  2. //shows how to write data directly to the
  3. //printer using Win32 APIs.
  4. //this code sends Hewlett-Packard PCL5 codes
  5. //to the printer to print
  6. //out a rectangle in the middle of the page.
  7. using System;
  8. using System.Text;
  9. using System.Runtime.InteropServices;
  10. [StructLayout(LayoutKind.Sequential)]
  11. public struct DOCINFO {
  12. [MarshalAs(UnmangedType.LPWStr)]
  13. public string pDocName;
  14. [MarshalAs(UnmangedType.LPWStr)]
  15. public string pOutputFile;
  16. [MarshalAs(UnmangedType.LPWStr)]
  17. public string pDataType;
  18. }
  19. public class PrintDirect {
  20. [DllImport("winspool.drv",
  21. CharSet = CharSet.Unicode, ExactSpelling = false,
  22. CallingConvention = CallingConvention.StdCall)]
  23. public static extern long OpenPrinter(String pPrinterName,
  24. ref IntPtr phPrinter, int pDefault);
  25. [DllImport("winspool.drv",
  26. CharSet = CharSet.Unicode, ExactSpelling = false,
  27. CallingConvention = CallingConvention.StdCall)]
  28. public static extern long StartDocPrinter(Int hPrinter,
  29. int Level, ref DOCINFO pDocInfo);
  30. [DllImport("winspool.drv",
  31. CharSet = CharSet.Unicode, ExactSpelling = false,
  32. CallingConvention = CallingConvention.StdCall)]
  33. public static extern long StartPagePrinter(
  34. IntPtr hPrinter);
  35. [DllImport("winspool.drv",
  36. CharSet = CharSet.Unicode, ExactSpelling = false,
  37. CallingConvention = CallingConvention.StdCall)]
  38. public static extern long WritePrinter(IntPtr hPrinter,
  39. string data, int buf, ref int pcWrittern);
  40. [DllImport("winspool.drv",
  41. CharSet = CharSet.Unicode, ExactSpelling = false,
  42. CallingConvention = CallingConvention.StdCall)]
  43. public static extern long EndPrinter(IntPtr hPrinter);
  44. [DllImport("winspool.drv",
  45. CharSet = CharSet.Unicode, ExactSpelling = false,
  46. CallingConvention = CallingConvention.StdCall)]
  47. public static extern long EndDocPrinter(IntPtr hPrinter);
  48. [DllImport("winspool.drv",
  49. CharSet = CharSet.Unicode, ExactSpelling = false,
  50. CallingConvention = CallingConvention.StdCall)]
  51. public static extern long ClosePrinter(IntPtr hPrinter);
  52. }
  53. public static void Main() {
  54. System.IntPtr lhPrinter =
  55. new System.IntPtr();
  56. DOCINFO di = new DOCINFO();
  57. int pcWritten = 0;
  58. string st1;
  59. //Text to print with a form-feed character
  60. st1 = "This is an example of printing " +
  61. "directory to a printer\f";
  62. di.pDocName = "my test document";
  63. di.pDataType = "RAW";
  64. //The "\xlb" means an ASCII escape character
  65. st1 = "\xlb*c600a6b0P\f";
  66. //lhPrinter contains the handle for the printer opened
  67. //IF lhPrinter is 0, then an error has occurred.
  68. PrintDirect.OpenPrinter(\\\\192.168 .1 .101\\ hp1,
  69. ref lhPrinter, 0);
  70. PrintDirect.StartDocPrinter(lhPrinter, 1, ref di);
  71. PrintDirect.StartPagePrinter(lhPrinter);
  72. try {
  73. //Moves the cursor 900 dots (3 inches at
  74. //300 dpi) in from the left margin, and
  75. //600 dots (2 inches at 300 dpi) down
  76. //from the top margin
  77. st1 = "\xlb*p900x600Y";
  78. PrintDirect.WritePrinter(lhPrinter,
  79. st1, st1.Length, ref pcWritten);
  80. //Using the print model commands for rectangle
  81. //dimensions, "600a" specifies a rectangle
  82. //with a horizontal size, or width, of 600 dots,
  83. //and "6b" specifies a vertical
  84. //size, or height, of 6 dots. "0P" selects the
  85. //solid black rectangle area fill
  86. st1 = "\xlb*c600a6b0P";
  87. PrintDirect.WritePrinter(lhPrinter,
  88. st1, st1.Length, ref pcWritten);
  89. //Specifies a rectangle with width of
  90. //6 dots, height of 600 dots, and a
  91. //Fill pattern of solid black
  92. st1 = "\xlb*c6a600b0P";
  93. PrintDirect.WritePrinter(lhPrinter,
  94. st1, st1.Length, ref pcWritten);
  95. //Moves the current cursor position to
  96. //900 dots from the left margin and
  97. //1200 dots down from the top margin
  98. st1 = "\xlb*p900x1200Y";
  99. PrintDirect.WritePrinter(lhPrinter,
  100. st1, st1.Length, ref pcWritten);
  101. //Specifies a rectangle with a width
  102. //of 606 dots, a height of 6 dots, and a
  103. //fill patter of solid balck
  104. st1 = "\xlb*c606a6b0P";
  105. PrintDirect.WritePrinter(lhPrinter,
  106. st1, st1.Length, ref pcWritter);
  107. //Move the current cursor position to 1500
  108. //dots in from the left margin and
  109. //600 dots down from the top margin
  110. st1 = "\xlb*p1500x600Y";
  111. PrintDirect.WritePrinter(lhPrinter,
  112. st1, st1.Length, ref pcWritten);
  113. //Specifies a rectangle with a width of 6 dots,
  114. //a height of 600 dots, and a
  115. //fill patter of solid black
  116. st1 = "\xlb*c6a600b0P";
  117. PrintDirect.WritePrinter(lhPrinter,
  118. st1, st1.Length, ref pcWritter);
  119. //Send a from-feed character to the printer
  120. st1 = "\f";
  121. PrintDirect.WritePrinter(lhPrinter,
  122. st1, st1.Length, ref pcWritten);
  123. } catch (Exception e) {
  124. Console.WriteLine(e.Message);
  125. }
  126. PrintDirect.EndPagePrinter(lhPrinter);
  127. PrintDirect.EndDocPrinter(lhPrinter);
  128. PrintDirect.ClosePrinter(lhPrinter);
  129. }
  130. }
Using this code will enable us to drive a printer at its maximum output rate.