visulaizar PDF in java application - Embedded - Problem in rendering

Asked

Viewed 87 times

1

Hello,

I need to view PDF’s in my JAVA application. After some research, putting aside the paid components it seemed to me that the best answer would be this:

Displaying pdf in Javafx

The colleague suggests the use of the library [PDF.js][2] running inside a WebView. I was able to view simple PDF’s (where there is only text), but I cannot view PDF’s with images. Can’t seem to load the full file contents.

Some images:

Original example - Using mozila demo (link)

inserir a descrição da imagem aqui

Java application - PDF example with images:

inserir a descrição da imagem aqui

Java Application - Simple PDF Example (text-only):

inserir a descrição da imagem aqui

Code used:

import java.io.File;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.Base64;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.apache.commons.io.FileUtils;

public class BrowserShowPdf extends Application {

    @Override
    public void start(Stage primaryStage) {

        WebView webView = new WebView();
        WebEngine engine = webView.getEngine();
        //Change the path according to yours.
        engine.setJavaScriptEnabled(true);

        File file;
        try {
            file = new File(getClass().getResource("viewer.html").toURI());
            System.out.println("Url ->>>>>" + file.toURI().toURL().toString());
            engine.load(file.toURI().toURL().toString());
        } catch (MalformedURLException ex) {
            Logger.getLogger(BrowserShowPdf.class.getName()).log(Level.SEVERE, null, ex);
        } catch (URISyntaxException ex) {
            Logger.getLogger(BrowserShowPdf.class.getName()).log(Level.SEVERE, null, ex);
        }



        StackPane root = new StackPane();
        root.getChildren().add(webView);

        engine.getLoadWorker()
                .stateProperty()
                .addListener((observable, oldValue, newValue) -> {
                    // this pdf file will be opened on application startup
                    if (newValue == Worker.State.SUCCEEDED) {
                        try {
                            // readFileToByteArray() comes from commons-io library
                            byte[] data = FileUtils.readFileToByteArray(new File("D:\\Font Awesome Cheatsheet.pdf"));
                            String base64 = Base64.getEncoder().encodeToString(data);
                            // call JS function from Java code
                            engine.executeScript("openFileFromBase64('" + base64 + "')");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

        Scene scene = new Scene(root, 900, 800);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

Some help?

Is there any more practical way to view PDF’s within java applications?

Thank you

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.