Results 1 to 6 of 6

Thread: [RESOLVED] Printing A New Line While Printing

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2010
    Posts
    56

    Resolved [RESOLVED] Printing A New Line While Printing

    Hello, everyone! I am setting up printing (to a printer or file) in my application, and it doesn't seem to print out any new lines that I specify. Here's some of my printing code:

    Code:
    ' Loop through all of the text in the array
            For i = 0 To 9
                If i Mod 2 = 0 Then
                    TheFont = SpecialFont
                Else
                    TheFont = NormalFont
                End If
    
                ' Find out how many characters will fit in the printing area
                e.Graphics.MeasureString(TextArray(i), TheFont, New SizeF(PrintAreaWidth, PrintAreaHeight), fmt, NumCharsFit, NumLinesFilled)
    
                ' Print the text on the page
                e.Graphics.DrawString(TextArray(i), TheFont, Brushes.Black, rectPrintingArea)
    
                ' Add how many characters were printed onto the page
                CurrentChar += NumCharsFit
            Next
    Here are the definitions for the TextArray array:

    Code:
    Dim TextArray(0 To 9) As String
    
            ' Find out the text we're going to print
            TextArray(0) = "Order Number: "
            TextArray(1) = txtOrderNum.Text & vbCrLf
    
            TextArray(2) = "Order Date: "
            TextArray(3) = txtOrderDate.Text & vbCrLf
    
            TextArray(4) = "Order Time: "
            TextArray(5) = txtOrderTime.Text & vbCrLf & vbCrLf
    
            TextArray(6) = "Employee that processed the order: "
            TextArray(7) = txtEmployee.Text & vbCrLf
    
            TextArray(8) = "Customer: "
            TextArray(9) = txtCustomer.Text & vbCrLf
    
            For i = 0 To 9
                TextLength += TextArray(i).Length
            Next
    When I print the output to a PDF file using PDFCreator, it stacks all of the letters onto each other rather than creating new lines at the places I've specified. I've also tried using vbNewLine instead of vbCrLf, but I haven't had any luck. I searched the web for this but couldn't find anything about it. Any help would be greatly appreciated!

  2. #2
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Printing A New Line While Printing

    Try something like:
    vb Code:
    1. 'The current location should be initialized to (Left margin, Top Margin)
    2.         Dim CurrentLocation As PointF = New PointF(0.0F, 0.0F)
    3.         Dim StringSize As SizeF
    4.  
    5.         ' Loop through all of the text in the array
    6.         For i = 0 To 9
    7.             If i Mod 2 = 0 Then
    8.                 TheFont = SpecialFont
    9.             Else
    10.                 TheFont = NormalFont
    11.             End If
    12.  
    13.             ' Find out how many graphic units the text occupies.
    14.             StringSize = e.Graphics.MeasureString(TextArray(i), TheFont, CInt(PrintAreaWidth), fmt)
    15.  
    16.             ' Print the text on the page
    17.             e.Graphics.DrawString(TextArray(i), TheFont, Brushes.Black, CurrentLocation)
    18.  
    19.             'Move the current location so the next line fits below the previous.
    20.             CurrentLocation.Y += StringSize.Height
    21.         Next

    You didn't record the measured string and didn't move the location which the string was written on.
    DrawString and MeasureString.

    Regards
    Tom
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2010
    Posts
    56

    Re: Printing A New Line While Printing

    Quote Originally Posted by ThomasJohnsen View Post
    Try something like:
    vb Code:
    1. 'The current location should be initialized to (Left margin, Top Margin)
    2.         Dim CurrentLocation As PointF = New PointF(0.0F, 0.0F)
    3.         Dim StringSize As SizeF
    4.  
    5.         ' Loop through all of the text in the array
    6.         For i = 0 To 9
    7.             If i Mod 2 = 0 Then
    8.                 TheFont = SpecialFont
    9.             Else
    10.                 TheFont = NormalFont
    11.             End If
    12.  
    13.             ' Find out how many graphic units the text occupies.
    14.             StringSize = e.Graphics.MeasureString(TextArray(i), TheFont, CInt(PrintAreaWidth), fmt)
    15.  
    16.             ' Print the text on the page
    17.             e.Graphics.DrawString(TextArray(i), TheFont, Brushes.Black, CurrentLocation)
    18.  
    19.             'Move the current location so the next line fits below the previous.
    20.             CurrentLocation.Y += StringSize.Height
    21.         Next

    You didn't record the measured string and didn't move the location which the string was written on.
    DrawString and MeasureString.

    Regards
    Tom
    Thank you! That worked very well!

  4. #4
    New Member
    Join Date
    Nov 2019
    Posts
    2

    Re: Printing A New Line While Printing

    Quote Originally Posted by hydrakiller4000 View Post
    Thank you! That worked very well!
    (PrintAreaWidth), fmt) is not declared

    can I see the exact code and declaration you used?

  5. #5
    New Member
    Join Date
    Nov 2019
    Posts
    2

    Re: Printing A New Line While Printing

    Quote Originally Posted by hydrakiller4000 View Post
    Thank you! That worked very well!
    (PrintAreaWidth), fmt) is not declared

    can I see the exact code and declaration you used?

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Printing A New Line While Printing

    This thread is old, over 8 years.
    hydrakiller4000 hasn't been active on the forum in over 7 Years.

    Chances are, you are not going to get a response from him, and probably not from ThomasJohnsen who didn't need to know what fmt was to correct the code.

    Just select MeastureString in the IDE and hit F1 to get information about the various method Overloads.
    If you don't know what you might want fmt to be, then just delete it. There is an overload that doesn't require it, so it technically doesn't matter to you what it was.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width