In the Open dialog of the Codeviewer application you can see some PDF files. Opening such a PDF file will give you the “This file extension is not supported” message.
The previous tutorial about the PDFbox_test was build to show PDF files.
Wouldn’t it be great if you can launch the PDFbox_test application from within the Codeviewer application? We can do just that.
Here’s how you can make the changes in the code from the two applications.
Let’s start with the Codeviewer application.
In the subroutine is_supported_file you add the extension “.pdf”:
Code:
Private Sub is_supported_file(fn As String) As Boolean
Dim file_ext As List = Array As String(".bas",".b4a",".b4j",".b4i",".txt", _
".php",".html",".htm",".xml",".css",".js", _
".json",".sql","java",".tpl",".var",".pdf")
In the subroutine show_file_content you add a test for the PDF extension:
Code:
Private Sub show_file_content(path As String,fname As String)
reset_UI
lblfilename.Text = path & fname
Dim strtxt As String = File.ReadString(path,fname)
extension = fname.SubString(fname.LastIndexOf("."))
If extension = ".pdf" Then
Dim shl As Shell
Private arguments() As String = Array As String(path & fname)
shl.Initialize("shl", "E:\B4J\pdfbox_test\B4J\Objects\temp\build\pdfbox_test.exe", arguments)
shl.WorkingDirectory = "E:\B4J\pdfbox_test\B4J\Objects\temp\build"
shl.Run(10000) 'set a timeout of 10 seconds
B4XPage_Appear
Return
End If
Make sure you add the jShell library to the Library Manager panel in the IDE.
You have to change the paths to your situation of course.
We are using a shell command with a command line argument. This command will start the pdfbox_test.exe application with the PDF file argument.
Run the Codeviewer application to see if it still works. Build a standalone package (menu Project / Build Standalone Package) that will give you the Codeviewer.exe file.
On to the second application, the PDFBox_test project.
In this application we need to check if there is a command line argument provided or not.
Open the project in the IDE and go to the Main tab.
In the Process_Globals subroutine add a declaration for a Public variable (param1):
Code:
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Public param1 As String
End Sub
In the AppStart subroutine add the test for the command line arguments:
Code:
Sub AppStart (Form1 As Form, Args() As String)
If Args.Length > 0 Then
param1 = Args(0)
Else
param1 = ""
End If
MainForm = Form1
MainForm.Show
Dim PagesManager As B4XPagesManager
PagesManager.Initialize(MainForm)
End Sub
Go to the B4XMainPage tab in the IDE and change the following line:
Code:
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
B4XPages.SetTitle(Me,"PDFBox test")
fchoose.Initialize
devwidth = B4XPages.GetNativeParent(B4XPages.MainPage).RootPane.Width
pdffile = Main.param1
Run the PDFBox_test application to see if it still works. You should see the message "Please load a PDF file first!". Unless of course you commented that line.
Build the standalone package and you can start testing.
Start the Codeviewer application and open a PDF file. See if the PDFBox_test application is started with the selected PDF file loaded. And that’s it.
On to the next project… maybe a dashboard application that starts all kinds of applications…
Happy coding!
Paul