Platforms to show: All Mac Windows Linux Cross-Platform
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /DynaPDF/Find and Replace
This example is the version from Wed, 21th Nov 2023.
Project "Find and Replace.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
EventHandler Sub Open()
Dim inputFile As FolderItem = FindFile("dynapdf_help.pdf")
Dim outputFile As FolderItem = SpecialFolder.Desktop.Child("output.pdf")
Dim pdf As New MyDynapdfMBS
pdf.SetLicenseKey "Pro" // For this example you can use a Pro or Enterprise License
Call pdf.CreateNewPDF // We do not produce a PDF file in this example!
// External cmaps should always be loaded when extracting text from PDF files.
// See the description of ParseContent() for further information.
dim folder as FolderItem = FindFile("CMap")
if folder<> nil then
Call pdf.SetCMapDir(folder, pdf.klcmRecursive)
end if
call pdf.SetImportFlags(BitwiseOr(pdf.kifImportAll, pdf.kifImportAsPage))
If pdf.OpenImportFile(inputFile, pdf.kptOpen, "") < 0 Then
MsgBox "Failed to load input pdf" + EndOfLine+EndOfLine + inputFile.NativePath
quit
end if
// import all pages and close file
call pdf.ImportPDFFile(1, 1.0, 1.0)
// now do search and replace
Dim FindText As String = "PDF"
Dim ReplaceText As String = "Test"
Dim count As Integer = pdf.GetPageCount
Dim Parser As New DynaPDFParserMBS(pdf)
Dim area As DynaPDFRectMBS = Nil // whole page
Dim SearchType As Integer = DynaPDFParserMBS.kstCaseInSensitive
Dim ContentParsingFlags As Integer = DynaPDFParserMBS.kcpfEnableTextSelection
For i As Integer = 1 To count
Dim needWrite As Boolean
If parser.ParsePage(i, ContentParsingFlags) Then
Dim found As Boolean = Parser.FindText(area, SearchType, FindText)
While found
If Parser.ReplaceSelText(ReplaceText) Then
needWrite = True
End If
found = Parser.FindText(area, SearchType, FindText, True)
Wend
If needWrite Then
Call Parser.WriteToPage
End If
End If
next
call pdf.OpenOutputFile(outputFile)
Call pdf.CloseFile
End EventHandler
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 Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
FileTypes1
Filetype Pdf
End FileTypes1
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
Sign
End Sign
End Project
The items on this page are in the following plugins: MBS DynaPDF Plugin.