Simply Printing Text

Why oh Why can we not print text as text and not have to thru the rigamarole of using graphics to print some text.
Have sorted the printing using graphics but works on my printer but comes out wrong on my mates printer, I did this as a test and his printer does not recognise the spacing and puts the text bunched up in the top left corner of the page.
Just a simple print command as in the old basic would be nice.
Look like I will have find a different language to do the job for my mate.

Ian

I’ve had pretty universal success with Xojo’s graphics / printing system.

I can’t quite tell if you’re looking for help, so I’ll do one debug question:
Is your friend using something unsupported, say Windows 11?

We are both using windows 11 but different printers.
His is epsom my is canon and the only question is why force us to graphics when simple printing command would do.
I have writtten a print rountine but it seems you have to make allowances for all printers and how you do that I do not know.
To be Honest have been at it for to long and will just write it to text file and print it from a text editor.

Thanks for yoyr question.

Ian

…which will then send it as a graphic to the printer, the same as Xojo does.
Its been a long time since it was easy to chuck chr(65) down a pipe to LPT1: or CON1: and get an output that way.

Printing text isn’t massively hard, so if anyone else finds this post and finds printing text hard, there is an example project with Xojo called ‘Printing Text’

what it contains is like this (amended here to remove reliance on screen controls):

//added by me
Var  OurText as string
OurText = "whatever text you want to print. This can be long and contain endofline chars"
//added by me

Var ps As New PrinterSetup
If ps.ShowPageSetupDialog Then
  Var page As Graphics
  page = OpenPrinterDialog(ps)
  If page <> Nil Then
    ' Draw text on page 1 with 1 inch left/top margin
    Var leftMargin As Double = ps.HorizontalResolution
    Var topMargin As Double = ps.VerticalResolution

    page.FontSize = 12   // bigger = bigger letters
    
    ' To print multiple pages, you need to parse each line and print it
    ' if it will fit on the page. If it will not fit, then
    ' you call NextPage so that your text will start printing at the top
    ' of the next page.
    
    ' First, split the text into multiple paragraphs


Var paragraphs() As String =   OurText.ReplaceLineEndings(EndOfLine).Split(EndOfLine)


    Var pageTextHeight As Double = topMargin
    Var newpageTextHeight As Double
    
    For i As Integer = 0 To paragraphs.LastIndex
      newpageTextHeight = pageTextHeight + page.TextHeight(paragraphs(i), ps.Width - leftMargin * 2)
      
      If newPageTextHeight > ps.Height - topMargin * 2 Then
        ' String does not fit on page, so move to next page
        page.NextPage
        pageTextHeight = topMargin
        newpageTextHeight = pageTextHeight + page.TextHeight(paragraphs(i), ps.Width - leftMargin * 2)
      End If
      ' If string fits on page, then draw it
      page.DrawText(paragraphs(i), leftMargin, pageTextHeight, ps.Width - leftMargin * 2)
      ' Keep a cumulative count of the pageheight as strings are added to it
      pageTextHeight = newpageTextHeight ' Adjust page text height
      pageTextHeight = pageTextHeight + topMargin / 8 ' 1/8 inch between paragraphs
    Next
  End If
End If

Hi jeff thanks for the reply and the info will give it a try on my system the send it to my Mate and see what happens.
Had problems getting my head round the showpage setup and the commands you can get back so as to change the length and width, so willread the program you have supplied and will try and learn from it.

Please am getting old so brain not as useful as it used to be…( am 71)

You may have to scale the FontSize based on ps.HorizontalResolution. At 72 dpi, a font size of 12 works well. At 300 dpi, you need a font size of 50 to produce the same output.

My printer is running at 72dpi but not sure about my mates so will have to find out.

LOL. Xojo DOES NOT send it as “a graphic” Try your example printing to a PDF printer and you will see that is TEXT, not a picture.

That is NOT what Ian is talking about, on the oposite side, VB6 was not writing directly to a port, it was doing the same as Xojo BUT 2 decades ago VB6 had nother layer to simplify the printing to the user:

Printer.CurrentX= 30
Printer.CurrentY= 49
Printer.Print "Some text"
Printer.EndDoc

How is that different than

g.DrawString("some text", 30, 49)
g = nil

Try your example printing to a PDF printer and you will see that is TEXT, not a picture.

That has just added another layer to the process.
The PDF file is a set of printer control language instructions, including position , font instructions, and sometimes even embedded fonts, not just “ABC” and they still have to be drawn using graphics at the printer end sometimes.

Xojo is not a word processor.

2 Likes

Never said it was a text editor…

Hi Everybody, First off I appreciate all the comments and Thanks to Jeff Tullin for taking the time to write that code which has solved the problem I was having.
I must say that my question seems to have stirred up some different opinions which is allways a good thing.
As for PDF I am having enough problems printing text never mind getting involved with PDF stuff.
Pleasse except my thanks and keep up the good work.

Ian (Brain still not quite up to scratch…)

Now that you are able to print (to paper), the only thing you have to do to create a PDF from your print is… to select a PDF Printer.

In MacOS, look at the PDF popupmenu (bottom left, near the ? help circle). You click there and choose Open in Preview or Save to disk.

Nothing else to do.

Really simple, nothing to do with age (as I am just below yours).

Neither did Tim :wink:

Sorry you missunderstood my comment, I know the way to print too PDF what I meant was getting involved with using Xojo to create PDF reports.
I Have been using the Print to PDF to save using up my paper and ink (Am using Win 11).

Thanks for the comment anyway.

Ian

I do not tried too.

I do not have the use.