[Done] Print and print preview

Just starting out? Need help? Post your questions and find answers here.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

[Done] Print and print preview

Post 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?
Last edited by collectordave on Sat Feb 04, 2017 7:02 am, edited 4 times in total.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Print and print preview

Post 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.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Print and print preview

Post 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
Windows (x64)
Raspberry Pi OS (Arm64)
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Print and print preview

Post 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?
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Print and print preview

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
infratec
Always Here
Always Here
Posts: 6855
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Print and print preview

Post 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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Print and print preview

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
infratec
Always Here
Always Here
Posts: 6855
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Print and print preview

Post by infratec »

Ups...

you are right, my eyes were to fast. :cry:
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Print and print preview

Post by collectordave »

Just downloaded 5.4 beta

Looks very interesting will get back after a while

thanks all
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Print and print preview

Post 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
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Print and print preview

Post 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.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Print and print preview

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Print and print preview

Post 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.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Print and print preview

Post 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" .
Windows (x64)
Raspberry Pi OS (Arm64)
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Print and print preview

Post 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.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply