MBS DynaPDF help needed.

Hi,
I have a 6 column ListBox (columns 0 and 6 are both hidden).
I am using DynaPDF from MBS, and have the code below - which SUCESSFULLY creates a pdf containing my ListBox content (columns 1,2,3 and 4).

[code] dim table as DynaPDFTableMBS
dim columnwidths(-1) as integer
dim rowNum as integer
dim PageWidth as integer = pdf.GetPageWidth-72-2 // width - left and right (was -72 -72)

// 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.kcp1252)

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 = 10 //was 12

call pdf.SetFont “Times”, pdf.kfsBold, 22, true, pdf.kcp1252
call pdf.SetTextRect 35,30,pdf.GetPageWidth-0,pdf.GetPageHeight-0
call pdf.WriteFText pdf.ktaJustify, "Accounts - " +PdfWindow.NameTextField.text
call table.SetFont(AllRows, AllColumns, font, pdf.kfsRegular, true, pdf.kcp1252)

rowNum = table.AddRow
for cx as integer=0 to list.ColumnCount-1
call table.SetColWidth(cx, columnwidths(cx), false)

if columnwidths(cx) = 0 then
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.kcp1252)
call table.SetFlags(rowNum, AllColumns, table.ktfHeaderRow)

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
  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.SetBorderWidth(AllRows, AllColumns, 0.0, 0.0, 0.5, 0.5)
call table.SetCellPadding(AllRows, AllColumns, 0.0, 0.0, 2.0, 2.0)
call table.SetCellSpacing(AllRows, AllColumns, 0.0, 0.0, 0.0, 15.0) // vertical space (height) between each row

dim PageHeight as Double = pdf.GetPageHeight - 72 - 72

do
call table.DrawTable( 35.0, 72.0, PageHeight) // left margin and top margin

// draw footer
if table.HaveMore then
  call pdf.EndPage
  call pdf.Append
  call pdf.SetFont "Arial", pdf.kfsBold, 22, true, pdf.kcp1252
  call pdf.SetTextRect 35,30,pdf.GetPageWidth-0,pdf.GetPageHeight-0
  // call pdf.WriteFText pdf.ktaJustify, "Accounts for " +TitleTextField.text+" Continued"
  call pdf.WriteFText pdf.ktaJustify, "Accounts - " +PdfWindow.NameTextField.text
  
else 
  // done
  exit
end if

loop[/code]

But, when I use the example code from Christians website (below), which draws a table around the data - only 2 columns are displayed (1 and 2) - columns 3 and 4 are not in the table??
I thought the example code would draw a table around any number of columns?

Could someone please look at the code below, and try to advise me?
I emailed Christian a couple of days ago, but received no reply.

Thank you all in advance.

[code]dim columnwidths(-1) as integer
dim height as integer = list.ListCount*21+22

redim columnwidths(list.ColumnCount)

call pdf.SetFillColor &hBBBBBB
call pdf.SetStrokeColor 0
call pdf.Rectangle x,y,width-1, 21, pdf.kfmFill
call pdf.Rectangle x,y,width-1,height,pdf.kfmStroke

dim cw(-1) as string = split(List.ColumnWidths,",")
dim remainingspace as integer = width-List.ColumnCount-1
dim starcount as integer

for each s as string in cw
if 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)
dim columnwidth as integer

if instr(s,"%")>0 then
  columnwidth=val(s)*(width-2.0)/100.0
elseif instr(s,"*")>0 then
  columnwidth=val(s)*remainingspace/starcount
else
  columnwidth=val(s)
end if
columnwidths(i)=columnwidth
remainingspace=remainingspace-columnwidth

next

call pdf.SetColors 0

for i as integer=1 to list.ListCount
call pdf.Moveto x,y+21i
call pdf.LineTo x+width-1,y+21
i
next

dim ex as integer=x+columnwidths(0)
for i as integer=1 to list.ColumnCount-1
call pdf.MoveTo ex,y
call pdf.LineTo ex,y+height
ex=ex+columnwidths(i)+1
next

call pdf.StrokePath

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 pdf.SetFont font,pdf.kfsNone, size, true, pdf.kcp1252

for cy as integer=0 to list.ListCount-1
ex=x+1
for cx as integer=0 to list.ColumnCount-1
if list.ColumnAlignment(cx)=list.AlignCenter then
call pdf.SetTextRect ex,y+23+cy21,columnwidths(cx),21
call pdf.WriteFText pdf.ktaCenter, list.Cell(cy,cx)
elseif list.ColumnAlignment(cx)=list.AlignRight then
call pdf.SetTextRect ex, y+23+cy
21, columnwidths(cx)-3,21
call pdf.WriteFText pdf.ktaRight, list.Cell(cy,cx)
else
call pdf.SetTextRect ex+3,y+23+cy*21, columnwidths(cx),21
call pdf.WriteFText pdf.ktaLeft, list.Cell(cy,cx)
end if
ex=ex+columnwidths(cx)+1
next
next

call pdf.setfont font, pdf.kfsBold, size, true, pdf.kcp1252

ex=x+1
for cx as integer=0 to list.ColumnCount-1
if list.ColumnAlignment(cx)=list.AlignCenter then
call pdf.SetTextRect ex,y+2,columnwidths(cx),21
call pdf.WriteFText pdf.ktaCenter, list.Heading(cx)
elseif list.ColumnAlignment(cx)=list.AlignRight then
call pdf.SetTextRect ex,y+2,columnwidths(cx)-3,21
call pdf.WriteFText pdf.ktaRight, list.Heading(cx)
else
call pdf.SetTextRect ex+3,y+2,columnwidths(cx),21
call pdf.WriteFText pdf.ktaLeft, list.Heading(cx)
end if

ex=ex+columnwidths(cx)+1

next[/code]

Well, the code may have a bug. Maybe you debug it?
Like if column widths array is calculated wrong, you may have a problem.

Also I think I have newer code using DynaPDFTableMBS class.

To make this more reusable, you might not want to query the ColumnWidths string,
but

listbox1.Column(x).WidthActual

This way you don’t have to worry about “*”, etc… and your hidden columns should be “0”

It’s way above my current knowledge level at the moment.
I will just have my ListBox printed out without a surrounding table, until I can find a more newbie friendly solution sometime in the future.

Thank you for trying to help.

What?

Simply use list.column(i).WidthActual instead of columnwidths(i)

and remove all the extra code that builds columnwidths() to begin with… All the calculations you are doing, are already done for you

I am totally lost :frowning:
There is only 1 line of code which says columnwidths(i)

So I replace

columnwidths(i)=columnwidth

with

list.column(i).WidthActual

But I have no idea which lines “build column widths() to begin with” ???

Do you mean that the following lines should be completely removed:

ex=ex+columnwidths(cx)+1 ex=ex+columnwidths(i)+1

If so, then my code would now look like this:

[code]dim columnwidths(-1) as integer
dim height as integer = list.ListCount*21+22

redim columnwidths(list.ColumnCount)

call pdf.SetFillColor &hBBBBBB
call pdf.SetStrokeColor 0
call pdf.Rectangle x,y,width-1, 21, pdf.kfmFill
call pdf.Rectangle x,y,width-1,height,pdf.kfmStroke

dim cw(-1) as string = split(List.ColumnWidths,",")
dim remainingspace as integer = width-List.ColumnCount-1
dim starcount as integer

for each s as string in cw
if 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)
dim columnwidth as integer

if instr(s,"%")>0 then
  columnwidth=val(s)*(width-2.0)/100.0
elseif instr(s,"*")>0 then
  columnwidth=val(s)*remainingspace/starcount
else
  columnwidth=val(s)
end if

list.column(i).WidthActual
remainingspace=remainingspace-columnwidth
next

call pdf.SetColors 0

for i as integer=1 to list.ListCount
call pdf.Moveto x,y+21i
call pdf.LineTo x+width-1,y+21
i
next

dim ex as integer=x+columnwidths(0)
for i as integer=1 to list.ColumnCount-1
call pdf.MoveTo ex,y
call pdf.LineTo ex,y+height
next

call pdf.StrokePath

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 pdf.SetFont font,pdf.kfsNone, size, true, pdf.kcp1252

for cy as integer=0 to list.ListCount-1
ex=x+1
for cx as integer=0 to list.ColumnCount-1
if list.ColumnAlignment(cx)=list.AlignCenter then
call pdf.SetTextRect ex,y+23+cy21,columnwidths(cx),21
call pdf.WriteFText pdf.ktaCenter, list.Cell(cy,cx)
elseif list.ColumnAlignment(cx)=list.AlignRight then
call pdf.SetTextRect ex, y+23+cy
21, columnwidths(cx)-3,21
call pdf.WriteFText pdf.ktaRight, list.Cell(cy,cx)
else
call pdf.SetTextRect ex+3,y+23+cy*21, columnwidths(cx),21
call pdf.WriteFText pdf.ktaLeft, list.Cell(cy,cx)
end if
next
next

call pdf.setfont font, pdf.kfsBold, size, true, pdf.kcp1252

ex=x+1
for cx as integer=0 to list.ColumnCount-1
if list.ColumnAlignment(cx)=list.AlignCenter then
call pdf.SetTextRect ex,y+2,columnwidths(cx),21
call pdf.WriteFText pdf.ktaCenter, list.Heading(cx)
elseif list.ColumnAlignment(cx)=list.AlignRight then
call pdf.SetTextRect ex,y+2,columnwidths(cx)-3,21
call pdf.WriteFText pdf.ktaRight, list.Heading(cx)
else
call pdf.SetTextRect ex+3,y+2,columnwidths(cx),21
call pdf.WriteFText pdf.ktaLeft, list.Heading(cx)
end if
next[/code]

Well, the problem is that PDF has other with than listbox, so we need to calculate it ourself.

If that’s not a problem, you could remove the columnwidths array and the code filling it. And than use listbox1.Column(x).WidthActual instead of columnwidths(x) later.

But in general I would recommend to move to the DynaPDFTableMBS class.

Christian,
I am using the version for Xojo 2013 r3.1

Where do I obtain this new class, and are you saying it could possibly solve my problem?

Thanks.

DynaPDFTableMBS was added in plugin version 12.4 and helps to layout tables.
We have an example for putting listbox in PDF with this class, I think.