As expected: My fault. Now here is a small tool that allows you to convert pdf texts completely or just the first page.
@marc56us: now i can update to the latest version. (I also saw it during my research.)
Code: Select all
EnableExplicit
DebugLevel 9
#ProductName$ = "Test-PDF2Text"
#ProductVersion$ = "0." + #PB_Editor_BuildCount + "." + #PB_Editor_CompileCount
#ProductCopyright$ = "Copyright (c) 2021 by AHa "
;' Window Title or Caption
#MainCaption$ = #ProductName$ + " " + #ProductVersion$
#Caption$ = #ProductName$ + " "
;' Program Flags
#PB_Program_Flags = #PB_Program_Open|#PB_Program_Read|#PB_Program_Error|#PB_Program_Ascii|#PB_Program_Hide
#ExecProg$ = "C:\Tools\Xpdf\bin64\pdftotext.exe" ;' @TODO please adjust here to your own needs
#DefaultFile$ = "C:\Tools\Xpdf\bin64\test.pdf" ;' @TODO please adjust here to your own needs
Enumeration EWindow 1 ;' starting with 1 because zero is reserved for Errors, Defaults, etc.
#WINDOW_Main
EndEnumeration
Enumeration EGadget 1 ;' I do not use zero here as well
#BUTTON_AllPages
#BUTTON_FirstPage
#STRING_File
#LIST_Text
EndEnumeration
Procedure RunPDFToText(File$, xAllPages)
Protected result, pid, stdout$, stderr$, tmp$
If FindString(File$, " ") > 0
File$ = #DQUOTE$ + File$ + #DQUOTE$ :Debug "INFO : take care of spaces in Filename "
EndIf
tmp$ = ""
If xAllPages = 0
tmp$ = "-f 1 -l 1 "
EndIf
pid = RunProgram(#ExecProg$, tmp$ + File$ + " -", "", #PB_Program_Flags)
If IsProgram(pid) :Debug "INFO : running "
While ProgramRunning(pid)
If AvailableProgramOutput(pid)
stdout$ = ReadProgramString(pid)
If stdout$
AddGadgetItem(#LIST_Text, -1, stdout$) ;:Debug ""+LSet(stdout$, 112)+" ; = "+RSet(Str(Len(stdout$)), 3)+" Chars "
EndIf
stderr$ = ReadProgramError(pid, #PB_Ascii) : If stderr$ :Debug "ERROR: " + stderr$ : EndIf
EndIf
Wend ;' ProgramRunning()
result = ProgramExitCode(pid) :Debug "INFO : ExitCode = " + result
CloseProgram(pid) :Debug "INFO : done"
EndIf
ProcedureReturn result
EndProcedure ;()
;=== Main Window ======================================================================================================
Procedure OnEventSizeMainWindow()
Protected ww, wh
ww = WindowWidth(#WINDOW_Main) : wh = WindowHeight(#WINDOW_Main)
ResizeGadget(#STRING_File, #PB_Ignore, #PB_Ignore, ww-172, #PB_Ignore)
ResizeGadget(#BUTTON_AllPages, ww-164, #PB_Ignore, #PB_Ignore, #PB_Ignore)
ResizeGadget(#BUTTON_FirstPage, ww-84, #PB_Ignore, #PB_Ignore, #PB_Ignore)
ResizeGadget(#LIST_Text, #PB_Ignore, #PB_Ignore, ww-8, wh-36)
EndProcedure
Procedure OpenMainWindow()
Protected hwnd, ww, wh
ww = 800 : wh = 600
hwnd = OpenWindow(#WINDOW_Main, 0, 0, ww, wh , #MainCaption$, #PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_SizeGadget)
If hwnd <> 0
StringGadget(#STRING_File, 4, 5, ww-172, 20, #DefaultFile$)
SendMessage_(GadgetID(#STRING_File), #EM_SETCUEBANNER, #Null, @"filename of the pdf file (copy the filename here)")
SHAutoComplete_(GadgetID(#STRING_File), $50000021) ;' #SHACF_AUTOAPPEND_FORCE_ON | #SHACF_AUTOSUGGEST_FORCE_ON | #SHACF_FILESYSTEM | #SHACF_FILESYS_DIRS
ButtonGadget(#BUTTON_AllPages, ww-164, 4, 76, 24, "All Pages")
ButtonGadget(#BUTTON_FirstPage, ww-84, 4, 76, 24, "First Page")
ListViewGadget(#LIST_Text, 4, 32, ww-8, wh-36)
BindEvent(#PB_Event_SizeWindow, @OnEventSizeMainWindow(), #WINDOW_Main)
EnableGadgetDrop(#STRING_File, #PB_Drop_Files, #PB_Drag_Copy) ;' make it droppable for filenames
EndIf
ProcedureReturn hwnd
EndProcedure
Procedure main()
Protected tt$
CoInitialize_(#Null) ;' for autocomplete in stringgadgets
If OpenMainWindow()
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
Select EventGadget()
Case #BUTTON_FirstPage
tt$ = GetGadgetText(#STRING_File)
ClearGadgetItems(#LIST_Text)
RunPDFToText(tt$, 0)
Case #BUTTON_AllPages
tt$ = GetGadgetText(#STRING_File)
ClearGadgetItems(#LIST_Text)
RunPDFToText(tt$, 1)
EndSelect
Case #PB_Event_GadgetDrop
Select EventGadget()
Case #STRING_File
tt$ = EventDropFiles()
tt$ = StringField(tt$, 1, #LF$)
SetGadgetText(#STRING_File, tt$)
ClearGadgetItems(#LIST_Text)
RunPDFToText(tt$, 0)
EndSelect
EndSelect ; WaitWindowEvent()
ForEver
EndIf ; OpenWindow()
CoUninitialize_() ;' autocomplete
ProcedureReturn 0
EndProcedure
End main()
;BoF