printing from textbox

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 1310 Views - Last Post: 04 February 2012 - 09:17 AM Rate Topic: -----

Topic Sponsor:

#31 Neku  Icon User is online

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 174
  • Joined: 21-May 09

Re: printing from textbox

Posted 30 January 2012 - 07:42 AM

i tried and it did not work well >.<
Was This Post Helpful? 0
  • +
  • -

#32 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 211
  • View blog
  • Posts: 1,249
  • Joined: 26-March 09

Re: printing from textbox

Posted 30 January 2012 - 08:07 AM

View PostNeku, on 30 January 2012 - 02:42 PM, said:

i tried and it did not work well >.<


In what way did it not work well?

I modified your code to fix the issue you said you had with uneven numbers of letters and short lines?

If you can tell me why it didn't work for you, then I'll have another look.
Was This Post Helpful? 0
  • +
  • -

#33 Neku  Icon User is online

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 174
  • Joined: 21-May 09

Re: printing from textbox

Posted 30 January 2012 - 09:33 AM

at times either the text go off page (happeneds with the code i wrote too) or it cut few letters from some words and sometimes in print only the last line in my text but each letter in different line
Was This Post Helpful? 0
  • +
  • -

#34 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 211
  • View blog
  • Posts: 1,249
  • Joined: 26-March 09

Re: printing from textbox

Posted 30 January 2012 - 10:51 AM

OK, you describing different problems there.

The code I wrote was to split your text into the array lines, based on a constant of how wide you wanted the line. That code did work and split according to your requirements.

What you're describing there sounds like a slightly different problem.

One question...

When you have the text going off the page, did you check the array lines held the data you expected (i.e. if each was to be 20 character, where they 20 characters). In other words, is the splitting function working correctly?

A couple of things to think about.

1. Are you using TrueType fonts or Fixed Fonts? If you're using true type fonts, then I would expect that this could happen because of the way the fonts are spaced, so certain letters are "wider" than others. That being the case, your splitting of the lines, won't work as you expect. You could try it with a fixed font (Courier for example) and see if that makes a difference (which I think it would), but that might not be what you want.

I'm sure I've seen examples of printing text accurately using VB6, so I'll have a dig around and see if anything comes up.


[EDIT] I've found a pretty good tutorial on printing in VB here. It also links to an option for printing from a RichTextBox, so that it handles the wordwrapping automatically for you including margins. The direct link for that code is here.)

This post has been edited by maj3091: 30 January 2012 - 11:09 AM

Was This Post Helpful? 0
  • +
  • -

#35 Neku  Icon User is online

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 174
  • Joined: 21-May 09

Re: printing from textbox

Posted 31 January 2012 - 01:18 PM

already know this tutorial and it didnt helped much.. but also i didnt check the fonttype >.< i just left it as it is... so i guess it was set to the type textboxes come with
Was This Post Helpful? 0
  • +
  • -

#36 Neku  Icon User is online

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 174
  • Joined: 21-May 09

Re: printing from textbox

Posted 01 February 2012 - 03:44 PM

so these should be my targets right?
-i should set the font type to courier so all letters will be the same size and then splitting them by a fixed size i want.
-and then i need a code that prevent from splitting in the middle of a word.
-also a counter to know how many lines fit inside a page and start a new page if needed.

now how i can print a line in about 0.5cm from the page beggining and 0.5cm from page end?
and if needed how i can start printing from the right side of the page?
Was This Post Helpful? 0
  • +
  • -

#37 deanobravo  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 99
  • Joined: 02-January 12

Re: printing from textbox

Posted 01 February 2012 - 04:13 PM

By using Printer.CurrentX and Printer.CurrentY you can position your text anywhere on the page

I still think my original code for breaking up lines of text would work best, it just needs some tweaking. Then you wouldn't need to worry about your font because textwidth would take care of the line length.
Was This Post Helpful? 0
  • +
  • -

#38 Neku  Icon User is online

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 174
  • Joined: 21-May 09

Re: printing from textbox

Posted 03 February 2012 - 06:08 AM

so after i set the scale mode lets say to cm then if CorrectX = 1 then it mean 1 cm?

also abit of help with the tweaking to the splitting code >.<
in the end i'd like it to be on dll file to make things easier

This post has been edited by Neku: 03 February 2012 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

#39 deanobravo  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 99
  • Joined: 02-January 12

Re: printing from textbox

Posted 03 February 2012 - 04:38 PM

Here's a bit of code I knocked up this morning.
Essentially it churns through the text and prints each line as it goes.
It also takes into account any carriage returns and line feeds.

Public Sub Print_Text()

Dim lineToPrint As String
Dim nextWord As String
Dim textRemaining As String
Dim aschar As Integer
Dim countChars As Integer
Dim widthText As Single

Printer.ScaleMode = 6   'set scalemode to mm
Printer.CurrentY = 20   'start printing 20mm from top of page

textRemaining = Text1.text  'get text to print from text box

Do

  Do
     countChars = countChars + 1
     
     If countChars > Len(textRemaining) Then Exit Do  'reached end of text
     
     aschar = Asc(Mid$(textRemaining, countChars, 1))
   
     Select Case aschar   'decide what to do depending on ascii value of character
     
         Case 13
           lineToPrint = lineToPrint & nextWord
   
           textRemaining = Replace(textRemaining, nextWord & Chr(13) & Chr(10), "", 1, 1)

           Printer.CurrentX = 15
           Printer.Print lineToPrint
           lineToPrint = ""
           nextWord = ""
           countChars = 0


           If textRemaining = "" Then
                Printer.CurrentX = 15
                Printer.Print lineToPrint
                Printer.EndDoc
              Exit Sub
           End If
                      
           Exit Do
           
         Case 32
           nextWord = nextWord & Mid$(textRemaining, countChars, 1)
           Exit Do
                      
         Case Else
   
           nextWord = nextWord & Mid$(textRemaining, countChars, 1)
     End Select
   
  Loop
  

  widthText = Printer.TextWidth(lineToPrint & nextWord)

  If widthText <> 0 Then
  
      If widthText < 150 Then       'add word to linetoprint if it will be less that 150mm wide
    
         lineToPrint = lineToPrint & nextWord
       
         textRemaining = Replace(textRemaining, nextWord, "", 1, 1)
    
          If textRemaining = "" Then
             Printer.CurrentX = 15
             Printer.Print lineToPrint
             Exit Do
          End If
         nextWord = ""
         countChars = 0
       Else
       
         Printer.CurrentX = 15
         Printer.Print lineToPrint
         lineToPrint = nextWord
         textRemaining = Replace(textRemaining, nextWord, "", 1, 1)
         
         nextWord = ""
         countChars = 0
       End If
  End If

Loop

Printer.EndDoc

End Sub



Was This Post Helpful? 1
  • +
  • -

#40 Neku  Icon User is online

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 174
  • Joined: 21-May 09

Re: printing from textbox

Posted 04 February 2012 - 09:17 AM

wow this code work perfectly!
via cutePDF it seems to handle extra pages too, does it do that also if i print to normal page? i did not find any line that start new page.
you defently gonna get credits for this! :D
all this site actually ^_^ you guys rock! :)
now all is left is to make the title in the middle of the line xD
i guess i can do that :)

solved title problem xD

it does also handle extra pages! yay :D
great job! thank you very much! :)

This post has been edited by Neku: 04 February 2012 - 09:36 AM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3