簡體   English   中英

如何使用PDF.js在WebView(JavaFX)面板中顯示PDF文件?

[英]How can I display a PDF file in a WebView (JavaFX) panel using PDF.js?

我得到了這個( https://stackoverflow.com/a/42040344/3789572 )解決方案,下面的代碼,但是不起作用。 當我按下按鈕時,什么也沒顯示。 您可以更改文件路徑,然后自己嘗試。

你能幫助我嗎?

控制器代碼:

package sample.principal;

import javafx.concurrent.Worker;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import netscape.javascript.JSObject;

import java.io.File;  
import java.net.URL;
import java.util.Base64;
import java.util.ResourceBundle;

import org.apache.commons.io.FileUtils;

public class WebController implements Initializable {

@FXML
private WebView web;

@FXML
private Button btn;

public void initialize(URL location, ResourceBundle resources) {
    WebEngine engine = web.getEngine();
    String url = getClass().getResource("..\\resources\\web\\viewer.html").toExternalForm();

    // connect CSS styles to customize pdf.js appearance
    engine.setUserStyleSheetLocation(getClass().getResource("..\\resources\\web\\viewer.css").toExternalForm());

    engine.setJavaScriptEnabled(true);
    engine.load(url);

    engine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
                // to debug JS code by showing console.log() calls in IDE console
                JSObject window = (JSObject) engine.executeScript("window");
                window.setMember("java", new JSLogListener());
                engine.executeScript("console.log = function(message){ java.log(message); };");

                // 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("C:\\Users\\Felipe\\Documents\\" +
                                "Programação\\Java\\" +
                                "IdeaProjects\\PDFviewerStackOverFlow\\src\\sample\\principal\\teste.pdf"));
                        String base64 = Base64.getEncoder().encodeToString(data);
                        // call JS function from Java code
                        engine.executeScript("openFileFromBase64('" + base64 + "')");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

    // this file will be opened on button click
    btn.setOnAction(actionEvent -> {
        try {
            byte[] data = FileUtils.readFileToByteArray(new File("teste.pdf"));
            String base64 = Base64.getEncoder().encodeToString(data);
            engine.executeScript("openFileFromBase64('" + base64 + "')");
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
}
}

主要代碼:

package sample.principal;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

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

public void start(Stage primaryStage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("PDF test app");
    primaryStage.setScene(new Scene(root, 1280, 576));
    primaryStage.show();
}
}

另一個:

package sample.principal;

public class JSLogListener {
public void log(String text) {
    System.out.println(text);
}
}

謝謝您的幫助。

謝謝。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage stage) {
    stage.setTitle("HTML");
    stage.setWidth(500);
    stage.setHeight(500);
    Scene scene = new Scene(new Group());

    VBox root = new VBox();     

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setContent(browser);
    webEngine.loadContent("<b>asdf</b>");

    root.getChildren().addAll(scrollPane);
    scene.setRoot(root);

    stage.setScene(scene);
    stage.show();
  }

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM