Results 1 to 9 of 9

Thread: NEED HELP...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2000
    Posts
    26

    Question

    I know you people probably have answered this one a million times, but I wasn't able to find the thread, so...
    How do you set the margin for a printed page. For example, I am printing the contents of a text box with -
    printer.print (form.txtbox)
    Anywho, it prints to close to the top, bottom, and side margins. I manually put spaces on top and left margin, but that doesn't work for printing 2 nd page or bottom margin.
    HELP ME PLEASE!!!!!

  2. #2

    Thread Starter
    Junior Member
    Join Date
    May 2000
    Posts
    26

    Angry

    I know somebody has to know the answer to something so simple... Are you guys ignoring this because it's too simple?

  3. #3
    Guest
    Try using the CurrentX property of the printer.


  4. #4
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    My god that is a hard quesiotn. After much effort i have knocked up the basics for you. it is not perfect, and i have not tested it thourouhgly, but you can modify it to suit yuor needs

    This example uses a picture box, which has the same properties as the printer object. So just change picture1 to printer.

    Code:
    Private Sub Command1_Click()
        Dim iTop As Integer
        Dim iLeft As Integer
        Dim iRight As Integer
        Dim iBottom As Integer
        Dim iCurX As Long
        Dim iCurY As Long
        
        Dim blNewLine As Boolean
        Dim ip1 As Long, ip2 As Long
        Dim stWord As String
        
        'set the correct font
        Picture1.Font = Text1.Font
        
        'set the margins
        iTop = 200
        iLeft = 200
        iBottom = Picture1.Height - 200
        iRight = Picture1.Width - 200
        
        iCurX = iLeft
        iCurY = iTop
        
        ip2 = 1
        ip1 = 1
        Do While (ip2 <> 0) And (ip2 < Len(Text1.Text))
          blNewLine = False
          'break up on spaces
          ip2 = InStr(ip1, Text1.Text, " ")
          If ip2 = 0 Then
            ip2 = Len(Text1.Text) + 1
          Else
            ip2 = ip2 + 1
          End If
          
          'test for a new line
          If InStr(ip1, Text1.Text, vbCrLf) Then
            ip2 = InStr(ip1, Text1.Text, vbCrLf)
            blNewLine = True
          End If
          
          'get the word from the string
          stWord = Mid$(Text1.Text, ip1, ip2 - ip1)
          
          'make sure the word will not go past the edge
          'of the paper
          If iCurX + TextWidth(stWord) > iRight Then
            iCurX = iLeft
            iCurY = iCurY + TextHeight(stWord)
          End If
          'make sure the word wont go over the bottom margin
          If iCurY > iBottom Then
            'newpage
            'Printer.NewPage
            'iCurX = iLeft
            'iCurY = iTop
          End If
          
          'set the current X an Y positions
          Picture1.CurrentX = iCurX
          Picture1.CurrentY = iCurY
          
          'print the word
          Picture1.Print (stWord)
          
          'increase the x pos by the length of the word
          iCurX = iCurX + TextWidth(stWord)
          
          'if a new line then move the cur Y postion
          If blNewLine = True Then
            ip2 = ip2 + 2
            iCurY = iCurY + TextHeight(stWord)
            iCurX = iLeft
          End If
          
          ip1 = ip2
          
        Loop
        
    End Sub
    Hope it works.
    Iain, thats with an i by the way!

  5. #5
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    Iain is a vb god! So if something he says is tough, it is.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2000
    Posts
    26

    Talking

    Yo, you are a god!!! Thanks for your help... I don't understand why they wouldn't make a property or something for printing page margins. What's up with that?

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The Printer object has been around for ages, and really isn't designed for anything particularly advanced. If you need anything better try ViewSoft's VSPrint control (look on ComponentSource for that).

  8. #8
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658

    More of the same.

    tsc128, and any one else who cares.

    The last post i made was just a quick attempt at printing a string to the printer with set margins. It had a few problems, like : If a word was longer than the margins then it would still go of the margigns.

    So i have wrote v1.2. This takes into acount long words, adn cuts them up. It is also a lot easier to read, as i am aware the last post did not read to well.

    Here ya go then. It will still work with picture boxes (print preview?).

    Code:
    Private Sub Command5_Click()
        'call the printing algorithim
        printerString Text1.Text
            
    End Sub
    
    
    
    Private Sub printerString(strText As String)
        Dim ipCrlf1 As Long, ipCrLf2 As Long        'line feed pointers
        Dim ipSp1 As Long, ipSp2 As Long            'space pointers
        Dim iCurX As Long, iCurY As Long            'Current X and Y positions
        
        Dim ipLeft As Integer, ipRight As Integer   'Left and Right Margins
        Dim ipTop As Integer, ipBottom As Integer   'Top and Bottom Margins
        
        Dim strWord As String, strLine As String    'Holds words and Lines
        
        Dim blLongWord As Boolean   'True if the word won't fit in the margins
        
        'set the margins for the page
        ipLeft = 1000
        ipRight = Printer.Width - 1500
        ipTop = 1000
        ipBottom = Printer.Height - 1500
        
        'set the current X and Y co-ordinates
        iCurX = ipTop
        iCurY = ipLeft
        
        'set the font to that of the text box
        Me.Font = Text1.Font
        Printer.Font = Text1.Font
        
        'initialse the line pointers
        ipCrlf1 = 1: ipCrLf2 = 1
        
        'break up text on line feeds
        Do
          ipSp1 = 1: ipSp2 = 1
          
          'find a line feed
          ipCrLf2 = InStr(ipCrlf1, strText, vbCrLf)
          If ipCrLf2 = 0 Then
            'if there isn't one then set the length to the string length
            ipCrLf2 = Len(strText) + 1
          End If
          
          'get the line of text from the string
          strLine = Mid$(strText, ipCrlf1, ipCrLf2 - ipCrlf1)
          
          Do
            'find a space
            ipSp2 = InStr(ipSp1, strLine, " ")
            If ipSp2 = 0 Then
              'if there isn't one then set the length to th string length
              ipSp2 = Len(strLine)
            End If
            
            'get the word
            strWord = Mid$(strLine, ipSp1, ipSp2 - ipSp1 + 1)
            
            blLongWord = False
            'Make sure the word will fit between the margins
            'if not we will have to chop the string
            If Printer.TextWidth(strWord) > (ipRight - ipLeft) Then
              'find out where to chop string
              ipSp2 = chopString(strWord, (ipRight - iCurX)) + ipSp1
              blLongWord = True
              'get the chopped String
              strWord = Mid$(strLine, ipSp1, ipSp2 - ipSp1 + 1)
            End If
            
            
            'make sure the word won't pass the edge of the margin
            'if it will then drop down a line
            If iCurX + Printer.TextWidth(strWord) > ipRight Then
              iCurX = ipLeft
              iCurY = iCurY + Printer.TextHeight(strWord)
            End If
            
            'make sure the word won't pass the bottom margin
            'if it does then we need a new page
            If iCurY + Printer.TextHeight(strWord) > ipBottom Then
              'new page
              Printer.NewPage
              iCurX = ipLeft
              iCurY = ipTop
            End If
          
            'set the printer current X and Y positions
            Printer.CurrentX = iCurX
            Printer.CurrentY = iCurY
            
            'print the word
            Printer.Print strWord
            
            'set the current X position
            iCurX = iCurX + Printer.TextWidth(strWord)
            If blLongWord Then
              'set iCurX to the margin
              iCurX = ipLeft
              'go down a line
              iCurY = iCurY + Printer.TextHeight(strWord)
            End If
            
            'set the pointer
            ipSp1 = ipSp2 + 1
          Loop Until ipSp2 >= Len(strLine)
          
          'set the pointer.  +2 to miss the vbCrLf characters
          ipCrlf1 = ipCrLf2 + 2
          
          'finished a line so move curX to the margin
          iCurX = ipLeft
          'and go down a line
          iCurY = iCurY + Printer.TextHeight(strLine)
        
        Loop Until ipCrLf2 >= Len(strText)
        
        
        'finished composing Document.  Tell printer to print.
        'PHEW :)
        Printer.EndDoc
    
    End Sub
    
    
    
    Private Function chopString(stWord As String, iLength As Long) As Long
        Dim i As Integer
        
        '-----------------------------------------------'
        'This function chops a long word in half until  '
        'it is small enough that it will fit into the   '
        'Page margins.  It then loops to find the exact '
        'size that will fit to the edge of the margin.  '
        '-----------------------------------------------'
        
        
        'if the text is too wide for the margin, then chop it
        If TextWidth(stWord) > (iLength) Then
          'chop it in half
          chopString = chopString(Mid$(stWord, 1, Len(stWord) \ 2), iLength)
          If chopString = -1 Then
            'means we have a string that is small enough to work with
            'loop to find the exact righ size
            For i = Len(stWord) To 1 Step -1
              If TextWidth(Mid$(stWord, 1, i)) <= iLength Then
                chopString = i - 1
                Exit For
              End If
            Next i
          End If
        Else
          'the string has been choped small enough
          'to loop through to find the size to use
          chopString = -1
        End If
        
    End Function
    Hope this is better for you.
    Iain, thats with an i by the way!

  9. #9
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658

    Question Font Styles

    I have decided to alter my printing algorithim to support diferent font styles (bold, italic, different fonts etc).

    My question is does any one know an easy way to do this?

    Or am i going to be stuck having to test each position in a text box for its font properties? As you can probably guess that would be very slow.

    Any ideas are apreciated.

    Regards,
    Iain, thats with an i by the way!

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