Share My Creation PDFBoxWrapper Class Module - show PDF-document in B4J

Discussion in 'B4J Share Your Creations' started by knutf, Mar 3, 2019.

  1. knutf

    knutf Member Licensed User

    PDFBoxWrapper is a Class Module that use PDFBox to view PDF-documents.

    The PDFBoxWrapper Class module need a Pane and a layout to show the PDF-document in the Pane. The Pane and the name of the layout must be specified as parameters to the initialize routine of the class.
    The layout must contain a ScrollPane named "myScrollPane".
    The layout can also optional contain:
    • Spinner for scaleing the page ,named "SpinnerZoom"
    • Spinner turning pages, named "SpinnerPageNumber"
    • Button rotate pages 90degrees clockwise, named ButtonRotate
    • Label for showing loaded PDF-document named "LabelFileName"
    PDFBox need to be referred in the Project Attributes Region:
    Code:
    #AdditionalJar: pdfbox-app-2.0.14.jar
    The file pdfbox-app-2.0.14.jar must be in the additional libraries folder

    The PDFBoxWrapper Class module and a working layout for the PDF-viewer can be obtained from the attached Example project.

    To use the attached example you have to download the pdfbox-app-2.0.14.jar https://pdfbox.apache.org/download.cgi#20x and place it in the additional libraries folder.

    PDFBoxWrapperExampleScreenDump.png
     

    Attached Files:

    Last edited: Mar 4, 2019
  2. Tayfur

    Tayfur Well-Known Member Licensed User

    Great work;Thank you for sharing.
    I didn't see the rotation function.If you add something similar to the following, it will be perfect.
    again thank your for sharing

    Code:
    Public Sub Rotate(angle As Int)
        
    Dim pg As JavaObject = thisDoc.RunMethodJO("getPage",Array(0))
        pg.RunMethodJO(
    "setRotation",Array(angle))
        Repaint(
    1)
    End Sub
     
    Johan Hormaza likes this.
  3. knutf

    knutf Member Licensed User

    With help of the sugestion from Tayfur I have updated the Class module with a Rotation function. It is like this:
    Code:
    Private Sub ButtonRotate_Click
        
    If DocIsLoaded And stopRenderingPages = False Then
            
    Dim page As JavaObject = thisDoc.RunMethodJO("getPage",Array(activePage))
            
    Dim angle As Int = page.RunMethod("getRotation",Null)
            
    If angle > 180 Then
                angle = 
    0
            
    Else
                angle = angle + 
    90
            
    End If
            
    page.RunMethodJO("setRotation",Array(angle))
            Repaint(myZoom)
        
    End If
    End Sub
    I had to do some other changes to get the pages nice placed in the scrollView when rotated.

    Further I have changed the horizontal position of the pages to the middel of the scrollView. (This applies when the zoom level is so that the page width is less than the scrollView Viewport width)

    The PDFBoxWrapperExample.zip in the first post is uppdated to the latest version.
     
    Peter Simpson, DarkMann and Tayfur like this.
  4. knutf

    knutf Member Licensed User

    There was an error in my code that caused the horizontal Scrollbar in the ScrollView not working. (Details in this post) It is now fixed, and The PDFBoxWrapperExample.zip in the first post is uppdated to the latest version.
     
    Tayfur likes this.
  5. sdleidel

    sdleidel Member Licensed User

    Hi,
    is there a way, to fit the width and height, from the PDF to the Pane ?

    Thx
    Sascha
     
  6. knutf

    knutf Member Licensed User

    In the current example it is not possible to fit the PDF document to the ScrollPane width. I have a nearly working example, but struggle finding the ScrollPane "viewport" width. I used this code:
    Code:
    Dim spNative As JavaObject = myScrollPane
    Dim vpWidth As Double = spNative.RunMethodJO("getViewportBounds",Null).RunMethod("getWidth",Null)
    but it does not take into account the size of the vertical scrollbar. Any suggestion?
     
  7. sdleidel

    sdleidel Member Licensed User

    Hi, and this ist not working ?

    Code:
    Dim spNative As JavaObject = myScrollPane
        
    Dim vpWidth As Double = spNative.RunMethodJO("getViewportBounds",Null).RunMethod("getWidth",Null)
        
    Dim vpheight As Double = spNative.RunMethodJO("getViewportBounds",Null).RunMethod("getHeight",Null)
        
    Log (vpWidth)
        
    Log(vpheight)
     
  8. knutf

    knutf Member Licensed User

    I was a bit fast. Contrary to what I wrote, the code in my last post and sdeidel's post take into account the size of the scrollbars.
     
  9. Omar Moreno

    Omar Moreno Member Licensed User

  10. knutf

    knutf Member Licensed User

    Thank you, I appreciate that you like the PDF file viewer.

    It might be a good idea to include printing in the class.

    But I'm sorry I did not have time to look into printing now, as I am a farmer and are busy on the fields in summer and at autumn.
     
    Cableguy likes this.
  11. Omar Moreno

    Omar Moreno Member Licensed User

    Hi.
    Exactly I do not understand much about Java, between the files that I downloaded
    There was an example for java, I extracted a fragment, I pasted it in its code
    and with a button I called to print.

    Boton_Imprimir.PNG

    Thank you.
     
  12. Omar Moreno

    Omar Moreno Member Licensed User

    Hi.
    Someone has happened that in Windows 10 the zoom (image size) of the PDF is not the same as in Windows 7.

    In Windows 10, 100% zoom is seen at 50%.

    :(
     
  13. Omar Moreno

    Omar Moreno Member Licensed User

    Sorry, but my query in post # 12 is incorrect. :oops:

    I noticed that the library detects the old 22 ”monitor with a value of 30DPI, therefore I had to create a function to hammer at least 96DPI before the library use the values of the DPI.

    Code:
    Sub SETDPI(dpi As Float) As Float
        
    '
        Dim MinDPI As Float = 96
        
    '
        If dpi < MinDPI Then
            dpi = MinDPI
        
    End If
        
    '
        Return dpi
        
    '
    End Sub
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice