B4J Question get pdf filename from B4j Print JavaFX8 library

le_toubib

Active Member
Licensed User
Longtime User
hi all
i'm using this library : B4j Print JavaFX8 to print a layout into a pdf file
normally , the save file chooser dialogue is triggered to save the file .
my question is how to retrieve the file name after saving it .
and can i skip the file chooser dialogue to save it programmatically ?
 

stevel05

Expert
Licensed User
Longtime User
You can't retrieve the job name after you've printed it, but you can set it before you print it see https://www.b4x.com/android/forum/threads/b4j-print-javafx8.49836/post-652102 (May work slightly differently for different printers).

Yes you can skip the dialogs, you will need to get the printer you want to print to using Printer_Static.GetAllPrinters, and find the printer object

Something like:
B4X:
Dim mPrinterName As String = "the target printer name"
Dim Ptr As Printer
Dim TPJ As PrinterJob
Dim JS As JobSettings
Dim PL As PageLayout

For Each P As Printer In AllPrinters
    If P.GetName.ToLowerCase = mPrinterName.ToLowerCase Then
        Ptr = P
        Exit
    End If
Next

Or get the default printer Ptr = Printer_Static.GetDefaultPrinter

Create a printer Job from the printer: TPJ = PrinterJob_Static.CreatePrinterJob2(Ptr)

Get the Jobsettings JS = TPJ.GetJobSettings, Unless you are happy with the defaults which you would have to try as they are not likely to be the same for all printers.

Then set the paper and other settings you require:
B4X:
PL = Ptr.CreatePageLayout(mPaper,mOrientation,LeftPageMargin,RightPageMargin,TopPageMargin,BottomPageMargin)
JS.SetPageLayout(PL)
 
Last edited:
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
You can't retrieve the job name after you've printed it, but you can set it before you print it see https://www.b4x.com/android/forum/threads/b4j-print-javafx8.49836/post-652102 (May work slightly differently for different printers).

Yes you can skip the dialogs, you will need to get the printer you want to print to using Printer_Static.GetAllPrinters, and find the printer object

Something like:
B4X:
Dim mPrinterName As String = "the target printer name"
Dim Ptr As Printer
Dim TPJ As PrinterJob
Dim JS As JobSettings
Dim PL As PageLayout

For Each P As Printer In AllPrinters
    If P.GetName.ToLowerCase = mPrinterName.ToLowerCase Then
        Ptr = P
        Exit
    End If
Next

Or get the default printer Ptr = Printer_Static.GetDefaultPrinter

Create a printer Job from the printer: TPJ = PrinterJob_Static.CreatePrinterJob2(Ptr)

Get the Jobsettings JS = TPJ.GetJobSettings, Unless you are happy with the defaults which you would have to try as they are not likely to be the same for all printers.

Then set the paper and other settings you require:
B4X:
PL = Ptr.CreatePageLayout(mPaper,mOrientation,LeftPageMargin,RightPageMargin,TopPageMargin,BottomPageMargin)
JS.SetPageLayout(PL)
thank you for your reply , i didn't mean to get the job name nor the printer dialogue ,
i ment to get the saved PDF File Name after saving it,
or better skip the save pdf save dialogue , and save the file programmatically .
i have already chosen the microsoft pdf printer , which works fine . the problem is in the file chooser dialogue .
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The link shows how to name the saved document. And the code is what you need to do to replace the dialogs.
 
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
The link shows how to name the saved document. And the code is what you need to do to replace the dialogs.
thanks again for your support ,
i'm using this code :
Microsoft Print to PDF:
PA.Initialize()
        PA = Paper_static.A4
        Dim PRPDF As Printer= GetPrinter("Microsoft Print to PDF")
        Dim PL As PageLayout
        PL.Initialize()
        PL = PRPDF.CreatePageLayout2(PA, PageOrientation_Static.PORTRAIT, "HARDWARE_MINIMUM")
        Dim PJR As PrinterJob = PrinterJob_Static.CreatePrinterJob2(PRPDF)
        Dim fn As String = File.GetUri(File.Dirdata("appdatafolder") & "\"& "PDFs\", "FileName")
        PJR.GetJobSettings.SetJobName(fn)
        PJR.PrintPage2(PL,Panetoprint)
        PJR.EndJob

the code is working fine in creating a the pdf , but since i need this operation to be automated , i need to control where will the pdf file will be saved and decide its name programmatically , but here i still get the save file dialogue , which allows user interference with file location .
furthermore : setting jobname didn't affect the filename user choice , not even suggesting the name in the dialogue.
any idea ?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
As I mentioned, the SetJobName sub works differently on different printers, it seems it doesn't work for MicrosoftPDF. And it would only set the JobName, not the full path.

This code works with Microsoft PDF printer (can't guarantee others) to set the full path (which must exist) and requires the reflection library.
B4X:
Dim PA As Paper
    PA.Initialize()
    PA = Paper_static.A4
    Dim PRPDF As Printer = GetPrinter("Microsoft Print to PDF")
    Dim PL As PageLayout
    PL.Initialize()
    PL = PRPDF.CreatePageLayout2(PA, PageOrientation_Static.PORTRAIT, "HARDWARE_MINIMUM")
    Dim PJR As PrinterJob = PrinterJob_Static.CreatePrinterJob2(PRPDF)
'    Dim fn As String = "FileName.pdf"'File.GetUri(File.Dirdata("appdatafolder") & "\"& "PDFs\", "FileName")
'    Dim PS As JobSettings = PJR.GetJobSettings
'    PS.SetJobName(fn)

    'Set the full filepath.  Works for Microsoft PDF printer
    Dim Refl As Reflector
    Refl.Target = PJR.GetObject
    Refl.Target = Refl.GetField("jobImpl")
    Dim AttSet As JavaObject = Refl.GetField("printReqAttrSet")
 
    'Directory has to exist
    File.MakeDir("D:\","PDfs")
 
    Dim URI As JavaObject
    URI.InitializeNewInstance("java.net.URI",Array(File.GetUri(File.Combine("D:\","PDfs"),"FileName.pdf")))
 
    Dim Dest As JavaObject
    Dest.InitializeNewInstance("javax.print.attribute.standard.Destination",Array(URI))
    AttSet.RunMethod("add",Array(Dest))


    PJR.PrintPage2(PL,Panetoprint)
    PJR.EndJob

Note: It creates and saved the document to a folder Pdfs on the D:\ drive, you will need to change that.
And doesn't display a dialog.
 
Last edited:
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
As I mentioned, the SetJobName sub works differently on different printers, it seems it doesn't work for MicrosoftPDF. And it would only set the JobName, not the full path.

This code works with Microsoft PDF printer (can't guarantee others) to set the full path (which must exist) and requires the reflection library.
B4X:
Dim PA As Paper
    PA.Initialize()
    PA = Paper_static.A4
    Dim PRPDF As Printer = GetPrinter("Microsoft Print to PDF")
    Dim PL As PageLayout
    PL.Initialize()
    PL = PRPDF.CreatePageLayout2(PA, PageOrientation_Static.PORTRAIT, "HARDWARE_MINIMUM")
    Dim PJR As PrinterJob = PrinterJob_Static.CreatePrinterJob2(PRPDF)
'    Dim fn As String = "FileName.pdf"'File.GetUri(File.Dirdata("appdatafolder") & "\"& "PDFs\", "FileName")
'    Dim PS As JobSettings = PJR.GetJobSettings
'    PS.SetJobName(fn)

    'Set the full filepath.  Works for Microsoft PDF printer
    Dim Refl As Reflector
    Refl.Target = PJR.GetObject
    Refl.Target = Refl.GetField("jobImpl")
    Dim AttSet As JavaObject = Refl.GetField("printReqAttrSet")
 
    'Directory has to exist
    File.MakeDir("D:\","PDfs")
 
    Dim URI As JavaObject
    URI.InitializeNewInstance("java.net.URI",Array(File.GetUri(File.Combine("D:\","PDfs"),"FileName.pdf")))
 
    Dim Dest As JavaObject
    Dest.InitializeNewInstance("javax.print.attribute.standard.Destination",Array(URI))
    AttSet.RunMethod("add",Array(Dest))


    PJR.PrintPage2(PL,Panetoprint)
    PJR.EndJob

Note: It creates and saved the document to a folder Pdfs on the D:\ drive, you will need to change that.
And doesn't display a dialog.
thank you so very much , that was really helpful and insightful
 
Upvote 0
Top