B4J Question HTMLEditorWrapper - Edit HTML

T201016

Active Member
Licensed User
Longtime User
Hello,
I have a question about the text editor from the example: HTMLEditorWrapper
Well, if you want to print the entire text (see: ScreenShot_0.png) on an A4 sheet,
I only get a fragment of the text printout (ScreenShot_1.png).
I checked the printer parameters, but nothing seems to be wrong
setting, such as multi-page 4 in 1 printing.


I print using the function below,
where "PrintOut" is the content of the text to be printed:

PrintToJobRun:
Public Sub PrintToJobRun(Job As Object, PrintOut As String) As ResumableSub
    Dim WebV As B4XView = HTMLEditor1.As(JavaObject).RunMethod("lookup",Array("WebView"))
    If WebV.IsInitialized Then       
'
'        May be needed to hide the caret, I've seen it once or twice on prints
'        PRINTOUT of formatted text in HtmlText. (don't do anything!)
'
        Dim OrigHTML As String = HTMLEditor1.HtmlText
        HTMLEditor1.HtmlText = PrintOut
        
        Dim JS As JavaObject
        JS.InitializeStatic("org.jsoup.Jsoup")
        Dim Doc As JavaObject = JS.RunMethod("parse", Array(HTMLEditor1.HtmlText))
        
        Dim Body As JavaObject = Doc.RunMethod("body",Null)
        Dim PropStr As String = Body.RunMethod("attr",Array("style"))
        Body.RunMethod("attr",Array("style","caret-color:transparent;" & PropStr))
        
        Dim WebE As JavaObject = WebV.As(JavaObject).RunMethodJO("getEngine",Null)
        WebE.RunMethod("print",Array(Job))
        Sleep(50)
'
'        Unhide the caret
'
        HTMLEditor1.HtmlText = OrigHTML
        HERequestFocus
        Return True
    Else
        Return False
    End If
End Sub

Thank you in advance for your help in explaining how to actually print in A4 format, the entire content of the text.
 

Attachments

  • ScreenShot_0.png
    ScreenShot_0.png
    40.6 KB · Views: 126
  • ScreenShot_1.png
    ScreenShot_1.png
    21.9 KB · Views: 127
Solution
Try this one, You'll see the changes I've made to the Initialize_A4 sub.

Basically they are

  • Created a new form to display the preview and a new layout to keep them separate and avoid possible timing issues with resizing the label.
  • Set some padding on the labels to avoid clipping at the right edge of the print.
  • Set the fontsizes in code, probably not needed if you check the layout fontsizes are the same, but it's probably be easier to maintain.
  • Added an option to use the PrintDialog (it made it easier to test so I left it there)
  • Set the label width and height to the reported printable width and height from the printerLayout
  • Removed the wait on the preview form just so it's easier to compare the preview and the...

stevel05

Expert
Licensed User
Longtime User
See the PrinterLayout Get...Margin methods.
 
Upvote 0

T201016

Active Member
Licensed User
Longtime User
See the PrinterLayout Get...Margin methods.
Unfortunately, I have failed to properly print an A4 page.
- I still have the same problem as in the post (see Attachments): Printing trouble - post: #4

Run::
Sub Initialize_A4(textstring As String)
    MainView.Initialize("", 0,0)
'    MainView.WindowWidth  = 605 'For A4 605x870
'    MainView.WindowHeight = 870 'For A4 605x870
    MainView.RootPane.LoadLayout("A4Size.bjl") '---->>> For A4 605x870 <<<----
    
    MainView.Title = SharedCode.JopView
    MainView.Icon = fx.LoadImage(File.DirAssets, "Untitled - 4.png")
    MainView.WindowHeight = fx.PrimaryScreen.MaxY - fx.PrimaryScreen.MinY
    
    LblTotal.Text = textstring
    MainView.ShowAndWait
    If LblTotal.Text <> "" Then
        Dim PA As Paper
        PA.Initialize()
        PA = Paper_static.A4
        
        Private PR As Printer
        PR.Initialize()
        PR = Printer_Static.GetDefaultPrinter
        
        Private PL As PageLayout
        PL.Initialize()
        PL = PR.CreatePageLayout2(PA, PageOrientation_Static.PORTRAIT, "DEFAULT") '"HARDWARE_MINIMUM")
        
        Private PJ As PrinterJob = PrinterJob_Static.CreatePrinterJob2(PR)
        Dim JS As JobSettings = PJ.GetJobSettings
        JS.SetJobName("New 1")
        PJ.PrintPage2(PL, LblTotal)
        If PJ.EndJob Then
            Dim myClipBoard As JavaObject
            myClipBoard.InitializeStatic("javafx.scene.input.Clipboard")
            myClipBoard.RunMethodJO("getSystemClipboard",Null).RunMethod("clear",Null)
            Main.xui.Msgbox2Async("Successfully spooleded to the printer queue.", "Printer Job","OK","","",Null)
        End If
    End If
End Sub
 

Attachments

  • A4Size.png
    A4Size.png
    49.4 KB · Views: 94
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You are getting closer, can you post your latest project to save me having to recreate what you have done so I can test with that.

Or, if you want to test my theory you just need to change the layout width to the page size (as it is) minus the default margin width. Ideally you will need to do it before you display the print preview, so you will also need to change the program flow a little.
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Try this one, You'll see the changes I've made to the Initialize_A4 sub.

Basically they are

  • Created a new form to display the preview and a new layout to keep them separate and avoid possible timing issues with resizing the label.
  • Set some padding on the labels to avoid clipping at the right edge of the print.
  • Set the fontsizes in code, probably not needed if you check the layout fontsizes are the same, but it's probably be easier to maintain.
  • Added an option to use the PrintDialog (it made it easier to test so I left it there)
  • Set the label width and height to the reported printable width and height from the printerLayout
  • Removed the wait on the preview form just so it's easier to compare the preview and the pdf, you can put the andwait back if you want it.
  • Additionally I set the HandleResizeEvent to off from the A4 layouts.

I've also added the bug fix provided by jmon to the HTMLEditorWrapper designerCreate sub to correctly display the height of the HTMLEditor. : https://www.b4x.com/android/forum/threads/htmleditorwrapper-edit-html.139491/post-891661


Edit: Oh and I changed the type of the labels to B4xView as it's easier to work with sizing.
 

Attachments

  • HTMLEditorSLzip.zip
    65.9 KB · Views: 95
Last edited:
Upvote 1
Solution

T201016

Active Member
Licensed User
Longtime User
Try this one, You'll see the changes I've made to the Initialize_A4 sub.

Basically they are

  • Created a new form to display the preview and a new layout to keep them separate and avoid possible timing issues with resizing the label.
  • Set some padding on the labels to avoid clipping at the right edge of the print.
  • Set the fontsizes in code, probably not needed if you check the layout fontsizes are the same, but it's probably be easier to maintain.
  • Added an option to use the PrintDialog (it made it easier to test so I left it there)
  • Set the label width and height to the reported printable width and height from the printerLayout
  • Removed the wait on the preview form just so it's easier to compare the preview and the pdf, you can put the andwait back if you want it.
  • Additionally I set the HandleResizeEvent to off from the A4 layout,

I've also added the bug fix provided by jmon to the HTMLEditorWrapper designerCreate sub to correctly display the height of the HTMLEditor. : https://www.b4x.com/android/forum/threads/htmleditorwrapper-edit-html.139491/post-891661


Edit: Oh and I changed the type of the labels to B4xView as it's easier to work with sizing.

Great, I'll check your changes. Thank you very much for the test material.
 
Upvote 0

T201016

Active Member
Licensed User
Longtime User
I just made your changes and so far it seems everything is fine. I will continue to test ...

Cool! Thank you very much again for your time.
 
Upvote 0
Top