I already have a document layout method for my log files that I’ve always had users print to PDF using the appropriate platform tools. However, with the PDF features maturing in Xojo, Should I be able to simply change my graphics object from the printer to the PDF layer?
I’ve tried using:
Dim ps As New PDFDocument
Dim g As Graphics = ps.Graphics
But the PageHeight and PageWidth parameters for a PDFDocument don’t seem to match the Height and Width properties for the printer setup.
For the most common use, any font you want to use needs to be installed in your computer (and that also includes Web Server if you intend to deploy a Web app, for example).
Setting the font to use should be done as with the Graphics class. For example, on macOS:
Var d As New PDFDocument
Var g as Graphics = d.Graphics
g.FontName = "Times"
As shown above, using the graphics object and setting the literal name to “LiberationSans” gives an error that I can’t use the graphics object outside of the paint event
None of the other operations on the graphics object cause that.
New Desktop Project
Add a PushButton to the window
Add the Pressed event to the PushButton
Add this code into the Pressed event:
Dim ps As New PDFDocument(PDFDocument.PageSizes.Letter)
Dim g As PDFGraphics
Dim PDFLogFile As FolderItem
Dim leftmargin, margin, curY As Integer
g = ps.Graphics
If g = Nil Then
Return
Else
leftmargin = 24
margin = 36
g.DrawPicture(SafeMigrateMain_MasterHeader, 0, 464)
g.FontName = "LiberationSans"
g.Bold = True
g.FontSize = 24
g.DrawString "Safe Migrate Job Log", leftmargin, 48
g.FontSize = 9
g.DrawString("Safe Migrate Job Results:", leftmargin, 2*72)
curY = 2.3*72
g.FontName = "LiberationMono"
End If
PDFLogFile = SpecialFolder.Documents.Child("Safe Migrate Logs").Child("Test PDF Save.pdf")
ps.Save(PDFLogFile)
ps = Nil
Just create a xojo_image to replace my image in the DrawPicture call.
I changed a couple of things: mostly DrawString to the API 2.0 DrawText (this was the one raising the exception), and also the name for the fonts.
It is working just fine here (Lnx Mint)
Dim ps As New PDFDocument(PDFDocument.PageSizes.Letter)
Dim g As PDFGraphics
Dim PDFLogFile As FolderItem
Dim leftmargin, margin, curY As Integer
g = ps.Graphics
If g = Nil Then
Return
Else
leftmargin = 24
margin = 36
g.DrawPicture(SafeMigrateMain_MasterHeader, 0, 464)
g.FontName = "Liberation Sans"
g.Bold = True
g.FontSize = 24
g.DrawText "Safe Migrate Job Log", leftmargin, 48
g.FontSize = 9
g.DrawText("Safe Migrate Job Results:", leftmargin, 2*72)
curY = 2.3*72
g.FontName = "Liberation Mono"
End If
PDFLogFile = SpecialFolder.Documents.Child("Safe Migrate Logs").Child("Test PDF Save.pdf")
ps.Save(PDFLogFile)
ps = Nil