Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Printing a blank page

Printing a blank page

Scheduled Pinned Locked Moved C#
jsonquestion
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shaun Becker
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • S Shaun Becker

      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

      J Offline
      J Offline
      John Fisher
      wrote on last edited by
      #2

      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.

      S 1 Reply Last reply
      0
      • J John Fisher

        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.

        S Offline
        S Offline
        Shaun Becker
        wrote on last edited by
        #3

        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); }

        J 1 Reply Last reply
        0
        • S Shaun Becker

          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); }

          J Offline
          J Offline
          John Fisher
          wrote on last edited by
          #4

          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.

          S 2 Replies Last reply
          0
          • J John Fisher

            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.

            S Offline
            S Offline
            Shaun Becker
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • J John Fisher

              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.

              S Offline
              S Offline
              Shaun Becker
              wrote on last edited by
              #6

              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.

              J 1 Reply Last reply
              0
              • S Shaun Becker

                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.

                J Offline
                J Offline
                John Fisher
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups