Page 1 of 2
[Done] Print and print preview
Posted: Wed Sep 02, 2015 11:20 am
by collectordave
Managed to write a Print and Preview module available here
http://www.purebasic.fr/english/viewtop ... 12&t=65192
So this thread at least for me is no longer open thanks for all the help.
New to PB so please forgive me if this has been done to death.
I am looking for a print preview and print routines for a cross platform solution windows/mac. (not web page printing)
Searched forums and there seems to be no definitive answer. (if you know of one please post here)
Looking to write my own in PB. (lots of luck and help needed)
To help with this I need functions for the following
get dpi of the screen(in mm if poss) Not needed anymore
get dpi of the selected printer(in mm if poss) Not needed anymore
select printer and paper size Have both for MAC(see below) only printer list for Windows
Procedure to print contents of imagegadget Not needed anymore
Can anyone help?
Re: Print and print preview
Posted: Wed Sep 02, 2015 3:12 pm
by collectordave
Ok more searching later.
i can get current screen resolution from the desktop functions etc. No way of knowing what size monitor that desktop is running on so will have to live with that.
Also found that you can get printer width and height in pixels.
Still cannot find what size paper has been selected for the printer. Print requester is good but i can see no way of capturing the paper size selected for the printer.
Any help appreciated.
Re: Print and print preview
Posted: Wed Sep 02, 2015 3:56 pm
by wilbert
If you want high quality printer output, best you can do is probably start experimenting with the PB 5.40 beta version.
I know it's a beta and that might not be ideal to start with but without the new VectorDrawing library it's hard to get high quality output on both Windows and Mac.
Here's a 5.40b1 example that would be very hard to do with the current stable release.
Code: Select all
; PB 5.40b1
LoadFont(0, "Impact", 20, #PB_Font_Bold)
If PrintRequester()
If StartPrinting("PureBasic Test")
If StartVectorDrawing(PrinterVectorOutput())
cx.d = VectorOutputWidth() / 2
cy.d = VectorOutputHeight() / 2
VectorFont(FontID(0), 25)
VectorSourceColor(RGBA(0, 0, 0, 80))
Text$ = "The quick brown fox jumped over the lazy doc"
For i = 1 To 6
MovePathCursor(cx - VectorTextWidth(Text$) / 2, cy - VectorTextHeight(Text$) / 2)
DrawVectorText(Text$)
RotateCoordinates(cx, cy, 30)
Next i
StopVectorDrawing()
EndIf
StopPrinting()
EndIf
EndIf
Re: Print and print preview
Posted: Wed Sep 02, 2015 5:01 pm
by collectordave
Thanks for the reply,
Does the PB 5.40 beta version have any extra printer functions such as returning the paper size selected or allowing landscape orientation?
Re: Print and print preview
Posted: Wed Sep 02, 2015 5:19 pm
by wilbert
collectordave wrote:Does the PB 5.40 beta version have any extra printer functions such as returning the paper size selected or allowing landscape orientation?
No, not those features exactly but it does make things easier.
The PrinterVectorOutput() command to start vector output to the printer allows you to choose if you want to use pixel, point, inch or millimetre.
Combined with VectorOutputWidth() and VectorOutputHeight() you can get the dimensions.
With RotateCoordinates() you can rotate the entire coordinate space which would give you an easy way to sort of mimic landscape orientation.
Re: Print and print preview
Posted: Wed Sep 02, 2015 5:21 pm
by infratec
Best for printing in PB:
PrinterLib 1.10
But I only found 1.07 which is for PB 4.xx.
There is a version 1.10 for PB5.2 and higher.
If you can not find it, send me a PM.
Bernd
Re: Print and print preview
Posted: Wed Sep 02, 2015 5:51 pm
by wilbert
infratec wrote:Best for printing in PB:
PrinterLib 1.10
But I only found 1.07 which is for PB 4.xx.
There is a version 1.10 for PB5.2 and higher.
If you can not find it, send me a PM.
Bernd
The question in the first post specifically asks for a Windows/Mac cross platform solution.
I have the impression this lib isn't cross platform.
Re: Print and print preview
Posted: Wed Sep 02, 2015 6:44 pm
by infratec
Ups...
you are right, my eyes were to fast.

Re: Print and print preview
Posted: Wed Sep 02, 2015 8:44 pm
by collectordave
Just downloaded 5.4 beta
Looks very interesting will get back after a while
thanks all
Re: Print and print preview
Posted: Thu Sep 03, 2015 5:41 am
by collectordave
Alittle digging into 5.4 Beta has given me this. I think it is just what I was looking for.
Code: Select all
pb 5.4 Beta
If PrintRequester()
If StartVectorDrawing(PrinterVectorOutput(#PB_Unit_Millimeter))
Debug "Printer Page Height = " + VectorOutputHeight() + " mm"
Debug "Printer Page Width = " + VectorOutputWidth() + " mm"
Debug "Printer Horizontal resolution = " + VectorResolutionX() + " dpi"
Debug "Printer Vertical resolution = " + VectorResolutionY() + " dpi"
StopVectorDrawing()
EndIf
EndIf
CreateImage(20, 500, 500)
If StartVectorDrawing(ImageVectorOutput(20,#PB_Unit_Millimeter))
Debug "Image Height = " + VectorOutputHeight() + " mm"
Debug "Image Width = " +VectorOutputWidth()+ " mm"
Debug "Screen Horizontal resolution = " + VectorResolutionX() + " dpi"
Debug "Screen Vertical resolution = " + VectorResolutionY() + " dpi"
StopVectorDrawing()
FreeImage(20)
EndIf
Hope this helps anyone else looking for these things without using the windows API
Re: Print and print preview
Posted: Fri Sep 04, 2015 7:03 am
by collectordave
Just a quick update.
I now have a routine to provide Print Preview and printing, very rough but seems to work. No API calls at all just PB commands so should be Ok. I have print line,print box and print text routines, working on print image. All routines are using the vector drawing functions so PB 5.4 is required.
Will be testing on MAC OS soon if it works on the MAC I will post the code here.
Re: Print and print preview
Posted: Fri Sep 04, 2015 7:10 am
by wilbert
collectordave wrote:Just a quick update.
I now have a routine to provide Print Preview and printing, very rough but seems to work. No API calls at all just PB commands so should be Ok. I have print line,print box and print text routines, working on print image. All routines are using the vector drawing functions so PB 5.4 is required.
Will be testing on MAC OS soon if it works on the MAC I will post the code here.
Sounds great
As for OSX, don't forget StartPrinting() / StopPrinting() .
The code example you posted before showing the dpi, doesn't work on OSX without those two commands.
Re: Print and print preview
Posted: Fri Sep 04, 2015 3:51 pm
by collectordave
Ok
It works on MAC OSX as well! When i add the startprint and stop print bits.(Thanks for that). Just working on actual print routines now.
However with the startstop printing, if the printer is on then it ejects a page even though there is no data sent to the printer i am just gathering info for scaling etc so no drawing takes place.
Is there a way of getting paper sizes supported by the connected printers without start and stop print as then the user could select a target paper size and the preview could work independently from the printer?
Or can the Stopprint routine check whether there is data to send to the printer before closing so if no data then just close and send nothing to the printer?
Ideal would be an enumerated list of connected printers plus page sizes supported by them. A dialogue could then be constructed to allow the user to select printer and page size.
The alternative would be to have a list of all page sizes (or just the common ones) documents etc can then be built and previewed without a printer connection being live.
Some thoughts on this would be appreciated.
Re: Print and print preview
Posted: Sat Sep 05, 2015 7:58 am
by wilbert
collectordave wrote:Ideal would be an enumerated list of connected printers plus page sizes supported by them. A dialogue could then be constructed to allow the user to select printer and page size.
The alternative would be to have a list of all page sizes (or just the common ones) documents etc can then be built and previewed without a printer connection being live.
Unfortunately at the moment such an enumerated list is only possible through api calls.
If you don't want to use api calls, the alternative approach you mentioned might be best although I'm not sure if the rendering to printer and image would be exactly identical if they are not the same size.
On OSX you can preview from the printer dialog. There's a combobox on the bottom left where you can select "Open PDF in Preview" .
Re: Print and print preview
Posted: Sat Sep 05, 2015 8:26 pm
by collectordave
Thanks for the reply
The list of printers will have to wait then. Must put in a feature request. Left printer selection in but disabled.
In the mean time I now have a page setup dialog to allow a user to select paper size (Not printer) and set margins and orientation. I am linking this to the preview form and writing a small app to demonstrate. This will allow page sizes etc to be available to programmers throughout their programme. Doing it this way as some applications need to setup each page differently like the one I am trying to move over to PB which needs to print portrait and landscape in the same document.
Just a thought if the programmer/user select a page size then select a printer when actually printing if page sizes do not match then could raise an error and abort printing. This would avoid the problem of the printer spitting out a page when gathering info.
The question about print quality to image quality is mute as the same commands used to show the preview will be used to print on the printer. The image needs to be scaled down quite a bit. I did play with full sized images just resized for the preview but with a high resolution printer the image resizing etc becomes quite noticibly slow. Settled for producing a scaled image of the page to be printed to show in preview.
Tidying up the code a little, as i hope to post the basic code here sometime over the weekend. Just working on the printer commands etc now which may take a little time.
In the mean time if anyone can produce a little snippet with a list of paper names and sizes in mm loaded into a combobox that would be great, I am using just A4 and Letter at the moment.