gpt4 book ai didi

java - 如何用java实现像pdf这样的网页浏览器渲染?

转载 作者:行者123 更新时间:2023-12-02 02:27:23 25 4
gpt4 key购买 nike

我目前正在尝试制作一个 pdf 编写器应用程序,最终用户可以在 pdf 文件上绘制(线条/形状或文本)。我正在使用 pdfbox 将 pdf 文件显示到 jPanel 等 java swing 组件。 pdfbox 的问题是它渲染 pdf 文件的速度很慢,因为它使用 bufferedImage 然后绘制到 jPanel。该文件的分辨率也很差,我无法放大/缩小该文件。

我想实现一个类似于显示我的 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 2.0 在 JPanel 中显示 PDF 的代码,基于问题 PDFBOX-3359 中提交的代码。它不能缩放,但可以改变大小。它仍然会很慢,而且窗口越大,速度就越慢。

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);
}
});
}
}

关于java - 如何用java实现像pdf这样的网页浏览器渲染?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57246100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com