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:
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.
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.
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.
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