-
-
Save bombless/d35eb33f2848d89c04f43c7bf8ae975c to your computer and use it in GitHub Desktop.
GDI Printing Example for C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region Win32 GDI | |
private void btnTextDrawCenter_Click(object sender, RoutedEventArgs e) | |
{ | |
var doc = new PrintDocument() | |
{ | |
PrintController = new StandardPrintController(), | |
}; | |
doc.OriginAtMargins = false; | |
doc.PrinterSettings.PrinterName = CurrentPrinter; | |
doc.PrintPage += (s, args) => | |
{ | |
var bounds = args.Graphics.VisibleClipBounds; | |
bounds.Width *= args.Graphics.DpiX / 96.0f; | |
var largeFont = new System.Drawing.Font("Consolas", 72.0f); | |
System.Drawing.Size strSz; | |
using (var renderer = new NativeTextRenderer(args.Graphics)) | |
{ | |
var str = "█<-1\"->█"; | |
renderer.DrawString(str, | |
largeFont, | |
Color.Black, | |
bounds, | |
RawPrinterHelper.TextFormatFlags.Center); | |
strSz = renderer.MeasureString(str, largeFont); | |
bounds.Y += strSz.Height; | |
} | |
using (var renderer = new NativeTextRenderer(args.Graphics)) | |
{ | |
var str = string.Format("bounds: {0}\nStrSize: {0}", bounds.ToString(), strSz.ToString()); | |
renderer.DrawString(str, | |
new System.Drawing.Font("Consolas", 12.0f), | |
Color.Black, | |
bounds, | |
0); | |
} | |
}; | |
doc.Print(); | |
} | |
#endregion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.c-sharpcorner.com/UploadFile/dbeniwal321/using-gdi-print-functionality/ | |
//PrintDirect.cs | |
//shows how to write data directly to the | |
//printer using Win32 APIs. | |
//this code sends Hewlett-Packard PCL5 codes | |
//to the printer to print | |
//out a rectangle in the middle of the page. | |
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; | |
//Text to print with a form-feed character | |
st1 = "This is an example of printing " + | |
"directory to a printer\f"; | |
di.pDocName = "my test document"; | |
di.pDataType = "RAW"; | |
//The "\xlb" means an ASCII escape character | |
st1 = "\xlb*c600a6b0P\f"; | |
//lhPrinter contains the handle for the printer opened | |
//IF lhPrinter is 0, then an error has occurred. | |
PrintDirect.OpenPrinter(\\\\192.168 .1 .101\\ hp1, | |
ref lhPrinter, 0); | |
PrintDirect.StartDocPrinter(lhPrinter, 1, ref di); | |
PrintDirect.StartPagePrinter(lhPrinter); | |
try { | |
//Moves the cursor 900 dots (3 inches at | |
//300 dpi) in from the left margin, and | |
//600 dots (2 inches at 300 dpi) down | |
//from the top margin | |
st1 = "\xlb*p900x600Y"; | |
PrintDirect.WritePrinter(lhPrinter, | |
st1, st1.Length, ref pcWritten); | |
//Using the print model commands for rectangle | |
//dimensions, "600a" specifies a rectangle | |
//with a horizontal size, or width, of 600 dots, | |
//and "6b" specifies a vertical | |
//size, or height, of 6 dots. "0P" selects the | |
//solid black rectangle area fill | |
st1 = "\xlb*c600a6b0P"; | |
PrintDirect.WritePrinter(lhPrinter, | |
st1, st1.Length, ref pcWritten); | |
//Specifies a rectangle with width of | |
//6 dots, height of 600 dots, and a | |
//Fill pattern of solid black | |
st1 = "\xlb*c6a600b0P"; | |
PrintDirect.WritePrinter(lhPrinter, | |
st1, st1.Length, ref pcWritten); | |
//Moves the current cursor position to | |
//900 dots from the left margin and | |
//1200 dots down from the top margin | |
st1 = "\xlb*p900x1200Y"; | |
PrintDirect.WritePrinter(lhPrinter, | |
st1, st1.Length, ref pcWritten); | |
//Specifies a rectangle with a width | |
//of 606 dots, a height of 6 dots, and a | |
//fill patter of solid balck | |
st1 = "\xlb*c606a6b0P"; | |
PrintDirect.WritePrinter(lhPrinter, | |
st1, st1.Length, ref pcWritter); | |
//Move the current cursor position to 1500 | |
//dots in from the left margin and | |
//600 dots down from the top margin | |
st1 = "\xlb*p1500x600Y"; | |
PrintDirect.WritePrinter(lhPrinter, | |
st1, st1.Length, ref pcWritten); | |
//Specifies a rectangle with a width of 6 dots, | |
//a height of 600 dots, and a | |
//fill patter of solid black | |
st1 = "\xlb*c6a600b0P"; | |
PrintDirect.WritePrinter(lhPrinter, | |
st1, st1.Length, ref pcWritter); | |
//Send a from-feed character to the printer | |
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment