Printing a blank page
-
I am trying to print formatted text with the Win32 Api using the DrawText function and it is printing out a blank page. Does anybody know what I could be doing wrong? Thanks
-
I am trying to print formatted text with the Win32 Api using the DrawText function and it is printing out a blank page. Does anybody know what I could be doing wrong? Thanks
There are many, many ways to write code incorrectly. In this sort of situation, we won't be able to do much without seeing the code that doesn't work. John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek. -
There are many, many ways to write code incorrectly. In this sort of situation, we won't be able to do much without seeing the code that doesn't work. John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.Here is the code. Thanks alot public IntPtr CreateFont(IntPtr hDC, string fontName, int size, FontStyle style) { LOGFONT lf = new PrinterApi.LOGFONT(); FONT_WEIGHT_FLAGS weight = FONT_WEIGHT_FLAGS.FW_DONTCARE; if (Convert.ToBoolean(style & FontStyle.Bold)) { weight |= FONT_WEIGHT_FLAGS.FW_BOLD; } if (Convert.ToBoolean(style & FontStyle.Regular)) { weight |= FONT_WEIGHT_FLAGS.FW_REGULAR; } lf.lfHeight = (-1) * GetFontSize(hDC, size); lf.lfWidth = 0; lf.lfEscapement = 0; lf.lfOrientation = 0; lf.lfWeight = weight; lf.lfItalic = Convert.ToByte(style & FontStyle.Italic); lf.lfUnderline = Convert.ToByte(style & FontStyle.Underline); lf.lfStrikeout = Convert.ToByte(style & FontStyle.Strikeout); lf.lfOutPrecision = CHARACTER_PRECISION.OUT_TT_ONLY_PRECIS; lf.lfClipPrecision = CLIP_PRECISION.CLIP_DEFAULT_PRECIS; lf.lfPitchAndFamily = (byte)FONT_PITCH.DEFAULT_PITCH | (byte)FONT_FAMILY.FF_DONTCARE; lf.lfFaceName = new byte[LF_FACESIZE]; byte[] temp = Encoding.ASCII.GetBytes(fontName); for (int i = 0; i < lf.lfFaceName.Length; i++) { if (i >= temp.Length) { lf.lfFaceName[i] = 0; } else { lf.lfFaceName[i] = temp[i]; } } return CreateFontIndirect(ref lf); } int GetFontSize(IntPtr hDC, int size) { return size / 72 * GetDeviceCaps(hDC, LOGPIXELSY); } public void Method() { string Text = "Some Text"; IntPtr hDC = CreateDC(null, @"Network Imagistics im3510 PCL5e", null, 0); DOCINFO doc = new PrinterApi.DOCINFO(); doc.cbSize = Marshal.SizeOf(typeof(DOCINFO)); doc.lpszDocName = "Test Win32Printer"; doc.lpszOutput = null; if (StartDoc(hDC, doc)) { StartPage(hDC); try { RECT rectStruct = new PrinterApi.RECT(); rectStruct.Left = 0; rectStruct.Top = 0; rectStruct.Right = 600; rectStruct.Bottom = 600; IntPtr font = CreateFont(hDC, "Times New Roman", 20, 0); IntPtr oldFont = SelectObject(hDC, font); DrawText(hDC, Text, Text.Length, ref rectStruct, DRAWTEXT_FORMAT_FLAGS.DT_NOCLIP); SelectObject(hDC, oldFont); DeleteObject(font); EndPage(hDC); EndDoc(hDC); } catch (Exception e) { AbortDoc(hDC); throw e; } finally { } } DeleteDC(hDC); }
-
Here is the code. Thanks alot public IntPtr CreateFont(IntPtr hDC, string fontName, int size, FontStyle style) { LOGFONT lf = new PrinterApi.LOGFONT(); FONT_WEIGHT_FLAGS weight = FONT_WEIGHT_FLAGS.FW_DONTCARE; if (Convert.ToBoolean(style & FontStyle.Bold)) { weight |= FONT_WEIGHT_FLAGS.FW_BOLD; } if (Convert.ToBoolean(style & FontStyle.Regular)) { weight |= FONT_WEIGHT_FLAGS.FW_REGULAR; } lf.lfHeight = (-1) * GetFontSize(hDC, size); lf.lfWidth = 0; lf.lfEscapement = 0; lf.lfOrientation = 0; lf.lfWeight = weight; lf.lfItalic = Convert.ToByte(style & FontStyle.Italic); lf.lfUnderline = Convert.ToByte(style & FontStyle.Underline); lf.lfStrikeout = Convert.ToByte(style & FontStyle.Strikeout); lf.lfOutPrecision = CHARACTER_PRECISION.OUT_TT_ONLY_PRECIS; lf.lfClipPrecision = CLIP_PRECISION.CLIP_DEFAULT_PRECIS; lf.lfPitchAndFamily = (byte)FONT_PITCH.DEFAULT_PITCH | (byte)FONT_FAMILY.FF_DONTCARE; lf.lfFaceName = new byte[LF_FACESIZE]; byte[] temp = Encoding.ASCII.GetBytes(fontName); for (int i = 0; i < lf.lfFaceName.Length; i++) { if (i >= temp.Length) { lf.lfFaceName[i] = 0; } else { lf.lfFaceName[i] = temp[i]; } } return CreateFontIndirect(ref lf); } int GetFontSize(IntPtr hDC, int size) { return size / 72 * GetDeviceCaps(hDC, LOGPIXELSY); } public void Method() { string Text = "Some Text"; IntPtr hDC = CreateDC(null, @"Network Imagistics im3510 PCL5e", null, 0); DOCINFO doc = new PrinterApi.DOCINFO(); doc.cbSize = Marshal.SizeOf(typeof(DOCINFO)); doc.lpszDocName = "Test Win32Printer"; doc.lpszOutput = null; if (StartDoc(hDC, doc)) { StartPage(hDC); try { RECT rectStruct = new PrinterApi.RECT(); rectStruct.Left = 0; rectStruct.Top = 0; rectStruct.Right = 600; rectStruct.Bottom = 600; IntPtr font = CreateFont(hDC, "Times New Roman", 20, 0); IntPtr oldFont = SelectObject(hDC, font); DrawText(hDC, Text, Text.Length, ref rectStruct, DRAWTEXT_FORMAT_FLAGS.DT_NOCLIP); SelectObject(hDC, oldFont); DeleteObject(font); EndPage(hDC); EndDoc(hDC); } catch (Exception e) { AbortDoc(hDC); throw e; } finally { } } DeleteDC(hDC); }
Well, I haven't done Win32 printing for a while, but here are a few pointers/questions. * Why use Win32 printing from C#, when the .NET Framework provides an easier method? * When using Win32 routines, you need to check return codes, and call GetLastError() -- especially when things aren't working as you expect. It is quite likely that one of the two will give you a strong hint. * Check basic things like - Is the text color white? - Am I really putting the text where I thought I was? - Can my printer even print in the area that I'm placing the text, or is it cutting that off? - Am I getting back a valid font? * You are simultaneously dealing with Interop issues and Win32 issues. Unless you understand them both well, the confusion is going to be much greater than if you were dealing with only on at a time. John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek. -
Well, I haven't done Win32 printing for a while, but here are a few pointers/questions. * Why use Win32 printing from C#, when the .NET Framework provides an easier method? * When using Win32 routines, you need to check return codes, and call GetLastError() -- especially when things aren't working as you expect. It is quite likely that one of the two will give you a strong hint. * Check basic things like - Is the text color white? - Am I really putting the text where I thought I was? - Can my printer even print in the area that I'm placing the text, or is it cutting that off? - Am I getting back a valid font? * You are simultaneously dealing with Interop issues and Win32 issues. Unless you understand them both well, the confusion is going to be much greater than if you were dealing with only on at a time. John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.The reason that I am trying to use Win32 to print instead of the .Net framework is because the project I am working on is a mass printing system and the .Net framework is too slow to keep up. How do I change the text color? Thanks
-
Well, I haven't done Win32 printing for a while, but here are a few pointers/questions. * Why use Win32 printing from C#, when the .NET Framework provides an easier method? * When using Win32 routines, you need to check return codes, and call GetLastError() -- especially when things aren't working as you expect. It is quite likely that one of the two will give you a strong hint. * Check basic things like - Is the text color white? - Am I really putting the text where I thought I was? - Can my printer even print in the area that I'm placing the text, or is it cutting that off? - Am I getting back a valid font? * You are simultaneously dealing with Interop issues and Win32 issues. Unless you understand them both well, the confusion is going to be much greater than if you were dealing with only on at a time. John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.Do you know of a good resource on printing with Win32 Api? Everything that I have found has been very vague and not as detailed as I would like. Thanks alot. I really appreciate your help.
-
Do you know of a good resource on printing with Win32 Api? Everything that I have found has been very vague and not as detailed as I would like. Thanks alot. I really appreciate your help.
I haven't personally found (or really needed) a good printing resource for Win32. However, my impression is that it is primarily generic Win32 GDI calls, with a different dpi and size. So, a reasonable way to test your code would be to separate the code that does actual drawing into a separate function, then send that function any DC handle. When trying it out, you can draw straight to the screen. Once that works, you can draw to the printer DC. SetTextColor() is what my memory tells me about changing the text color. John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.