PDFDocument
From Xojo Documentation
New in 2020r1
An object containing graphics and/or text that can be saved to a file.
Properties | |||||||||||||
|
Methods | ||||||||||
|
Enumerations | |||
|
Shared Methods | |
|
Constructors | |||
|
Notes
PDFDocument supports only .ttf and .otf font files.
Sample Code
This examples creates a PDFDocument object then draws a bar chart into and saves it to a file on the desktop:
Var g As Graphics = pdf.Graphics
g.PenSize = 1
g.DrawLine(0, 0, 0, 324)
g.DrawLine(0, 324, g.Width, 324)
Var r As New Random
Var startX As Integer = 1
Var colors() As Color
Var bars() As Integer
If colors.LastRowIndex = -1 Then
For n As Integer = 0 To 5
colors.AddRow(RGB(r.InRange(0, 255), r.InRange(0, 255), r.InRange(0, 255)))
bars.AddRow(r.InRange(325 * 0.1, 325 - 325 * 0.2))
Next
End If
Var barWidth As Integer = g.Width / 6 * 0.8
Var spaceBetween As Integer = g.Width / 6 * 0.2
For n As Integer = 0 To 5
g.DrawingColor = colors(n)
g.FillRectangle(startX, 325 - bars(n), barWidth, bars(n) - 1)
startX = startX + barWidth + spaceBetween
Next
Var f As FolderItem = SpecialFolder.Desktop.Child("Barchart.pdf")
pdf.Save(f)
Notes
Drawing
Every new object is drawn above the previous one in the same way as the Graphics class does in all other cases. The difference is that PDFDocument does not support transparency. As such you will want to consider the order in which you do your drawing to avoid a new layer overlapping a previous one, especially when drawing Pictures (even if the Picture has an alpha channel).
Meta Data
The Author, Keywords, Subject and Title are metadata values that can be used by search utilities to find the document.
JSON Format
PDFDocuments can be created by passing JSON data to the Constructor (see Constructors above). The JSON format required is as follows:
{ "General": { "Author": String, "Creator": String, "Keywords": String, "Subject": String, "Title": String }, "Document": { "Compression": Boolean, "EmbeddedFonts": Boolean, "Landscape": Boolean, "Width": Double, "Height": Double }, "Actions": { Dictionary { Dictionary } } "Images": { Dictionary { Dictionary } }
Actions
Actions is a Dictionary of dictionaries, each containing a single graphics command and parameters you wish to invoke. For example:
"Actions": { "0": { "Color": "&h00FFFFFF" }, "1": { "FillRectangle": ".00,.00,612.00,792.00" }, "2": { "FontName": "Helvetica" }, "3": { "DrawText": "Sample text,_ENDTEXT_,100,200,200,False" } }
Images
Images is a Dictionary of dictionaries, each containing a single Base64-encoded image from the document (when generated but the Template method) regardless of how many times that image appears in the document. For example:
"Images":{ "0": {"0":"\/9j\/4AAQSkZJRgABAQAASABIAAD………"}, "1": {"1":"\/9j\/4AAQSkZJRgABAQAASABIAAD………"}, "2": {"2":"\/9j\/4AAQSkZJRgABAQAASABIAAD………"} }
Note that if you are planning to create this manually, the inner key must match the outer key.
Drawing Text
The _ENDTEXT_ marker delimits the end of the text to be drawn from the rest of the parameters. This simplifies the JSON structure when created manually.
You can also save your PDFDocument JSON data for use later as a template, by calling the Template method.
See Also
DesktopCanvas control; PrinterSetup class