Printing tutorial
Posted
#1
(In Topic #2102)
Trainee

A detailed one please?
I think this is the right place to ask for a detailed tutorial on printing, right?I have problems with printing. I can't get passed a blank pdf and a configure window!
If anyone could help, it would be deeply apreciated!
Thanx!
Posted
Administrator



My understanding is that you want to print a pdf file and want a turorial.Berkut said
I think this is the right place to ask for a detailed tutorial on printing, right?
I have problems with printing. I can't get passed a blank pdf and a configure window!
From “Printing tutorial”, March 29th 2026, 2:56 PM
13th of April I will release Gambas Learning Guide Book 2.
In there you learn to build a PDF viewer and how to print a PDF.
It is a very detailed explanation that should help you get started.
But that will not help you right now, so I'll post some code in my next reply as somehow I can't add a Gambas code box right now
gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories
… there is always a Catch if things go wrong!
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories
… there is always a Catch if things go wrong!
Posted
Administrator

I'll move you over to the proper Gambas Programming forum.
sholzy
Gambas One Site Director
To help make creating posts and using the forum a little easier:
Tutorials - Website and forum and Playground
To report bugs in the Gambas IDE:
Official Gambas Bug Tracker
Gambas One Site Director
To help make creating posts and using the forum a little easier:
Tutorials - Website and forum and Playground
To report bugs in the Gambas IDE:
Official Gambas Bug Tracker
Posted
Enthusiast

Posted
Administrator



Code (gambas)
- $hPrinter.Print
- '--set the number of pages for the printer object--
- $hPrinter.Count = $hPdfFile.Count
- $hPrinter.Resolution = 150
- '--run once for each page
- '--render the page--
- fWidth = $hPrinter.PaperWidth / 25.4 * $hPrinter.Resolution
- fHeight = $hPrinter.PaperHeight / 25.4 * $hPrinter.Resolution
- hPage = $hPdfFile[$hPrinter.Page - 1].Render(0, 0, fWidth, fHeight, $hPdfFile.Rotation, $hPrinter.Resolution)
- '--print the page--
- Paint.DrawImage(hPage, 0, 0, Paint.W, Paint.H)
Replace "path/to/pdf/mypdf.pdf" with a path to an actual PDF file.
Add a Button named btnPrint for the printing code.
Edit: You need gb.poppler component selected for doing above with a PDF file!
gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories
… there is always a Catch if things go wrong!
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories
… there is always a Catch if things go wrong!
Posted
Trainee

Sorry for posting in the wrong place.
Actually, by saying "detailed", I meant a tutorial on printing anything (text, fonts, graphics) from within Gambas! I suppose though this must be a long one!
But a way to print .pdf is a good start! I'll wait for your tutorial eagerly! Meanwhile I'll try your code.
Much obliged.
The following is not my code. I was just trying to get it to work. (I don't even remember where I got it!)
Sorry it's not formatted. I don't know how to do it here yet!
——————————
' Gambas class file
Public Sub Form_Open()
Me.Center
End
Public Sub btnPrint_Click()
If ptrPrinter.Configure() Then Return
Me.Enabled = False
Inc Application.Busy
ptrPrinter.Print
Dec Application.Busy
Me.Enabled = True
End
Public Sub prtPrinter_Begin()
ptrPrinter.Count = 1
ptrPrinter.A4
txtArea.Text &= "Set number of pages to 1\n"
txtArea.Text &= "Begin printing\n"
End
Public Sub prtPrinter_Paginate()
txtArea.Text &= "Begin paginate\n"
ptrPrinter.FullPage = True
End
Public Sub prtPrinter_Draw()
Dim X As Float
Dim Y As Float
Dim W As Float
Dim H As Float
Dim wFactor As Float
Dim hFactor As Float
txtArea.Text &= "Begin Draw\n"
'Set scale
wFactor = Paint.Width / ptrPrinter.PaperWidth
hFactor = Paint.Height / ptrPrinter.PaperHeight
Paint.Scale(wFactor, hFactor)
'Set Color to red
Paint.Brush = Paint.Color(Color.RGB(0, 0, 0, 0))
'Set line width
Paint.LineWidth = 0.45
'Set X,Y origin to 0,0
X = 0
Y = 0
'Set Width, Height
W = ptrPrinter.PaperWidth - Paint.LineWidth
H = ptrPrinter.PaperHeight - Paint.LineWidth
'Draw a rectangle
Paint.Rectangle(X, Y, W, H)
'Write it on paper
Paint.Stroke
'Write text to paper
Paint.Font.Name = "Times New Roman"
Paint.Font.Size = 12 * (Desktop.Resolution / ptrPrinter.Resolution)
Paint.Text("Hello world!", 20, 20)
Paint.Fill
End
Public Sub prtPrinter_End()
txtArea.Text &= "End printing\n"
End
Posted
Enthusiast

It is not that much complicated, but you have to find out by using the "Comcode add tag".
Click on it and find "code" under "code tags"
There you can decide which type of code ("Gambas,…")
if you like line numbered etc.
see here:
Code (gambas)
- ' Gambas class file
- Me.Center
- ptrPrinter.Print
- ptrPrinter.Count = 1
- ptrPrinter.A4
- txtArea.Text &= "Set number of pages to 1\n"
- txtArea.Text &= "Begin printing\n"
- txtArea.Text &= "Begin paginate\n"
- txtArea.Text &= "Begin Draw\n"
- 'Set scale
- wFactor = Paint.Width / ptrPrinter.PaperWidth
- hFactor = Paint.Height / ptrPrinter.PaperHeight
- Paint.Scale(wFactor, hFactor)
- 'Set Color to red
- Paint.Brush = Paint.Color(Color.RGB(0, 0, 0, 0))
- 'Set line width
- Paint.LineWidth = 0.45
- 'Set X,Y origin to 0,0
- X = 0
- Y = 0
- 'Set Width, Height
- W = ptrPrinter.PaperWidth - Paint.LineWidth
- H = ptrPrinter.PaperHeight - Paint.LineWidth
- 'Draw a rectangle
- Paint.Rectangle(X, Y, W, H)
- 'Write it on paper
- Paint.Stroke
- 'Write text to paper
- Paint.Text("Hello world!", 20, 20)
- Paint.Fill
- txtArea.Text &= "End printing\n"
Regards,
Yogi
Posted
Guru


1 guest and 0 members have just viewed this.


