2012-04-19 85 views
1

我有一个SWT图像我想使用iText API将此图像导出为pdf文件。 我已经尝试将此映像保存在磁盘上,然后使用图像路径将其导出为pdf,这需要大量时间来生成PDF。 我也尝试将SWT图像转换为AWT图像,然后将其导出到 pdf中,此方法需要更多时间来生成pdf。 我一直在尝试另一种方法是图像的原始数据转换成使用ImageLoader的对象 JPEG byteArrayOutputStream如下图所示:使用Java iText API将SWT图像导出为PDF

ImageLoader tempLoader = new ImageLoader(); 
tempLoader.data = new ImageData[] { 
    image.getImageData()      
}; 
ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
tempLoader.save(bos, SWT.IMAGE_JPEG); 

我现在用的这个ByteArrayOutputStream作为输入

OutputStream outStream = new FileOutputStream(selectedPathAndName); 
Document document = new Document();  
document.setMargins(0,0,0,0); 
document.setPageSize(new Rectangle(0,0,width,height)); 
PdfWriter.getInstance(document, outStream); 
document.open(); 
com.itextpdf.text.Image pdfImage = com.itextpdf.text.Image.getInstance(bos.toByteArray()); 
document.add(pdfImage); 
document.close(); 

这生成我已经设置的宽度和高度的PDF文件,但该页面似乎是空的。 任何建议或任何其他方法是最受欢迎的。

谢谢

回答

1

它看起来你的页面大小是零,尝试像A4将它们设置为东西构造。

Document document = new Document(PageSize.A4, 50, 50, 50, 50);