使用JAVA在我的计算机中打印PDF

使用JAVA在我的计算机中打印PDF,java,pdf,Java,Pdf,我的电脑里有10000多个PDF文件。我想编写一个Java应用程序,为PDF找到合适的名称并打印出来 有人能帮我打印吗 谢谢你的回答 我试过这个 import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.print.Book; import java.awt.print.PageFormat; import java.awt.print.Paper; i

我的电脑里有10000多个PDF文件。我想编写一个Java应用程序,为PDF找到合适的名称并打印出来

有人能帮我打印吗

谢谢你的回答 我试过这个

    import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.print.Book;
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.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.PDFRenderer;

 /**
 * Converts the PDF content into printable format
 */
 public class PrintPdf {

private PrinterJob pjob = null;

 public static void main(String[] args) 
 throws IOException, PrinterException    {
    if (args.length != 1) {
        System.err.println("The first parameter must have 
the location of the PDF file to be printed");
    }
    System.out.println("Printing: " + args[0]);
    // Create a PDFFile from a File reference
    FileInputStream fis = new FileInputStream(args[0]);
    PrintPdf printPDFFile = new PrintPdf(fis, "Test Print PDF");
    printPDFFile.print();
}

/**
 * Constructs the print job based on the input stream
 * 
 * @param inputStream
 * @param jobName
 * @throws IOException
 * @throws PrinterException
 */
public PrintPdf(InputStream inputStream, String jobName) 
 throws IOException, PrinterException {
    byte[] pdfContent = new byte[inputStream.available()];
    inputStream.read(pdfContent, 0, inputStream.available());
    initialize(pdfContent, jobName);
 }

/**
 * Constructs the print job based on the byte array content
 * 
 * @param content
 * @param jobName
 * @throws IOException
 * @throws PrinterException
 */
public PrintPdf(byte[] content, String jobName)
throws IOException,   PrinterException {
    initialize(content, jobName);
}

/**
 * Initializes the job
 * 
 * @param pdfContent
 * @param jobName
 * @throws IOException
 * @throws PrinterException
 */
private void initialize(byte[] pdfContent,  
 String jobName) throws     IOException, PrinterException {
    ByteBuffer bb = ByteBuffer.wrap(pdfContent);
    // Create PDF Print Page
    PDFFile pdfFile = new PDFFile(bb);
    PDFPrintPage pages = new PDFPrintPage(pdfFile);

    // Create Print Job
    pjob = PrinterJob.getPrinterJob();
    PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
    pjob.setJobName(jobName);
    Book book = new Book();
    book.append(pages, pf, pdfFile.getNumPages());
    pjob.setPageable(book);

    // to remove margins
    Paper paper = new Paper();
    paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
    pf.setPaper(paper);
}

public void print() throws PrinterException {
    // Send print job to default printer
    pjob.print();
 }
 }

  /**
 * Class that actually converts the PDF file into Printable format
*/
 class PDFPrintPage implements Printable {

private PDFFile file;

PDFPrintPage(PDFFile file) {
    this.file = file;
}

public int print(Graphics g, PageFormat format, int index) 
throws   PrinterException {
    int pagenum = index + 1;
    if ((pagenum >= 1) && (pagenum <= file.getNumPages())) {
        Graphics2D g2 = (Graphics2D) g;
        PDFPage page = file.getPage(pagenum);

        // fit the PDFPage into the printing area
        Rectangle imageArea = new Rectangle((int)
 format.getImageableX(),    (int) format.getImageableY(), 
                (int) format.getImageableWidth(),
(int)      format.getImageableHeight());
        g2.translate(0, 0);
        PDFRenderer pgs = new PDFRenderer(page, g2, imageArea, null, null);
        try {
            page.waitForFinish();
            pgs.run();
        } catch (InterruptedException ie) {
            // nothing to do
        }
        return PAGE_EXISTS;
    } else {
        return NO_SUCH_PAGE;
    }
  }
 }
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.Rectangle;
导入java.awt.print.Book;
导入java.awt.print.PageFormat;
导入java.awt.print.Paper;
导入java.awt.print.Printable;
导入java.awt.print.PrinterException;
导入java.awt.print.PrinterJob;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.nio.ByteBuffer;
导入com.sun.pdfview.PDFFile;
导入com.sun.pdfview.PDFPage;
导入com.sun.pdfview.PDFRenderer;
/**
*将PDF内容转换为可打印格式
*/
公共类打印PDF{
private PrinterJob pjob=null;
公共静态void main(字符串[]args)
抛出IOException、PrinterException{
如果(args.length!=1){
System.err.println(“第一个参数必须
要打印的PDF文件的位置”);
}
System.out.println(“打印:+args[0]);
//从文件引用创建PDFFile
FileInputStream fis=新的FileInputStream(args[0]);
PrintPdf printpdfile=新的PrintPdf(fis,“测试打印PDF”);
printpdfile.print();
}
/**
*基于输入流构造打印作业
* 
*@param inputStream
*@param jobName
*@抛出异常
*@PrinterException
*/
公共打印PDF(InputStream InputStream,字符串jobName)
抛出IOException、PrinterException{
byte[]pdfContent=新字节[inputStream.available()];
读取(pdfContent,0,inputStream.available());
初始化(pdfContent,jobName);
}
/**
*基于字节数组内容构造打印作业
* 
*@param内容
*@param jobName
*@抛出异常
*@PrinterException
*/
公共打印PDF(字节[]内容,字符串jobName)
抛出IOException、PrinterException{
初始化(内容、作业名称);
}
/**
*初始化作业
* 
*@param pdfContent
*@param jobName
*@抛出异常
*@PrinterException
*/
私有无效初始化(字节[]pdfContent,
字符串jobName)引发IOException、PrinterException{
ByteBuffer bb=ByteBuffer.wrap(pdfContent);
//创建PDF打印页面
Pdfile Pdfile=新的Pdfile(bb);
PDFPrintPage pages=新的PDFPrintPage(pdfFile);
//创建打印作业
pjob=PrinterJob.getPrinterJob();
PageFormat pf=PrinterJob.getPrinterJob().defaultPage();
pjob.setJobName(jobName);
书=新书();
book.append(pages、pf、pdfFile.getNumPages());
pjob.setPageable(book);
//删除页边距
纸张=新纸张();
paper.setImageableArea(0,0,paper.getWidth(),paper.getHeight());
设定纸(纸);
}
public void print()引发PrinterException{
//将打印作业发送到默认打印机
pjob.print();
}
}
/**
*类,该类实际将PDF文件转换为可打印格式
*/
类PDFPrintPage实现可打印{
私有PDFFile文件;
PDFPrintPage(PDFFile文件){
this.file=文件;
}
公共整数打印(图形g、页面格式、整数索引)
抛出PrinterException{
int pagenum=索引+1;

如果((pagenum>=1)&&(pagenum@Raptor)我已经编辑了帖子,你应该在这里发布代码,而不是外部链接。我更新Post@Raptor现在可以了。