Platforms to show: All Mac Windows Linux Cross-Platform
/DynaPDF/Tables/DynaPDF Table from Listbox
Required plugins for this example: MBS DynaPDF Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /DynaPDF/Tables/DynaPDF Table from Listbox
This example is the version from Wed, 14th Sep 2021.
Project "DynaPDF Table from Listbox.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Const AllColumns = -1
Const AllRows = -1
Control Listbox1 Inherits Listbox
ControlInstance Listbox1 Inherits Listbox
EventHandler Sub Open()
me.ColumnAlignment(0)=me.AlignCenter
me.ColumnAlignment(2)=me.AlignRight
End EventHandler
End Control
Control BevelButton1 Inherits BevelButton
ControlInstance BevelButton1 Inherits BevelButton
EventHandler Sub Action()
for i as integer = 4 to 1000
Listbox1.AddRow str(i), "Test line", "$5"
next
run
End EventHandler
End Control
EventHandler Sub Open()
run
End EventHandler
Sub DrawListbox(pdf as dynapdfmbs, list as listbox, x as integer, y as integer, width as integer)
dim table as DynaPDFTableMBS
dim columnwidths(-1) as integer
dim rowNum as integer
dim PageWidth as integer = pdf.GetPageWidth-72-72 // width - left and right
// scale down to page width if listbox on GUI is wider
dim Factor as Double = PageWidth / list.Width
table = pdf.CreateTable(list.ListCount, list.ColumnCount, PageWidth, 8.0)
call table.SetFont(AllRows, AllColumns, "Arial", pdf.kfsRegular, true, pdf.kcpUnicode)
redim columnwidths(list.ColumnCount)
dim cw(-1) as string = split(List.ColumnWidths,",")
dim remainingspace as integer = PageWidth
dim starcount as integer
for each s as string in cw
s = s.trim
if s = "*" then
starcount = starcount + 1
elseif instr(s,"*")>0 then
starcount=starcount+val(s)
end if
next
for i as integer=0 to UBound(Cw)
dim s as string = cw(i).trim
dim columnwidth as integer
if instr(s,"%")>0 then
columnwidth=val(s)*(PageWidth)/100.0
elseif s = "*" then
columnwidth = remainingspace/starcount
elseif instr(s,"*")>0 then
columnwidth=val(s)*remainingspace/starcount
else
columnwidth=val(s)*Factor
end if
columnwidths(i)=columnwidth
remainingspace=remainingspace-columnwidth
next
dim font as string = list.TextFont
if font="System" then font="Times"
dim size as integer = list.TextSize
if size <= 0 then size = 12
call table.SetFont(AllRows, AllColumns, font, pdf.kfsRegular, true, pdf.kcpUnicode)
rowNum = table.AddRow
for cx as integer=0 to list.ColumnCount-1
call table.SetColWidth(cx, columnwidths(cx), false)
if columnwidths(cx) = 0 then
// ignore
elseif list.ColumnAlignment(cx)=list.AlignCenter then
call table.SetCellText(rowNum, cx, pdf.ktaCenter, table.kcoCenter, list.Heading(cx))
elseif list.ColumnAlignment(cx)=list.AlignRight then
call table.SetCellText(rowNum, cx, pdf.ktaRight, table.kcoCenter, list.Heading(cx))
else
call table.SetCellText(rowNum, cx, pdf.ktaLeft, table.kcoCenter, list.Heading(cx))
end if
next
call table.SetFont(rowNum, AllColumns, font, pdf.kfsBold, true, pdf.kcpUnicode)
call table.SetFlags(rowNum, AllColumns, table.ktfHeaderRow)
call table.SetBorderWidth(rowNum, AllColumns, 0.5, 0.5, 0.5, 0.5)
for cy as integer=0 to list.ListCount-1
rowNum = table.AddRow
for cx as integer=0 to list.ColumnCount-1
if columnwidths(cx) = 0 then
// ignore
elseif list.ColumnAlignment(cx)=list.AlignCenter then
call table.SetCellText(rowNum, cx, pdf.ktaCenter, table.kcoCenter, list.Cell(cy,cx) )
elseif list.ColumnAlignment(cx)=list.AlignRight then
call table.SetCellText(rowNum, cx, pdf.ktaRight, table.kcoCenter, list.Cell(cy,cx) )
else
call table.SetCellText(rowNum, cx, pdf.ktaLeft, table.kcoCenter, list.Cell(cy,cx) )
end if
next
next
call table.SetCellPadding(AllRows, AllColumns, 0.0, 0.0, 2.0, 2.0)
call table.SetCellSpacing(AllRows, AllColumns, 0.0, 0.0, 0.0, 2.0)
dim PageHeight as Double = pdf.GetPageHeight - 72 - 72
do
call table.DrawTable( 55.0, 72.0, PageHeight)
// draw footer
if table.HaveMore then
call pdf.EndPage
call pdf.Append
else
// done
exit
end if
loop
End Sub
Sub Run()
dim pdf as new MyDynapdfMBS
pdf.SetLicenseKey "Starter" // For this example you can use a Starter, Lite, Pro or Enterprise License
dim outfile as FolderItem = SpecialFolder.Desktop.Child("Table.pdf")
call pdf.CreateNewPDF(nil) // the output file is created later
call pdf.SetDocInfo pdf.kdiCreator, "Realbasic test application"
call pdf.SetDocInfo pdf.kdiTitle, "My first table output"
call pdf.SetPageCoords pdf.kpcTopDown
call pdf.Append
DrawListbox pdf, Listbox1, 50, 50, pdf.GetPageWidth-100
call pdf.EndPage
// no error?
if pdf.HaveOpenDoc then
if not pdf.OpenOutputFile(outfile) then
MsgBox "Can't write file to "+outfile.NativePath
quit
end if
end if
call pdf.CloseFile
outfile.Launch
End Sub
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MyDynaPDFMBS Inherits DynaPDFMBS
EventHandler Function Error(ErrorCode as integer, ErrorMessage as string, ErrorType as integer) As integer
// output all messages on the console:
System.DebugLog str(ErrorCode)+": "+ErrorMessage
// and display dialog:
Dim d as New MessageDialog //declare the MessageDialog object
Dim b as MessageDialogButton //for handling the result
d.icon=MessageDialog.GraphicCaution //display warning icon
d.ActionButton.Caption="Continue"
d.CancelButton.Visible=True //show the Cancel button
// a warning or an error?
if BitAnd(ErrorType, me.kE_WARNING) = me.kE_WARNING then
// if user decided to ignore, we'll ignore
if IgnoreWarnings then Return 0
d.Message="A warning occurred while processing your PDF code."
// we add a third button to display all warnings
d.AlternateActionButton.Caption = "Ignore warnings"
d.AlternateActionButton.Visible = true
else
d.Message="An error occurred while processing your PDF code."
end if
d.Explanation = str(ErrorCode)+": "+ErrorMessage
b=d.ShowModal //display the dialog
Select Case b //determine which button was pressed.
Case d.ActionButton
Return 0 // ignore
Case d.AlternateActionButton
IgnoreWarnings = true
Return 0 // ignore
Case d.CancelButton
Return -1 // stop
End select
End EventHandler
Property IgnoreWarnings As Boolean
End Class
End Project
See also:
The items on this page are in the following plugins: MBS DynaPDF Plugin.