Platforms to show: All Mac Windows Linux Cross-Platform
/DynaPDF/Text Positions with parser
Last modified Thu, 3rd Jul 2024.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /DynaPDF/Text Positions with parser
Download this example: Text Positions with parser.zip
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
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)
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 r as DynaPDFRectMBS = t.rect
Var x As Double = r.Left * factor
Var y As Double = r.Top * factor
Var w As Double = r.Width * factor
Var h As Double = (r.Bottom - r.top) * factor
If Not QuadPoints Then
g.ForeColor = &cFF0000
g.DrawRectangle X, Y, w, h
Else
Var 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
Var 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 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
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
Var Parser As 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
Var index As Integer = 0
Var found As Boolean = Parser.FindText(area, SearchType, "")
While found
Var r As DynaPDFRectMBS = parser.SelBBox
Var 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
If False Then
// let's output rectangles back to PDF
Var OutputFile As FolderItem = SpecialFolder.Desktop.Child("output.pdf")
If p.EditPage(1) Then
// auto rotate
Call p.SetOrientationEx(p.GetOrientation)
Call p.SetLineWidth(0.0)
Call p.SetStrokeColor(255*65536) // blue
For Each t As PDFText In texts
Var height As Double = t.rect.Top - t.rect.bottom
Call p.Rectangle(t.rect.Left, t.rect.top, t.rect.Width, Height, p.kfmClose)
Next
Call p.ClosePath(p.kfmStroke)
Call p.EndPage
Call p.OpenOutputFile(OutputFile)
End If
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:
'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
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:
- /DynaPDF/text editing/delete text
- /DynaPDF/text editing/edit text
- /DynaPDF/text editing/replace text
- /DynaPDF/Text extraction
- /DynaPDF/text formatting/text formatting Ansi
- /DynaPDF/text formatting/text formatting parallel
- /DynaPDF/text formatting/text formatting Unicode
- /DynaPDF/Text Positions
Download this example: Text Positions with parser.zip
The items on this page are in the following plugins: MBS DynaPDF Plugin.
