Hi, I haven’t found a clear enough answer from previous posts. I’m new to Xojo and could use some guidance on this item: I need to pre-set Page Setup options so the end user never has to input the right settings.
I have a simple desktop app that prints one-off bar code labels to a desktop Dymo LabelWriter printer. Using some of the tutorial code and minor edits of my own, the code below works to print my labels perfectly.
However, each time the user clicks the Print button, they have to change the settings for:
Format For [defaults to Any Printer]
Paper Size [defaults to US Letter]
Orientation [defaults to Portrait]
Anyone working with label printers knows how finnicky they are. I need to change each of these to the precise settings, in which case printing is perfect.
I want to make those setting changes in my app so the end user never has to do this. Can anyone point me to the right example code or provide some tips?
Thanks!
Var g As Graphics
Var p As PrinterSetup
p = New PrinterSetup
If p.ShowPageSetupDialog Then
g = p.ShowPrinterDialog
If g <> Nil Then
Var hDPI As Double = p.HorizontalResolution
Var vDPI As Double = p.VerticalResolution
Var pageWidth As Double = 3.5
Var pageHeight As Double = 1.1
g.FontName = "IDAutomationHC39M Free Version"
g.FontSize = 12
// Width and height of label in inches
Var labelWidth As Double = 3.5
Var labelHeight As Double = 1.1
// Width and height of label in page points
Var labelPageWidth As Double = hDPI * labelWidth
Var labelPageHeight As Double = vDPI * labelHeight
// Print bar code label
g.DrawText(TextArea_BarCode.text,15,50)
End If
End If
Have-you read: PrinterSetup, especially the part where you can save the settings?
Also:
If you select your code and click in the </> icon, it will appears as:
Var g As Graphics
Var p As PrinterSetup
p = New PrinterSetup
If p.ShowPageSetupDialog Then
g = p.ShowPrinterDialog
If g <> Nil Then
Var hDPI As Double = p.HorizontalResolution
Var vDPI As Double = p.VerticalResolution
Var pageWidth As Double = 3.5
Var pageHeight As Double = 1.1
g.FontName = "IDAutomationHC39M Free Version"
g.FontSize = 12
// Width and height of label in inches
Var labelWidth As Double = 3.5
Var labelHeight As Double = 1.1
// Width and height of label in page points
Var labelPageWidth As Double = hDPI * labelWidth
Var labelPageHeight As Double = vDPI * labelHeight
// Print bar code label
g.DrawText(TextArea_BarCode.text,15,50)
I’ve been through that and it has helped advance me slightly…
I added a “Print Settings Config” button for now – once I learn how to code menu items I’ll move this to a Printer Configuration menu item. The code is as follows:
Var PageSetupSettings As String
Var pageSetup As New PrinterSetup
If pageSetup.ShowPageSetupDialog Then
PageSetupSettings = pageSetup.Settings
End If
But I’m having trouble figuring out how to pass this info along into my print code which lives underneath my Print button (same code as my initial post, just here for easy ref).
Any help where/how to insert appropriate code below would be extremely useful, both for making my app work as well as my continued learning. Thanks!
Var g As Graphics
Var p As PrinterSetup
p = New PrinterSetup
If p.ShowPageSetupDialog Then
g = p.ShowPrinterDialog
If g <> Nil Then
Var hDPI As Double = p.HorizontalResolution
Var vDPI As Double = p.VerticalResolution
Var pageWidth As Double = 3.5
Var pageHeight As Double = 1.1
g.FontName = "IDAutomationHC39M Free Version"
g.FontSize = 12
// Width and height of label in inches
Var labelWidth As Double = 3.5
Var labelHeight As Double = 1.1
// Width and height of label in page points
Var labelPageWidth As Double = hDPI * labelWidth
Var labelPageHeight As Double = vDPI * labelHeight
// Print bar code label
g.DrawText(TextArea_BarCode.text,15,50)
End If
End If
The idea is to let the user choose once its target printer/set the default properties, then save the settings to disk (at the appropriate location) and load it silently at application open time.
AND: do not forget to add a button if that can be needed in the future (printer change for example).
You need to store the PrinterSetup string and use it later when creating subsequent prints.
var ps as new PrinterSetup
if msPrinterSettings = "" then
// No settings stored,
// Show modal dialog and await boolean response
// True = clicked OK
// False = clicked cancel
if not ps.ShowPageSetupDialog then
return
end
// Store the print settings
msPrinterSettings = ps.Settings
else
// Load the settings instead of ask the user
ps.Settings = msPrinterSettings
end
var g as Graphics = PrinterSetup.OpenPrinter(ps)
The PrinterSetup string is sensitive data that will crash your app if you load in a malformed string. Do not try to manipulate the string data, only save and load it.
And if you’re going to save it to disk in text format for later retrieval then it’s a good idea to Base64 encode/decode it, don’t try to save and restore the raw string.
I’ve tried something as simple as a new application with two buttons – one to set the page settings, and one to bring up the print dialog box and hopefully see that it received the settings.
Under the first button named ButtonPageSetup, I put this sample code:
Var s As String
Var pageSetup As PrinterSetup
pageSetup = New PrinterSetup
If pageSetup.ShowPageSetupDialog Then
s = pageSetup.Settings
End If
under my second button named ButtonPrint, I put this sample code:
Var pageSetup As PrinterSetup
pageSetup = New PrinterSetup
pageSetup.Settings = s
If pageSetup.ShowPageSetupDialog Then
s = pageSetup.Settings
End If
First error I get is that s is undefined… which makes sense, but no matter what I try I can’t figure out how to actually pass the saved value along. I’m sure it’s something basic I just haven’t learned yet. Any thoughts?
Here’s some food. The code below works, but I am not happy with the design: the user set the Printer settings once and cannot modify it (unless he delete the binary file).
So, it is up to you to adapt it to your needs.
// ********** ********** ********** ********** ********** ********** ********** **********
// Imprime la page
//
// Emile Schwarz
// [2024-08-17] Début du codage
//
// ********** ********** ********** ********** ********** ********** ********** **********
Dim ps As New PrinterSetup
Dim Settings_Data As String = "Settings.bin"
Dim Settings_FI As FolderItem
Dim Settings_BS As BinaryStream
Dim g As Graphics
// Change the Window Name
Dim wName As String = Self.Title
// To get a meaningful title when saved as pdf (I do not print to paper at develop time, until pdf is OK)
Self.Title = wName + " - " + TA_Nom.Text + " " + TF_Prenom.Text
// 1. Check for the presence of a Settings file
Settings_FI = App.spf_Master.Child(Settings_Data) // Reference to the “Secours Populaire” folder —> to be adapted to your need
If Settings_FI = Nil Or Not Settings_FI.Exists Then
// The file does not exists (first run)
If ps.ShowPageSetupDialog Then
g = ps.ShowPrinterDialog
If g <> Nil Then
// Save the settings (for future use
// Get a Reference for the file
Settings_FI = App.spf_Master.Child(Settings_Data)
// Get a Write Reference
Settings_BS = BinaryStream.Create(Settings_FI, True) // Overwrite any previous file
// Write the settings
Settings_BS.Write ps.SetupString
// Close the Writereference
Settings_BS.Close
End If
End If
Else
// A file named "Settings.bin" already exists, so load it
Settings_BS = BinaryStream.Open(Settings_FI, False)
// Read the file contents all at once…
ps.SetupString = Settings_BS.Read(Settings_FI.Length)
Settings_BS.Close
// Get a Graphics Reference
g = PrinterSetup.OpenPrinter(ps)
End If
// Test: print the window as is…
' Window.DrawInto(g as Graphics, x as Integer, y as Integer)
Self.DrawInto(g, 10, 10)
// Set back the window original name
Self.Title = wName