DrawText Issue in PDFDocument

Help!

I have developed a label solution for a timber company I am working with - it basically takes an input batch number, queries a web service (written by me) that communicates with their ERP system and returns a JSON string containing the data required to then create a PDFDocument and then allow them to print as required. All great so far apart from one really strange issue I just can’t resolve.

There may, on occasion, be multiple labels required to be printed for a particular batch. This is determined by examining the JSON. In the event of multiple labels then I need to output a single, multipage PDF. Again this is working well other than this one issue which involves a particular drawtext command on the graphics context. The product description can be upto 200 characters long and so I inform the drawtext command to wrap onto the next line after x characters.

On the first label this works perfectly as can be seen here:

However on the next label, after a .nextpage directive, this happens:

No matter what I try the drawtext will not wrap the text correctly. Each time the exact same command is running.

Obviously I can handle this by breaking up into multiple strings and printing each manually but just wondered if anyone had any ideas why this is behaving like this?

It almost looks like it’s using the line height of the text immediately to the left on the second page. I wonder what would happen if you used PDFGraphics.SaveState on the first page before drawing the first right-hand block, then used PDFGraphics.RestoreState on the subsequent page before drawing that same block.

Thanks Anthony. I’ll have a look. Really odd behaviour.

Ah SaveState appears to be iOS only. This is a Windows desktop app - I develop on Mac though and build for Windows. Have tested both to rule out some platform specific issue but it’s the same on both.

Think I’ll have to try and split the text into multiple strings and print those strings myself. I have to deliver the project on Monday and I’ve wasted a day now trying to get this working.

If you’re using PDFGraphics, it should have those methods. The docs don’t say that it’s iOS only for that class. If it doesn’t work, we need to get a feedback case in.

https://documentation.xojo.com/api/pdf/pdfgraphics.html#pdfgraphics-savestate

I’m getting a little confused now - if I look at the documentation it has two entries for both SaveState and RestoreState. One is for a graphics object and one for the PDFGraphics object.

Now in my program I don’t see the SaveState method at all in the auto complete?

Confused? Yes I am!

Hi @Mark_Mitchell1

Apart from the documentation for SaveState, would you mind to attach a simple project file reproducing the problem you’re facing, please? :wink:

Sorry for the delay - had to recreate - how do I attach a project file here?

You can use dropbox or other sharing online tool and post the link.

Yes thanks - just thought there may be a way to message a project on the forum.

Here is a sample project - look at page 2 on the PDF it generates.

Sample Project

Made a small modification. Looks good now:

Var myPDFDocument As New PDFDocument(595,420)
myPDFDocument.Compressed =True
myPDFDocument.Creator = "NRLabel"
myPDFDocument.Author = "NRLabel"
myPDFDocument.Keywords = "K8, Batch, Label"
myPDFDocument.Title = "K8 Batch Label"
myPDFDocument.Subject = "K8 Batch Label"
Var i As Integer
Var g As Graphics

g = myPDFDocument.Graphics

For i = 1 To 2
  
  
  
  g.DrawingColor = &c000000
  g.FontName = "Helvetica"
  
  g.FontSize = 60
  g.Bold = True
  g.DrawText("B: 7464646S22", 10, 60)
  g.DrawText("P: 01", 10,110)
  
  // Main body text
  g.FontSize = 20
  
  g.Bold = False
  
  g.DrawText("RECD: 21/01/2021", 10, 160)
  g.DrawText("PO: 65577", 220,160)
  g.DrawText("EXPIRY: 21/01/2023", 380,160)
  
  g.FontSize = 50
  g.Bold = True
  
  g.DrawText("857575757",10, 240)
  
  g.Bold = False
  g.FontSize = 18
  g.DrawText(app.longtext,330,215,180)
  
  g.FontSize = 60
  g.Bold = True
  g.DrawText("FSC",10,390)
  If i = 1 Then
    myPDFDocument.Graphics.ResetState ' added
    g.NextPage
  End If
  
  
Next i



Var PDFName As String
PDFName = "Test" + ".pdf"

Var f As FolderItem = SpecialFolder.Desktop.Child(PDFName)

Try
  // Saving and opening the PDF document with the by default PDF viewer
  // installed on the OS.
  myPDFDocument.Save(f)
  f.Open
  
Catch e As IOException
  MessageBox(e.Message)
  
End Try

1 Like

Hmmm - what does this do then?

I’ve only added myPDFDocument.Graphics.ResetState before g.NextPage to reset the properties.

I did this change and it works:
image

The correct way is to create a new BUG case in the feedback app, and attach your project there.

Looks like a BUG when setting the FontName.

Try to pun the line of code g.FontName = “Helvetica” BEFORE your FOR, in a way tha it is called only ONCE.

Yes thank you - what does reset state do is what I meant.

https://documentation.xojo.com/api/pdf/pdfgraphics.html#pdfgraphics-resetstate

Resets the FontName, FontSize, Bold, Italic and DrawingColor properties to their default states.

1 Like

Hmm - interesting.

The myPDFdocument.Graphics.ResetState mentioned by @Martin_T also works. Thank you to you both.

1 Like

I know how to look it up :slight_smile:- I was wondering why, without it, it caused this specific issue.