Platforms to show: All Mac Windows Linux Cross-Platform
/DynaPDF/Text Positions with parser
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /DynaPDF/Text Positions with parser
This example is the version from Thu, 28th Feb 2024.
Project "Text Positions with parser.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub NewDocument()
If WindowCount = 0 Then
Dim f As FolderItem = GetOpenFolderItem(FileTypes1.pdf)
If f <> Nil Then
OpenDocument f
End If
End If
End EventHandler
EventHandler Sub Open()
Dim f As FolderItem = FindFile("license.pdf")
If f<> Nil And f.Exists Then
OpenDocument f
End If
Dim 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)
Dim 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)
Const factor = 2.0
dim lines() as string
dim Page as DynaPDFPageMBS = pdf.GetPage(1)
dim bbox as DynaPDFRectMBS = page.BBox(page.kpbMediaBox)
dim PageWidth as integer = bbox.Width
Dim PageHeight As Integer = bbox.top - bbox.Bottom
if ShowPDF then
// we draw pdf as background
dim pic as Picture = pdf.RenderPagePicture(1, PageWidth*factor, PageHeight*factor, 2, nil)
g.DrawPicture pic, 0, 0
end if
// now draw text blocks
For Each t As PDFText In texts
dim r as DynaPDFRectMBS = t.rect
Dim x As Double = r.Left * factor
Dim y As Double = r.Top * factor
Dim w As Double = r.Width * factor
Dim h As Double = (r.Bottom - r.top) * factor
If Not QuadPoints Then
g.ForeColor = &cFF0000
g.DrawRectangle X, Y, w, h
Else
Dim points() As DynaPDFPointMBS = t.points
g.ForeColor = &c00FF00
g.DrawLine points(0).X * factor, points(0).Y * factor, points(1).X * factor, points(1).Y * factor
g.DrawLine points(1).X * factor, points(1).Y * factor, points(2).X * factor, points(2).Y * factor
g.DrawLine points(2).X * factor, points(2).Y * factor, points(3).X * factor, points(3).Y * factor
g.DrawLine points(3).X * factor, points(3).Y * factor, points(0).X * factor, points(0).Y * factor
End If
if ShowTexts then
g.ForeColor = &c0000FF
g.TextSize = h
Dim tw As Double = g.StringWidth(t.Text)
g.DrawString t.Text, x + (tw - w) / 2, y + h
end if
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 Open()
dim p as new MyDynapdfMBS
p.SetLicenseKey "Pro" // For this example you can use a Pro or Enterprise License
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)
call p.CloseImportFile
// now do search and replace
Dim Parser As New DynaPDFParserMBS(p)
Dim area As DynaPDFRectMBS = Nil // whole page
Dim SearchType As Integer = DynaPDFParserMBS.kstMatchAlways
Dim ContentParsingFlags As Integer = DynaPDFParserMBS.kcpfEnableTextSelection
If parser.ParsePage(1, ContentParsingFlags) Then
Dim index As Integer = 0
Dim found As Boolean = Parser.FindText(area, SearchType, "")
While found
Dim r As DynaPDFRectMBS = parser.SelBBox
Dim t As New PDFText
t.Text = parser.SelText
t.rect = r
t.index = index
t.points = parser.SelBBox2
texts.Append t
index = index + 1
found = Parser.FindText(area, SearchType, "", True)
Wend
End If
Self.pdf = p
End EventHandler
Sub Constructor(file as FolderItem)
Self.file = file
Self.Title = file.DisplayName
// Calling the overridden superclass constructor.
Super.Constructor
End Sub
Property QuadPoints As Boolean
Property ShowPDF As Boolean = true
Property ShowTexts As Boolean
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
// output all messages on the console:
System.DebugLog str(ErrorCode)+": "+ErrorMessage
'// and display dialog:
'Dim d as New MessageDialog //declare the MessageDialog object
'Dim 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
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
End Module
Class PDFText
Property index As Integer
Property points() As DynaPDFPointMBS
Property rect As DynaPDFRectMBS
Property text As string
End Class
FileTypes1
Filetype Pdf
End FileTypes1
Sign
End Sign
End Project
See also:
The items on this page are in the following plugins: MBS DynaPDF Plugin.