2011-01-05 87 views
3

嗨我创建了一个PDF文件,其中包含一个图像,我想在创建后打印我的pdf。如果我在内存中使用PDF而不是有文件,然后将其发送给打印机,那么更好......任何想法?如何打印使用iText创建的PDF?

我正在使用iText。检查我的代码:

import com.lowagie.text.Document; 
    import com.lowagie.text.DocumentException; 
    import com.lowagie.text.Image; 
    import com.lowagie.text.PageSize; 
    import com.lowagie.text.Rectangle; 
    import com.lowagie.text.pdf.PdfContentByte; 
    import com.lowagie.text.pdf.PdfPrinterGraphics2D; 
    import com.lowagie.text.pdf.PdfTemplate; 
    import com.lowagie.text.pdf.PdfWriter; 

    import javax.imageio.ImageIO; 

    import java.awt.Color; 
    import java.awt.Graphics2D; 
    import java.awt.Toolkit; 
    import java.awt.image.BufferedImage; 
    import java.io.ByteArrayInputStream; 
    import java.io.File; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.io.InputStream; 


     private boolean exportToPdfThroughPNG(String fileName, float width, float height) throws DocumentException, IOException { 
     logger.debug("[boolean exportToPdfQuick() throws IOException, DocumentException]"); 

     BufferedImage pngFile = createPngFile(); 

     Document document = new Document(); 
     document.setPageSize(new Rectangle(width, height)); 
     PdfWriter.getInstance(document, new FileOutputStream(fileName)); 
     document.open(); 
     Image image = Image.getInstance(Toolkit.getDefaultToolkit().createImage(pngFile.getSource()), Color.WHITE); 
     document.add(image); 
     // If some day anyone wants to put text in the pdf. @Eduardo 
     // document.add(new Paragraph("title of the process")); 
     document.close(); 

     return true; 
    } 

在此先感谢!

+0

这是在Windows上还是在不同的平台上运行? – 2011-01-06 13:48:27

回答

1

您始终可以使用ByteArrayOutputStream而不是FileOutputStream。

有了PDF字节后,它就成了一个正常的“你怎么用Java打印”的问题。许多打印机(或者至少他们的驱动程序)现在都会直接使用PDF,所以在那个时候可以争辩说你已经完成了。 PS:一旦我标记了你的问题“Java”,它会使用“导入”作为关键字等将代码块着色。将来需要记住的东西。

+0

谢谢马克,我工作上很累,终于可以解决这个问题... PS:我没有专家使用这个编辑器!抱歉! – MadMad666 2011-01-07 05:18:38