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 Question - characters per line

Printing Question - characters per line

Scheduled Pinned Locked Moved C#
questiongraphics
6 Posts 5 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.
  • A Offline
    A Offline
    al3xutzu00
    wrote on last edited by
    #1

    Hi Guys, I am trying to print some items from a listview. I read the whole listview item in a String and then i try to see how many characters i can write on a line of the page that i want to print one :

    public void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    ...
    float characterPerLine = e.MarginBounds.Width/printFont.GetHeight(e.Graphics);
    ...
    }

    but this is not returning me the correct number. The question is ... how can i calculate the number of characters per line when printing? Kind regards, Alex

    “Be the change you want to see in the world.”

    D L 2 Replies Last reply
    0
    • A al3xutzu00

      Hi Guys, I am trying to print some items from a listview. I read the whole listview item in a String and then i try to see how many characters i can write on a line of the page that i want to print one :

      public void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
      {
      ...
      float characterPerLine = e.MarginBounds.Width/printFont.GetHeight(e.Graphics);
      ...
      }

      but this is not returning me the correct number. The question is ... how can i calculate the number of characters per line when printing? Kind regards, Alex

      “Be the change you want to see in the world.”

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Sooo you're dividing the width by the height of a character to get the number of possible characters?? You don't see a problem with that?? Wouldn't you divide the width the page by the width of a character to get that?? BTW, the math you've chosen only work with fixed-width fonts. It will not return an accurate value for a proportional width font (which most fonts are!)

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      A 1 Reply Last reply
      0
      • A al3xutzu00

        Hi Guys, I am trying to print some items from a listview. I read the whole listview item in a String and then i try to see how many characters i can write on a line of the page that i want to print one :

        public void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
        ...
        float characterPerLine = e.MarginBounds.Width/printFont.GetHeight(e.Graphics);
        ...
        }

        but this is not returning me the correct number. The question is ... how can i calculate the number of characters per line when printing? Kind regards, Alex

        “Be the change you want to see in the world.”

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, as an approximation you could come up with an "äverage string" of say 100 characters, then feed it to e.Graphics.MeasureString and get the returned Width. if you need an (almost) exact value, you should use MeasureString for each actual string that interests you. The "(almost)" refers to a CP article on some minor anomalies, the result being of by a few pixels. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


        1 Reply Last reply
        0
        • D Dave Kreskowiak

          Sooo you're dividing the width by the height of a character to get the number of possible characters?? You don't see a problem with that?? Wouldn't you divide the width the page by the width of a character to get that?? BTW, the math you've chosen only work with fixed-width fonts. It will not return an accurate value for a proportional width font (which most fonts are!)

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          A Offline
          A Offline
          al3xutzu00
          wrote on last edited by
          #4

          Sorry , you are right , my bad. i tried this : float characterPerLine = e.MarginBounds.Width/printFont.Size; and it worked. but it still is not einough... I know that i shoud devide the widith of the line with the widith of the font but there is no printFont.GetWidith in c# intelisense.

          “Be the change you want to see in the world.”

          modified on Tuesday, May 5, 2009 3:24 PM

          CPalliniC OriginalGriffO 2 Replies Last reply
          0
          • A al3xutzu00

            Sorry , you are right , my bad. i tried this : float characterPerLine = e.MarginBounds.Width/printFont.Size; and it worked. but it still is not einough... I know that i shoud devide the widith of the line with the widith of the font but there is no printFont.GetWidith in c# intelisense.

            “Be the change you want to see in the world.”

            modified on Tuesday, May 5, 2009 3:24 PM

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            al3xutzu00 wrote:

            I know that i shoud devide the widith of the line with the widith of the font but there is no printFont.GetWidith in c# intelisense.

            That is a hint, I suppose... :rolleyes:

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • A al3xutzu00

              Sorry , you are right , my bad. i tried this : float characterPerLine = e.MarginBounds.Width/printFont.Size; and it worked. but it still is not einough... I know that i shoud devide the widith of the line with the widith of the font but there is no printFont.GetWidith in c# intelisense.

              “Be the change you want to see in the world.”

              modified on Tuesday, May 5, 2009 3:24 PM

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              al3xutzu00 wrote:

              I know that i shoud devide the widith of the line with the widith of the font but there is no printFont.GetWidith in c# intelisense.

              There is no printFont.GetWidth in C# for a very good reason. It doesn't make any sense. Since most windows fonts are proportional there is no fixed width of a character. I.e. an 'i' character is a lot narrower than a 'W'. If there is no fixed character width in a font, you can't have a method which returns the width of a character.

              No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              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