java 用java打印pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17171573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
print pdf with java
提问by Ivan
I have tried to print pdf with this code:
我曾尝试使用以下代码打印 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;
}
}
}
and also from other different ways there on the internet, but with all the ways I've tried, when I print the document, print odd numbers and letters, like this:
以及互联网上的其他不同方式,但通过我尝试过的所有方式,当我打印文档时,打印奇数和字母,如下所示:
% 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......
Someone can help me achieve print my document?
有人可以帮我实现打印我的文档吗?
回答by Rapha?l
I think PDFBox from Apache better suit your need (http://pdfbox.apache.org/).
我认为来自 Apache 的 PDFBox 更适合您的需要(http://pdfbox.apache.org/)。
Here is how it can fit inside your code:
以下是它如何适合您的代码:
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);
}
}
You can find more info about this if you look at the source of org.apache.pdfbox.PrintPDF.
如果您查看 org.apache.pdfbox.PrintPDF 的来源,您可以找到有关此的更多信息。
回答by Makky
回答by Michael Capper
We have tried PDFBox too, also PDFView and IText, but what worked best for us was using the systems ghostscript to render the PDF into an image - otherwise in our PDF with a couple of images and form fields, things would get rendered not perfectly.
我们也尝试过 PDFBox,还有 PDFView 和 IText,但对我们来说最有效的是使用系统 ghostscript 将 PDF 渲染成图像——否则在我们的 PDF 中,有几个图像和表单字段,事情会变得不完美。
First write your pdf to an temporary file, then call gs:
首先将您的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();
}