[英]How to launch a pdf file embedded in web browser with Reading mode and JAVA
[英]How to achieve a web browser like pdf rendering in java?
我目前正在嘗試做一個pdf編寫器應用程序,最終用戶可以在pdf文件上繪制(線條/形狀或文本)。 我正在使用pdfbox將pdf文件顯示到java jwing組件(如jPanel)。 pdfbox的問題在於,重新打包pdf文件的速度很慢,因為它使用bufferedImage然后繪制到jPanel。 該文件的分辨率也很差,我無法放大/縮小文件。
我想實現一個Web瀏覽器,例如顯示pdf文件。
謝謝。
使用pdfbox將pdf轉換為bufferedImage的代碼。
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
public class Parse {
private static File file;
private int imageWidth;
private int imageHeight;
public Parse(File file) {
this.file = file;
}
public BufferedImage getPage(int pageIndex) throws IOException {
PDDocument pdf = PDDocument.load(file);
Float realWidth = new Float(pdf.getPage(0).getMediaBox().getWidth());
Float realHeight = new
Float(pdf.getPage(0).getMediaBox().getHeight());
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Double ratio = .8;
imageHeight = (int) (screenSize.getHeight() * ratio);
imageWidth = (int) ((imageHeight * realWidth) / realHeight);
PDFRenderer renderer = new PDFRenderer(pdf);
BufferedImage renderImage = renderer.renderImage(pageIndex, 1, ImageType.RGB);
pdf.close();
return renderImage;
}
public int getImageWidth() {
return imageWidth;
}
public void setImageWidth(int imageWidth) {
this.imageWidth = imageWidth;
}
public int getImageHeight() {
return imageHeight;
}
public void setImageHeight(int imageHeight) {
this.imageHeight = imageHeight;
}
}
用於將圖像繪制到JPanel的代碼。
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class PDFPanel extends JPanel {
private Image image;
private int width;
private int height;
public PDFPanel(Image image, int width, int height) {
this.image = image;
this.width = width;
this.height = height;
setBorder(new EmptyBorder(0, 0, 0, 0));
setSize(width, height);
}
@Override
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
graphics2D.drawImage(image, 0, 0, width, height, null);
}
}
這是一些根據在問題PDFBOX-3359中提交的代碼在帶有PDFBox 2.0的JPanel中顯示PDF的代碼。 它不會縮放,但可以更改大小。 窗口越大,它仍然會變慢,甚至變慢。
public class PDFBox3359PanelTestEnhanced
{
private static JPanel getTestPanel()
{
final PDDocument doc;
try
{
doc = PDDocument.load(new File("XXXXX.pdf"));
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
final PDFRenderer renderer = new PDFRenderer(doc);
JPanel panel = new JPanel()
{
int i;
@Override
protected void paintComponent(Graphics g)
{
try
{
g.setColor(Color.red);
g.fillRect(0, 0, getWidth(), getHeight());
PDPage page = doc.getPage(0);
PDRectangle cropBox = page.getCropBox();
boolean rot = false;
if (page.getRotation() == 90 || page.getRotation() == 270)
{
rot = true;
}
// https://stackoverflow.com/questions/1106339/resize-image-to-fit-in-bounding-box
float imgWidth = rot ? cropBox.getHeight() : cropBox.getWidth();
float imgHeight = rot ? cropBox.getWidth() : cropBox.getHeight();
float xf = getWidth() / imgWidth;
float yf = getHeight() / imgHeight;
float scale = Math.min(xf, yf);
if (yf < xf)
{
g.translate((int) ((getWidth() - imgWidth * yf) / 2), 0);
}
else
{
g.translate(0, (int) ((getHeight() - imgHeight * xf) / 2));
}
renderer.renderPageToGraphics(0, (Graphics2D) g, scale);
}
catch (IOException e)
{
e.printStackTrace();
}
}
};
return panel;
}
public static void main(String[] args) throws Exception
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(getTestPanel());
frame.pack();
frame.setSize(600, 400);
Dimension paneSize = frame.getSize();
Dimension screenSize = frame.getToolkit().getScreenSize();
frame.setLocation((screenSize.width - paneSize.width) / 2, (screenSize.height - paneSize.height) / 2);
frame.setTitle("Test");
frame.setVisible(true);
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.