本文整理汇总了Java中com.sun.pdfview.PDFFile类的典型用法代码示例。如果您正苦于以下问题:Java PDFFile类的具体用法?Java PDFFile怎么用?Java PDFFile使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PDFFile类属于com.sun.pdfview包,在下文中一共展示了PDFFile类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readPdfAsImages
import com.sun.pdfview.PDFFile; //导入依赖的package包/类
public static List<BufferedImage> readPdfAsImages(File pdfFile) {
try {
RandomAccessFile raf = new RandomAccessFile(pdfFile, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdf = new PDFFile(buf);
List<BufferedImage> images = new ArrayList<BufferedImage>();
for (int pageNumber = 1; pageNumber <= pdf.getNumPages(); ++pageNumber) {
images.add(readPage(pdf, pageNumber));
}
raf.close();
return images;
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
示例2: readPdfPageAsImage
import com.sun.pdfview.PDFFile; //导入依赖的package包/类
/**
*
* @param pdfFile
* Path to the pdf file.
* @param pageNumber
* One-based page number to read
* @return
*/
public static BufferedImage readPdfPageAsImage(File pdfFile, int pageNumber) {
if (pageNumber < 1)
throw new RuntimeException("page numbering starts with 1; '" + pageNumber + "' given");
try {
RandomAccessFile raf = new RandomAccessFile(pdfFile, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdf = new PDFFile(buf);
BufferedImage image = readPage(pdf, pageNumber);
raf.close();
return image;
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
示例3: toImage
import com.sun.pdfview.PDFFile; //导入依赖的package包/类
@Override
public Image toImage(byte[] input, int pageNumber) throws IOException {
File file = File.createTempFile("pdf2img", "pdf");
try{
copy(input,file);
PDFFile pdffile = toPDFFile(file);
// draw a single page to an image
PDFPage page = pdffile.getPage(pageNumber);
return toImage(page,(int)page.getBBox().getWidth(),(int)page.getBBox().getHeight(),false);
}
finally{
deleteEL(file);
}
}
示例4: writeImages
import com.sun.pdfview.PDFFile; //导入依赖的package包/类
@Override
public void writeImages(byte[] input, Set pages, Resource outputDirectory,
String prefix, String format, int scale, boolean overwrite,
boolean goodQuality, boolean transparent) throws PageException,
IOException {
File file = File.createTempFile("pdf2img", "pdf");
try{
copy(input,file);
PDFFile pdf = toPDFFile(file);
Resource res;
int count = pdf.getNumPages();
for(int page=1;page<=count;page++) {
if(pages!=null && !pages.contains(Integer.valueOf(page)))continue;
res=createDestinationResource(outputDirectory,prefix,page,format,overwrite);
//res=outputDirectory.getRealResource(prefix+"_page_"+page+"."+format);
writeImage(pdf,page,res,format,scale,overwrite,goodQuality, transparent);
}
}
finally{
deleteEL(file);
}
}
示例5: writeImage
import com.sun.pdfview.PDFFile; //导入依赖的package包/类
private static void writeImage(PDFFile pdf, int pageNumber, Resource destination,String format, int scale,
boolean overwrite, boolean goodQuality, boolean transparent) throws PageException, IOException {
PDFPage page = pdf.getPage(pageNumber);
if(scale<1) throw new ExpressionException("scale ["+scale+"] should be at least 1");
int width = (int)page.getBBox().getWidth();
int height = (int)page.getBBox().getHeight();
if(scale!=100){
double s=(scale)/100d;
width= (int)((width)*s);
height= (int)((height)*s);
}
Image img=toImage(page, width, height,transparent);
img.writeOut(destination,format, overwrite, 1f);
}
示例6: readThing
import com.sun.pdfview.PDFFile; //导入依赖的package包/类
/**
* get the next postscript "word". This is basically the next
* non-whitespace block between two whitespace delimiters.
* This means that something like " [2 4 53]" will produce
* three items, while " [2 4 56 ]" will produce four.
*/
public String readThing() {
// skip whitespace
// System.out.println("PAParser: whitespace: \"");
while (PDFFile.isWhiteSpace(data[loc])) {
// System.out.print (new String(data, loc, 1));
loc++;
}
// System.out.print("\": thing: ");
// read thing
int start = loc;
while (!PDFFile.isWhiteSpace(data[loc])) {
loc++;
if (!PDFFile.isRegularCharacter(data[loc])) {
break; // leave with the delimiter included
}
}
String s = new String(data, start, loc - start);
// System.out.println(": Read: "+s);
return s;
}
示例7: ThumbPanel
import com.sun.pdfview.PDFFile; //导入依赖的package包/类
/**
* Creates a new ThumbPanel based on a PDFFile. The file may be null.
* Automatically starts rendering thumbnails for that file.
*/
public ThumbPanel(PDFFile file) {
super();
this.file = file;
if (file != null) {
int count = file.getNumPages();
images = new Image[count];
xloc = new int[count];
setPreferredSize(new Dimension(defaultWidth, 200));
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
handleClick(evt.getX(), evt.getY());
}
});
anim = new Thread(this);
anim.setName(getClass().getName());
anim.start();
} else {
images = new Image[0];
setPreferredSize(new Dimension(defaultWidth, 200));
}
}
示例8: initialize
import com.sun.pdfview.PDFFile; //导入依赖的package包/类
private void initialize() throws FileNotFoundException, IOException, MalformedURLException, URISyntaxException, Exception {
pdfPanel.addMouseListener(actionListener);
pdfPanel.addMouseMotionListener(actionListener);
pdfPanel.addKeyListener(actionListener);
pdfPanel.setComponentPopupMenu(menu);
byte[] pdfBytes = null;
if(DataURL.isDataURL(source)) {
pdfBytes = new DataURL(source).getData();
}
else {
URL sourceURL = new URL(source);
if("file".equalsIgnoreCase(sourceURL.getProtocol())) {
pdfBytes = Files.getByteArray(new File(sourceURL.toURI()));
}
else {
pdfBytes = IObox.fetchUrl(sourceURL);
}
}
if(pdfBytes != null) {
pdfFile = new PDFFile(ByteBuffer.wrap(pdfBytes));
if(pdfFile != null) {
pageCount = pdfFile.getNumPages();
if(pageCount > 0) {
setPageText.invoke(1, pageCount);
pdfPanel.changePage(pdfFile.getPage(0));
}
else {
throw new Exception("PDF has no pages at all. Can't view.");
}
}
}
else {
throw new Exception("Can't read data out of locator resource.");
}
}
示例9: readPDF
import com.sun.pdfview.PDFFile; //导入依赖的package包/类
public static Image readPDF(File f) {
try {
RandomAccessFile raf = new RandomAccessFile(f, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdf = new PDFFile(buf);
PDFPage page = pdf.getPage(0);
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
Image image = page.getImage(rect.width, rect.height, rect, null, true, true);
raf.close();
return image;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
示例10: initialize
import com.sun.pdfview.PDFFile; //导入依赖的package包/类
private void initialize(byte[] pdfContent, String jobName,String nomeImpressora) throws IOException, PrinterException {
ByteBuffer bb = ByteBuffer.wrap(pdfContent);
PDFFile pdfFile = new PDFFile(bb);
PDFPrintPage pages = new PDFPrintPage(pdfFile);
PrintService[] pservices = PrinterJob.lookupPrintServices();
System.out.println(pservices.length);
if (pservices.length > 0) {
for (PrintService ps : pservices) {
System.out.println("Impressora Encontrada: " + ps.getName());
if (ps.getName().contains(nomeImpressora)) {
System.out.println("Impressora Selecionada: " + nomeImpressora);
impressora = ps;
break;
}
}
}
if (impressora != null) {
pjob = PrinterJob.getPrinterJob();
pjob.setPrintService(impressora);
PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
pjob.setJobName(jobName);
Book book = new Book();
book.append(pages, pf, pdfFile.getNumPages());
pjob.setPageable(book);
Paper paper = new Paper();
paper.setSize(getWidth, getHeight);
paper.setImageableArea(margin, (int) margin / 4, getWidth - margin * 2, getHeight - margin * 2);
//paper.setImageableArea(margin,margin, paper.getWidth()-margin*2,paper.getHeight()-margin*2);
pf.setOrientation(orientacao);
pf.setPaper(paper);
}
}
示例11: openFile
import com.sun.pdfview.PDFFile; //导入依赖的package包/类
public void openFile(File file) throws IOException {
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer bb = ByteBuffer.NEW(channel.map(
FileChannel.MapMode.READ_ONLY, 0, channel.size()));
mPdfFile = new PDFFile(bb);
}
示例12: PrintablePDF
import com.sun.pdfview.PDFFile; //导入依赖的package包/类
/**
* Create a new Printable pdf
* @param pdf The PDFFile to print
* @param associatedFile A randomAccessFile to close when this class is finalized (or null)
* @param a page format (or null for default)
*/
public PrintablePDF(PDFFile pdf, RandomAccessFile associatedFile, PageFormat pageFormat) {
this.pdf = pdf;
this.associatedFile = associatedFile;
// create a new print job
this.printJob = PrinterJob.getPrinterJob();
// use the default page if we have a null format
this.pageFormat = (pageFormat == null) ? printJob.defaultPage() : pageFormat;
printJob.setPageable(this);
printJob.setJobName("Tellervo Report");
}
示例13: fromFile
import com.sun.pdfview.PDFFile; //导入依赖的package包/类
/**
* Create a new printable pdf from the given file
* @param pdfFile
* @return a PrintablePDF object
* @throws IOException
*/
public static PrintablePDF fromFile(File pdfFile) throws IOException {
// open the file as a random access file
RandomAccessFile file = new RandomAccessFile(pdfFile, "r");
// get the file as a read-only byte array
ByteBuffer pdfBytes = file.getChannel().map(FileChannel.MapMode.READ_ONLY,
0, file.getChannel().size());
return new PrintablePDF(new PDFFile(pdfBytes), file);
}
示例14: numPagesInPdf
import com.sun.pdfview.PDFFile; //导入依赖的package包/类
public static int numPagesInPdf(File pdfFile) {
try {
RandomAccessFile raf = new RandomAccessFile(pdfFile, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdf = new PDFFile(buf);
int numPages = pdf.getNumPages();
raf.close();
return numPages;
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
示例15: readPage
import com.sun.pdfview.PDFFile; //导入依赖的package包/类
private static BufferedImage readPage(PDFFile pdf, int pageNumber) {
double scale = 2.5; // because otherwise the image comes out really tiny
PDFPage page = pdf.getPage(pageNumber);
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
BufferedImage bufferedImage = new BufferedImage((int)(rect.width * scale), (int)(rect.height * scale), BufferedImage.TYPE_INT_RGB);
Image image = page.getImage((int)(rect.width * scale), (int)(rect.height * scale), rect, null, true, true);
Graphics2D bufImageGraphics = bufferedImage.createGraphics();
bufImageGraphics.drawImage(image, 0, 0, null);
return bufferedImage;
}