B4J Question Printing Multiple Pages to Pdf using JavaFX8

MarcTG

Active Member
Licensed User
Longtime User
@Erel Here's the post I was talking about:

If it can be useful to someone else:

I have created a procedure that by selecting the printer (Better if physical, does not work well with the PDF virtual printer) extracts all the nodes (or views) corresponding to a page (in this case A4) from the panel and attach them to another panel, print them and put them back in the original panel, then do the same with the views that did not enter the first page perform a second print and so on until all the views are printed


B4X:
Sub PrintMultiplePage(Printer As Printer,PL As PageLayout,Panel As Pane, HeightPrinterPage As Double)
    Dim Left,Top,Height,Width As Int
    Dim TopPage=0,IndexNode  As Int
 
    Do While TopPage<Panel.Height
        Dim PanePrint As Pane
        Dim PJ As PrinterJob= PrinterJob_Static.CreatePrinterJob2(Printer)
        PanePrint.Initialize("")
        IndexNode=0
        Do While IndexNode<Panel.NumberOfNodes
            Dim V As Node = Panel.GetNode(IndexNode)
            If V.Top>=TopPage And V.Top<(TopPage+HeightPrinterPage) Then
                Left=V.Left
                Top=V.Top-TopPage
                Height=V.PrefHeight
                Width=V.PrefWidth
                V.RemoveNodeFromParent
                PanePrint.AddNode(V,Left,Top,Width,Height)
            Else
                IndexNode=IndexNode+1
            End If
        Loop
        PJ.PrintPage2(PL,PanePrint)
        PJ.EndJob

        For IndexNode=0 To PanePrint.NumberOfNodes-1
            Dim V As Node = PanePrint.GetNode(0)
            Left=V.Left
            Top=V.Top+TopPage
            Height=V.PrefHeight
            Width=V.PrefWidth
            V.RemoveNodeFromParent
            Panel.AddNode(V,Left,Top,Width,Height)
        Next
        TopPage=TopPage+HeightPrinterPage
    Loop
 
End Sub

Sub PrintMultiplePage(Printer As Printer,PL As PageLayout,Panel As Pane, HeightPrinterPage As Double)
    Dim Page As Int = Round(Panel.Height/HeightPrinterPage)
    Dim Left,Top,Height,Width, IndexNode As Int

    For i=0 To Page-1
        Dim PanePrint As Pane
        Dim TopPage As Int = I * HeightPrinterPage
        Dim PJ As PrinterJob= PrinterJob_Static.CreatePrinterJob2(Printer)
        PanePrint.Initialize("")
        Log(":::" & TopPage & "--" & Panel.NumberOfNodes)
        IndexNode=0
        Do While IndexNode<Panel.NumberOfNodes
            Dim V As Node = Panel.GetNode(IndexNode)
            Log("_" & V.Top)
            If V.Top>=TopPage And V.Top<(TopPage+HeightPrinterPage) Then
                Left=V.Left
                Top=V.Top-TopPage
                Height=V.PrefHeight
                Width=V.PrefWidth
                V.RemoveNodeFromParent
                PanePrint.AddNode(V,Left,Top,Width,Height)
            Else
                IndexNode=IndexNode+1
            End If
        Loop
        PJ.PrintPage2(PL,PanePrint)
        PJ.EndJob

        For IndexNode=0 To PanePrint.NumberOfNodes-1
            Dim V As Node = PanePrint.GetNode(0)
            Left=V.Left
            Top=V.Top+TopPage
            Height=V.PrefHeight
            Width=V.PrefWidth
            V.RemoveNodeFromParent
            Panel.AddNode(V,Left,Top,Width,Height)
        Next
    Next

End Sub

View attachment 75468

Is there any other simpler way of doing this?
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
here
 

Attachments

  • PrinterSample.zip
    2.6 KB · Views: 343
Upvote 0

MarcTG

Active Member
Licensed User
Longtime User
Thanks!

When I try to print to pdf it prints each page in a separate file... Not sure if it could be made to print multiple pages to one file.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Thanks!

When I try to print to pdf it prints each page in a separate file... Not sure if it could be made to print multiple pages to one file.
As I explained in the thread it is suitable for physical printers.

For PDF use a suitable PDF library.
 
Upvote 0
Top