Printing tutorial

Post

Posted
Rating:
#1 (In Topic #2102)
Avatar
Trainee
Berkut is in the usergroup ‘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!
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’
gbWilly is in the usergroup ‘Blogger’
gbWilly is in the usergroup ‘GambOS Developer’

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!
My understanding is that you want to print a pdf file and want a turorial.
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 O_o


 

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!
Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by gbWilly)
#3
Avatar
Administrator
sholzy is in the usergroup ‘unknown’
This forum is for tutorials for the website and forum itself.

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
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
show us your code to get the blank PDF … then we can help you with corrections .

Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by sholzyLiked by Yogi)
#5
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’
gbWilly is in the usergroup ‘Blogger’
gbWilly is in the usergroup ‘GambOS Developer’
Ok, now I can write a code example. Seems being in the wrong part of the forum prevented that.

Code (gambas)

  1. Private $hPrinter As Printer
  2. Private $hPdfFile As PdfDocument
  3.  
  4. Public Sub Form_Open()
  5.  
  6.   $hPrinter = New Printer As "MyPrinter"
  7.   $hPdfFile = New PdfDocument("/path/to/pdf/myfile.pdf") As "MyPdfFile"
  8.  
  9.  
  10. Public Sub btnPrint_Click()
  11.  
  12.   If Not $hPrinter.Configure() Then
  13.     $hPrinter.Print
  14.  
  15.  
  16. Public Sub MyPrinter_Begin()
  17.  
  18.  
  19.   '--set the number of pages for the printer object--
  20.   $hPrinter.Count = $hPdfFile.Count
  21.   $hPrinter.FullPage = True
  22.   $hPrinter.Resolution = 150
  23.  
  24.  
  25. '--run once for each page
  26. Public Sub MyPrinter_Draw()
  27.  
  28.   Dim hPage As Image
  29.   Dim fWidth, fHeight As Float
  30.  
  31.   '--render the page--
  32.   fWidth = $hPrinter.PaperWidth / 25.4 * $hPrinter.Resolution
  33.   fHeight = $hPrinter.PaperHeight / 25.4 * $hPrinter.Resolution
  34.   hPage = $hPdfFile[$hPrinter.Page - 1].Render(0, 0, fWidth, fHeight, $hPdfFile.Rotation, $hPrinter.Resolution)
  35.  
  36.   '--print the page--
  37.   Paint.DrawImage(hPage, 0, 0, Paint.W, Paint.H)
  38.  
  39.  
  40. Public Sub MyPrinter_End()
  41.  
  42.  
  43.  
  44.  

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!
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Trainee
Berkut is in the usergroup ‘Trainee’
Thank you all!
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

 
Online now: No Back to the top

Post

Posted
Rating:
Item has a rating of 5 (Liked by gbWilly)
#7
Avatar
Enthusiast
Yogi is in the usergroup ‘Enthusiast’
Hi,

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)

  1.  ' Gambas class file
  2.  
  3. Public Sub Form_Open()
  4.  
  5.     Me.Center
  6.  
  7.  
  8. Public Sub btnPrint_Click()
  9.  
  10.     If ptrPrinter.Configure() Then Return
  11.     Me.Enabled = False
  12.     Inc Application.Busy
  13.     ptrPrinter.Print
  14.     Dec Application.Busy
  15.     Me.Enabled = True
  16.  
  17.  
  18. Public Sub prtPrinter_Begin()
  19.  
  20.     ptrPrinter.Count = 1
  21.     ptrPrinter.A4
  22.     txtArea.Text &= "Set number of pages to 1\n"
  23.     txtArea.Text &= "Begin printing\n"
  24.  
  25.  
  26. Public Sub prtPrinter_Paginate()
  27.  
  28.     txtArea.Text &= "Begin paginate\n"
  29.     ptrPrinter.FullPage = True
  30.  
  31.  
  32. Public Sub prtPrinter_Draw()
  33.  
  34.     Dim X As Float
  35.     Dim Y As Float
  36.     Dim W As Float
  37.     Dim H As Float
  38.     Dim wFactor As Float
  39.     Dim hFactor As Float
  40.  
  41.     txtArea.Text &= "Begin Draw\n"
  42.     'Set scale
  43.     wFactor = Paint.Width / ptrPrinter.PaperWidth
  44.     hFactor = Paint.Height / ptrPrinter.PaperHeight
  45.     Paint.Scale(wFactor, hFactor)
  46.     'Set Color to red
  47.     Paint.Brush = Paint.Color(Color.RGB(0, 0, 0, 0))
  48.     'Set line width
  49.     Paint.LineWidth = 0.45
  50.     'Set X,Y origin to 0,0
  51.     X = 0
  52.     Y = 0
  53.  
  54.     'Set Width, Height
  55.     W = ptrPrinter.PaperWidth - Paint.LineWidth
  56.     H = ptrPrinter.PaperHeight - Paint.LineWidth
  57.     'Draw a rectangle
  58.     Paint.Rectangle(X, Y, W, H)
  59.     'Write it on paper
  60.     Paint.Stroke
  61.  
  62.     'Write text to paper
  63.     Paint.Font.Name = "Times New Roman"
  64.     Paint.Font.Size = 12 * (Desktop.Resolution / ptrPrinter.Resolution)
  65.     Paint.Text("Hello world!", 20, 20)
  66.     Paint.Fill
  67.  
  68.  
  69. Public Sub prtPrinter_End()
  70.  
  71.     txtArea.Text &= "End printing\n"
  72.  

Regards,
Yogi
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Guru
cogier is in the usergroup ‘Guru’
cogier is in the usergroup ‘GambOS Contributor’
There are 2 example programs on the Gambas Farm that might help, one written by me! :cool:
Online now: No Back to the top
1 guest and 0 members have just viewed this.