用java打印pdf

时间:2013-06-18 14:29:54

标签: java pdf printing

我尝试使用以下代码打印pdf:

package imprimir;

import java.io.*;
import java.awt.print.*;
import java.awt.print.PrinterJob.*;
import java.awt.print.PageFormat.*;

public class Imprimir {

    static public void main(String[] args) {
        PrinterJob job = PrinterJob.getPrinterJob();
        PageFormat pf = job.defaultPage();
        Paper paper = new Paper();
        paper.setSize(612.0, 832.0);
        double margin = 10;
        paper.setImageableArea(margin, margin, paper.getWidth() - margin, paper.getHeight() - margin);
        pf.setPaper(paper);
        pf.setOrientation(PageFormat.LANDSCAPE);
        job.setPrintable(new ObjetoDeImpresion(), pf);
        job.setJobName("funciona?");
        try {
            job.print();
        } catch (PrinterException e) {
            System.out.println(e);
        }
    }
}


package imprimir;

import java.awt.*;
import java.awt.print.*;

public class ObjetoDeImpresion implements Printable {

    public int print(Graphics g, PageFormat f, int pageIndex) {
        if (pageIndex == 0) {
            g.drawString("Hola ivan", 100, 200);
            return PAGE_EXISTS;
        } else {
            return NO_SUCH_PAGE;
        }
    }
}

以及互联网上的其他不同方式,但是我尝试过的所有方式,当我打印文档时,打印奇数和字母,如下所示:

% PDF ||1.6
endobobj <</linerrized 1/L 20597/O 40/E 14115/N 1/T ............
xref
37 34
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
0000000013 00000 n
...
..
..
trailer
<</Size...... 

有人可以帮我打印我的文件吗?

3 个答案:

答案 0 :(得分:4)

我认为来自Apache的PDFBox更适合您的需求(http://pdfbox.apache.org/)。

以下是它如何适合您的代码:

static public void main(String[] args) {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    Paper paper = new Paper();
    paper.setSize(612.0, 832.0);
    double margin = 10;
    paper.setImageableArea(margin, margin, paper.getWidth() - margin, paper.getHeight() - margin);
    pf.setPaper(paper);
    pf.setOrientation(PageFormat.LANDSCAPE);

    // PDFBox
    PDDocument document = PDDocument.load("yourfile.pdf");
    job.setPageable(new PDPageable(document, job));

    job.setJobName("funciona?");
    try {
        job.print();
    } catch (PrinterException e) {
        System.out.println(e);
    }
}

如果您查看org.apache.pdfbox.PrintPDF的来源,可以找到更多相关信息。

答案 1 :(得分:0)

最好的选择是使用iTEXT

iText Web Link

<强>教程

Tutorial # 01

答案 2 :(得分:0)

我们也尝试过PDFBox,PDFView和IText,但最适合我们的是使用系统ghostscript将PDF渲染成图像 - 否则在我们的PDF中有几个图像和表单字段,就会呈现出来的东西不完美。

首先将pdf写入临时文件,然后调用gs:

        String command;
    if (System.getProperty("os.name").toLowerCase().contains("windows"))
    {
        command = "gswin32";
    }
    else
    {
        command = "gs";
    }

    String absolutePath = pngFile.getAbsolutePath();
    command = command + " -q -dSAFER -dBATCH -dNOPAUSE -sDEVICE="
                + color.name()
                + " -dGraphicsAlphaBits=4 -dTextAlphaBits=4 -dFirstPage="
                + pageNo + " -dLastPage=" + pageNo + " -r" + dpi
                + " -sOutputFile=" + absolutePath + " "
                + pdfFile.getAbsolutePath();
    System.out.println(command);
    Process p = Runtime.getRuntime().exec(command);
    boolean success = false;
    for (int i = 0; i < 1200; i++) //wait for completion
    {
        try
        {
            p.exitValue();
            success = true;
            break;
        }
        catch (Exception e)
        {
            logger.trace(e.getMessage());
        }
        Thread.currentThread();
        Thread.sleep(200);
    }
    if (!success)
    {
        p.destroy();
    }