2012-04-19 43 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);