Right alignment of text in PDFDocument does not align

HI all, as a printing solution for Linux, I create a preview option where the user an create a PDF of their report and then print from the PDF viewer. The source of the data is a listbox. I’ve run into a problem with right aligned text. It doesn’t align in a columnar style report. The same code works perfect when sending directly to a printer. I’m running Manjaro Linux but the error occurs in windows 11 as well. Just not as drastic. My Xojo release is 2022 v2 64 bit.

Here are the pertinent parts of code with comments:

Var dW as Integer  //passed value depending on letter or legal selected
Var dH Integer  //passed value depending on letter or legal selected
Var d As New PDFDocument(dW, dH) 
Var g As Graphics = d.Graphics
Var r as integer //row#
Var c as integer //column#
Var Row as integer // graphics row top
Var ColumnLeft() as string  //graphics column left
Var ColumnWidths() as integer  //graphics column widths
Var lMargin as integer = 18  //left margin
Var rH as integer = xx  //xx is a calculated number to determine row height
Var justified() in string  //passed parameter R or L for right or left justified column
Var pEnd as integer //tracks rows per page and when page end is reached, next page is issued
Var pC as integer  //to calculate right justified position

//aBegin and nCols are passed parameters based on the listbox details
//LB is passed parameter as listbox

//lots of code missing here but this is what seems pertinent to issue.

While r <= LB.RowCount - 1 //cycle through all rows of listbox

   //column detail
      For i = r To pEnd   //cycle through rows of listbox for current page
         For c = aBegin To nCols - 1  // cycle through columns-start to end
            Column(c) = LB.CellTextAt(r,c).Trim  //current listbox cell value. LB is listbox r&c are row and column

            select case justified(c)
            case "L"  //left justified
                g.DrawText(Column(c), ColumnLeft,  Row )
            case "R"  //right justified
*              pC = ColumnLeft + ColumnWidths(c) - g.TextWidth(Column(c)) 
*              g.DrawText(Column(c), pC , Row )
            end select

            //advance column left to next column
            ColumnLeft = ColumnLeft + ColumnWidths(c)

         Next c

         //reset row and column
         ColumnLeft = lMargin  //reset graphics current column left to left margin
         Row = Row + rH  //next graphics row top
         r = r + 1   //next listbox row

   Next i

Wend

// Saving the PDF document instance to a file
Var f As folderitem = SpecialFolder.Documents.Child("ReportOutput.pdf")
d.Save(f)
f.Open

The result looks something like this and it is consistent for every right justified column. Left justified columns are perfectly aligned Left and in the print version of this code, right justified columns are fine with same basic code.

  $1,000.00
      $250.00
         $50.00
  $2,000.00
         $25.00
      $150.00

Please let me know if you’ve seen this or know the solution.

You can add ``` before your first line of code and also after the last line of code. It will result in something like this:

Var dW as Integer //passed value depending on letter or legal selected
Var dH Integer //passed value depending on letter or legal selected
Var d As New PDFDocument(dW, dH)
Var g As Graphics = d.Graphics
Var r as integer //row#
Var c as integer //column#
Var Row as integer // graphics row top
Var ColumnLeft() as string //graphics column left
Var ColumnWidths() as integer //graphics column widths
Var lMargin as integer = 18 //left margin
Var rH as integer = xx //xx is a calculated number to determine row height
Var justified() in string //passed parameter R or L for right or left justified column
Var pEnd as integer //tracks rows per page and when page end is reached, next page is issued
Var pC as integer //to calculate right justified position

//aBegin and nCols are passed parameters based on the listbox details
//LB is passed parameter as listbox

//lots of code missing here but this is what seems pertinent to issue.

While r <= LB.RowCount - 1 //cycle through all rows of listbox

//column detail
For i = r To pEnd //cycle through rows of listbox for current page
For c = aBegin To nCols - 1 // cycle through columns-start to end
  Column(c) = LB.CellTextAt(r,c).Trim //current listbox cell value. LB is listbox r&c are row and column

  select case justified(c)
  case "L"  //left justified
    g.DrawText(Column(c), ColumnLeft,  Row )
  case "R"  //right justified
   pC = ColumnLeft + ColumnWidths(c) - g.TextWidth(Column(c)) 
   g.DrawText(Column(c), pC , Row )
  end select

  //advance column left to next column
  ColumnLeft = ColumnLeft + ColumnWidths(c)

Next c

   //reset row and column
   ColumnLeft = lMargin  //reset graphics current column left to left margin
   Row = Row + rH  //next graphics row top
   r = r + 1   //next listbox row
Next i

Wend

// Saving the PDF document instance to a file
Var f As folderitem = SpecialFolder.Documents.Child(“ReportOutput.pdf”)
d.Save(f)
f.Open

That is easier to read.

Also, if you can share a sample application I’m sure someone can take a look and try to help you.

There’s definitely some kind of problem with PDFGraphics, this issue continually comes up. I couldn’t find a ticket for it, but I know we just talked about it recently:

Thanks Alberto, I was wondering how to do that

James, is your application Desktop, right? or is Web?

Edit: sorry, for some reason I replied to Tim’s post instead of James’

Its a Desktop application. Sorry I left that out. I’m new to this.

I’m embarrassed to say as a temporary workaround, I added the below. Makes no sense at all.

Select Case Platform  //function returnes os
Case "Win"
  pC = ColumnLeft + ColumnWidths(c) - g.TextWidth(Column(c)) - (Len(column(c)) *  .85) 
Case "Lin"
  pC = ColumnLeft + ColumnWidths(c) - g.TextWidth(Column(c)) + (Len(column(c)) * 2.2) 
End Select

right justified text is very close to properly justified at least on the laptops I’ve tried it on. Definitely not a solution.

1 Like

Also, I don’t know if this makes a difference but all my fonts are system default.

By the standards Adobe has set, the core-14 fonts should not need to be installed for PDF generating software to operate. Adobe provides the font metrics for the core-14 and Xojo is quite clearly not using them.

You unfortunately need hacky workarounds until Xojo properly implements PDF.