com.sun.pdfview.PDFFile
时间: 2023-06-21 17:12:13 浏览: 227
com.sun.pdfview.PDFFile 是一个Java程序包中的类,用于读取和操作PDF文件。它提供了一种方便的方式来获取PDF文件的内容,并对其进行操作和呈现。该类可以解析PDF文件中的所有内容,并允许您访问PDF文档的结构树、页面、注释、书签、元数据等。它还提供了一些有用的方法来处理PDF文件,例如搜索、旋转、缩放、裁剪、合并、拆分等。
相关问题
java 实现file 转pdf预览代码
您好!以下是Java实现file转PDF预览的代码:
```
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.PagePanel;
public class PDFConverter {
public static void main(String[] args) {
JFrame frame = new JFrame("File to PDF Preview");
try {
File file = new File("test.pdf");
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
PDFFile pdfFile = new PDFFile(fis);
PDFPrintPage pages = new PDFPrintPage(pdfFile);
frame.add(pages);
frame.pack();
frame.setVisible(true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
static class PDFPrintPage extends JPanel implements Printable {
private static final long serialVersionUID = 1L;
private PDFFile file;
private int pageIndex = 0;
public PDFPrintPage(PDFFile file) {
this.file = file;
setPreferredSize(new Dimension(800, 1000));
}
@Override
public int print(Graphics g, PageFormat format, int pageIndex) {
if (pageIndex >= file.getNumPages()) {
return NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D) g;
PDFPage page = file.getPage(pageIndex + 1);
double pwidth = format.getImageableWidth();
double pheight = format.getImageableHeight();
double aspect = page.getAspectRatio();
double paperAspect = pwidth / pheight;
double scaleFactor = 1.0;
if (aspect > paperAspect) {
// The PDF aspect ratio is wider than the paper aspect
scaleFactor = pwidth / page.getWidth();
} else {
// The PDF aspect ratio is taller than the paper aspect
scaleFactor = pheight / page.getHeight();
}
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
double cx = (format.getImageableX() + format.getWidth()) / 2.0;
double cy = (format.getImageableY() + format.getHeight()) / 2.0;
Rectangle2D rect = page.getBBox();
rect.setRect(rect.getX() * scaleFactor + cx - (page.getWidth() * scaleFactor) / 2.0,
rect.getY() * scaleFactor + cy - (page.getHeight() * scaleFactor) / 2.0,
rect.getWidth() * scaleFactor, rect.getHeight() * scaleFactor);
PagePanel panel = new PagePanel();
panel.showPage(page);
try {
panel.print(g);
} catch (PrinterException e) {
e.printStackTrace();
}
pageIndex++;
return PAGE_EXISTS;
}
}
}
```
希望对您有所帮助!
阅读全文