Platforms to show: All Mac Windows Linux Cross-Platform
/DynaPDF/Parser/Text Positions with parser content
Last modified Thu, 13th May 2026.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /DynaPDF/Parser/Text Positions with parser content
Download this example: Text Positions with parser content.zip
Project "Text Positions with parser content.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub NewDocument()
If WindowCount = 0 Then
Var f As FolderItem = GetOpenFolderItem(FileTypes1.pdf)
If f <> Nil Then
OpenDocument f
End If
End If
End EventHandler
EventHandler Sub Open()
Var f As FolderItem = FindFile("license.pdf")
If f<> Nil And f.Exists Then
OpenDocument f
End If
Var ff As FolderItem = SpecialFolder.Desktop.child("test.pdf")
If ff<> Nil And ff.Exists Then
OpenDocument ff
End If
End EventHandler
EventHandler Sub OpenDocument(item As FolderItem)
Var m As New MainWindow(item)
m.show
End EventHandler
End Class
Class MainWindow Inherits Window
Control Canvas1 Inherits Canvas
ControlInstance Canvas1 Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect)
#pragma unused areas
Const factor = 2.0
Var lines() As String
Var Page as DynaPDFPageMBS = pdf.GetPage(1)
Var bbox as DynaPDFRectMBS = page.BBox(page.kpbMediaBox)
Var PageWidth as integer = bbox.Width
Var PageHeight As Integer = bbox.top - bbox.Bottom
if ShowPDF then
// need to swap width/height for rotated pages
var orientation As Integer = Page.Orientation
If orientation = 90 Or orientation = 180 Then
var temp As Integer = PageWidth
PageWidth = PageHeight
PageHeight = temp
End If
// we draw pdf as background
Var pic As Picture = pdf.RenderPagePicture(1, PageWidth*factor, PageHeight*factor, pdf.kpsFitBest, Nil)
g.DrawPicture pic, 0, 0
End If
// now draw text blocks
For Each t As PDFText In texts
Var m As DynaPDFMatrixMBS = t.EffectiveMatrix
g.TextSize = GetScaleY(m) * t.State.FontSize * factor
var TextPos As Double = 0
var verticalLayout as Boolean = t.FontInfo.WMode = 1
For Each n As DynaPDFParserContentTextNodeMBS In t.TextNodes
var OldTextPos As Double = TextPos
var text as string = n.UniText
TextPos = TextPos - n.Advance
g.ForeColor = &c00FF00
g.PenWidth = 1
If verticalLayout Then
TextPos = TextPos - n.Width
Else
TextPos = TextPos + n.Width
End If
// calculate all 4 points
// top left
Var x1 As Double = OldTextPos
Var y1 As Double = 0.0
// bottom left
Var x2 As Double = OldTextPos
Var y2 As Double = t.State.FontSize
// bottom right
Var x3 As Double = TextPos
Var y3 As Double = t.State.FontSize
// top right
Var x4 As Double = TextPos
Var y4 As Double = 0
if verticalLayout then
x1 = -t.State.FontSize/2
y1 = OldTextPos
// bottom left
x2 = t.State.FontSize
y2 = OldTextPos
// bottom right
x3 = t.State.FontSize
y3 = TextPos
// top right
x4 = -t.State.FontSize/2
y4 = TextPos
end if
m.Transform(x3,y3)
m.Transform(x2,y2)
m.Transform(x1,y1)
m.Transform(x4,y4)
If QuadPoints then
// draw rectangle (could be rotated or skewed)
g.DrawLine x1*factor, (PageHeight-y1)*factor, x2*factor, (PageHeight-y2)*factor
g.DrawLine x2*factor, (PageHeight-y2)*factor, x3*factor, (PageHeight-y3)*factor
g.DrawLine x3*factor, (PageHeight-y3)*factor, x4*factor, (PageHeight-y4)*factor
g.DrawLine x4*factor, (PageHeight-y4)*factor, x1*factor, (PageHeight-y1)*factor
Else
g.DrawRectangle x1*factor, (PageHeight-y1)*factor, (x3 - x1) * factor, (y1 - y3) * factor
end if
if ShowTexts then
g.ForeColor = &c0000FF
g.TextSize = CalcDistance(x1,y1,x2,y2)*factor
var y As Double = Min(y1,y2) // on the bottom side we draw text.
var x As Double = (x3+x1) / 2.0 // middle of the text block for our text
var tw As Double = g.StringWidth(Text)
g.DrawString text, x * factor - tw/2, (PageHeight-y)*factor
end if
next
next
End EventHandler
End Control
Control CheckShowPDF Inherits CheckBox
ControlInstance CheckShowPDF Inherits CheckBox
EventHandler Sub Action()
ShowPDF = me.Value
canvas1.Invalidate
End EventHandler
End Control
Control CheckShowText Inherits CheckBox
ControlInstance CheckShowText Inherits CheckBox
EventHandler Sub Action()
ShowTexts = me.Value
canvas1.Invalidate
End EventHandler
End Control
Control CheckQuadPoints Inherits CheckBox
ControlInstance CheckQuadPoints Inherits CheckBox
EventHandler Sub Action()
QuadPoints = me.Value
canvas1.Invalidate
End EventHandler
End Control
EventHandler Sub Close()
If pdf <> Nil Then
Call pdf.CloseFile
pdf = Nil
End If
End EventHandler
EventHandler Sub Open()
Var p As New MyDynapdfMBS
p.SetLicenseKey "Pro" // For this example you can use a Pro or Enterprise License
Var folder As FolderItem = FindFile("CMap")
If folder <> Nil Then
Call p.SetCMapDir(folder, p.klcmRecursive)
Else
Break // CMap needed for Chinese, Japanese and Korean texts
End If
call p.CreateNewPDF(Nil)
// Skip anything that is not required
call p.SetImportFlags p.kifImportAll+p.kifImportAsPage
// From which PDF file do you want to extract the images?
call p.OpenImportFile(self.file, p.kptOpen, "")
// Comment this out if you want to extract the images from specific pages only
call p.ImportPDFFile(1, 1.0, 1.0)
// query boxes to inspect page sizes
Var MediaBox As DynaPDFRectMBS = p.GetInBBox(1, p.kpbMediaBox)
Var CropBox As DynaPDFRectMBS = p.GetInBBox(1, p.kpbCropBox)
// now do search and replace
Parser = New DynaPDFParserMBS(p, DynaPDFMBS.kofDefault)
Var area As DynaPDFRectMBS = Nil // whole page
Var SearchType As Integer = DynaPDFParserMBS.kstMatchAlways
Var ContentParsingFlags As Integer = DynaPDFParserMBS.kcpfEnableTextSelection
Call p.SetPageCoords(DynaPDFMBS.kpcTopDown)
If parser.ParsePage(1, ContentParsingFlags) Then
// the state tracks current font, matrix, etc.
current = New PDFState
states.RemoveAll
Var contents() As DynaPDFParserContentMBS = parser.Contents
WalkContent contents
End If
Self.pdf = p
End EventHandler
Shared Function CalcDistance(x1 as double, y1 as Double, x2 as Double, y2 as Double) As Double
// Distance between two points
var dx as double = x2-x1
var dy as double = y2-y1
return sqrt(dx*dx + dy*dy)
End Function
Sub Constructor(file as FolderItem)
Self.file = file
Self.Title = file.DisplayName
// Calling the overridden superclass constructor.
Super.Constructor
End Sub
Shared Function FindFile(name as string) As FolderItem
// Look for file in parent folders from executable on
dim parent as FolderItem = app.ExecutableFile.Parent
while parent<>Nil
dim file as FolderItem = parent.Child(name)
if file<>Nil and file.Exists then
Return file
end if
parent = parent.Parent
wend
End Function
Shared Function GetScaleX(m As DynapdfMatrixMBS) As Double
var x1 As double= 0.0
var y1 As double= 0.0
var x2 As double= 1.0
var y2 As double= 0.0
Transform(m, x1, y1)
Transform(m, x2, y2)
if (y1 > y2) then
return -CalcDistance(x1, y1, x2, y2)
else
return CalcDistance(x1, y1, x2, y2)
end if
End Function
Shared Function GetScaleY(m As DynapdfMatrixMBS) As Double
var x1 As double= 0.0
var y1 As double= 0.0
var x2 As double= 0.0
var y2 As double= 1.0
Transform(m, x1, y1)
Transform(m, x2, y2)
if (y1 > y2) then
return -CalcDistance(x1, y1, x2, y2)
else
return CalcDistance(x1, y1, x2, y2)
end if
End Function
Shared Sub Transform(m as DynaPDFMatrixMBS, byref x as Double, byref y as double)
var tx as double = x
x = tx * M.a + y * M.c + M.x
y = tx * M.b + y * M.d + M.y
End Sub
Sub WalkContent(contents() As DynaPDFParserContentMBS)
For Each content As DynaPDFParserContentMBS In contents
var contentVariant as Variant = content
Select case content.Operator
Case DynaPDFParserContentMBS.kopMulMatrix
// multiple current matrix to e.g. move text to position
var m as DynaPDFParserContentMulMatrixMBS = contentVariant
current.matrix = m.Matrix * current.matrix
Case DynaPDFParserContentMBS.kopRestoreGS
// restore last state
current = states.pop
Case DynaPDFParserContentMBS.kopSaveGS
// save current state
states.Append current
current = New PDFState(Current)
Case DynaPDFParserContentMBS.kopSetCharSpacing
var c as DynaPDFParserContentFloatMBS = contentVariant
current.CharSpacing = c.Value
Case DynaPDFParserContentMBS.kopSetExtGState
Var c As DynaPDFParserContentExtGStateMBS = contentVariant
current.ExtGState = c
Case DynaPDFParserContentMBS.kopSetMiterLimit
Var c As DynaPDFParserContentFloatMBS = contentVariant
current.MiterLimit = c.Value
Case DynaPDFParserContentMBS.kopSetTextDrawMode
Var c As DynaPDFParserContentIntMBS = contentVariant
current.textDrawMode = c.Value
Case DynaPDFParserContentMBS.kopSetTextScale
Var c As DynaPDFParserContentFloatMBS = contentVariant
current.TextScale = c.Value
Case DynaPDFParserContentMBS.kopSetWordSpacing
Var c As DynaPDFParserContentFloatMBS = contentVariant
current.WordSpacing = c.Value
Case DynaPDFParserContentMBS.kopSetFont
Var c As DynaPDFParserContentFontMBS = contentVariant
current.FontName = c.Name
current.FontSize = c.FontSize
current.FontInfo = c.FontInfo
case DynaPDFParserContentMBS.kopDrawTemplate
// place a template in 3 steps:
// 1. begin template
Var c As DynaPDFParserContentDrawTemplateMBS = contentVariant
states.Append current
current = New PDFState(Current)
If c.matrix <> Nil Then
current.matrix = c.matrix * current.matrix
End If
// 2. draw template things
WalkContent c.Elems
// 3. end template
current = states.pop
Case DynaPDFParserContentMBS.kopDrawTranspGroup
// draw a transparency group
// 1. begin template
Var c As DynaPDFParserContentDrawGroupMBS = contentVariant
states.Append current
current = New PDFState(Current)
If c.matrix <> Nil Then
current.matrix = c.matrix * current.matrix
End If
// 2. draw template things
WalkContent c.Elems
// 3. end template
current = states.pop
Case DynaPDFParserContentMBS.kopShowText
// draw some text
Var c As DynaPDFParserContentShowTextMBS = contentVariant
var t As New PDFText
t.State = New PDFState(current)
t.FontInfo = c.FontInfo
t.Width = c.Width
t.TextNodes = c.TextNodes
t.Matrix = c.matrix
t.Parser = Parser
texts.Append t
end Select// setleading todo
next
End Sub
Property Parser As DynaPDFParserMBS
Property QuadPoints As Boolean
Property ShowPDF As Boolean = true
Property ShowTexts As Boolean
Property States() As PDFState
Property current As PDFState
Property file As FolderItem
Property pdf As MyDynaPDFMBS
Property texts() As PDFText
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MyDynaPDFMBS Inherits DynaPDFMBS
EventHandler Function Error(ErrorCode as integer, ErrorMessage as string, ErrorType as integer) As integer
#pragma unused ErrorType
// output all messages on the console:
System.DebugLog str(ErrorCode)+": "+ErrorMessage
'// and display dialog:
'Var d as New MessageDialog //declare the MessageDialog object
'Var 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
Module UtilModule
Function FindFile(name as string) As FolderItem
// Look for file in parent folders from executable on
Var parent as FolderItem = app.ExecutableFile.Parent
while parent<>Nil
Var file as FolderItem = parent.Child(name)
if file<>Nil and file.Exists then
Return file
end if
parent = parent.Parent
wend
End Function
End Module
Class PDFText
Function EffectiveMatrix() As DynaPDFMatrixMBS
Return matrix * state.matrix
End Function
Property FontInfo As DynaPDFFontInfoMBS
Property Matrix As DynapdfMatrixMBS
Property Parser As DynaPDFParserMBS
Property State As PDFState
Property TextNodes() As DynaPDFParserContentTextNodeMBS
Property Width As Double
Property index As Integer
End Class
FileTypes1
Filetype Pdf
End FileTypes1
Sign
End Sign
Class PDFState
Sub Constructor(other as PDFState)
self.CharSpacing = other.CharSpacing
self.ExtGState = other.ExtGState
self.FontInfo = other.FontInfo
self.FontName = other.FontName
self.FontSize = other.FontSize
Self.matrix = New DynaPDFMatrixMBS(other.matrix) // copy
self.MiterLimit = other.MiterLimit
self.textDrawMode = other.textDrawMode
self.TextScale = other.TextScale
self.WordSpacing = other.WordSpacing
End Sub
Sub constructor()
matrix = new DynaPDFMatrixMBS // makes new identity matrix
FontSize = 1.0
CharSpacing = 0.0
TextDrawMode = DynaPDFMBS.kdmNormal
TextScale = 100.0
WordSpacing = 0.0
End Sub
Property CharSpacing As Double
Property ExtGState As DynaPDFParserContentExtGStateMBS
Property FontInfo As DynaPDFFontInfoMBS
Property FontName As String
Property FontSize As double
Property Leading As Double
Property MiterLimit As double
Property TextScale As Double
Property WordSpacing As Double
Property matrix As DynaPDFMatrixMBS
Property textDrawMode As Integer
End Class
End Project
See also:
Download this example: Text Positions with parser content.zip
The items on this page are in the following plugins: MBS DynaPDF Plugin.