2015-04-23 126 views
-1

我使用iText 1.3将图像文件(gif,png,jpg,bmp)成功转换为pdf。 我无法更改版本,因为我们不能在专业环境中明显更改jar版本。使用iText和Java将图像转换为pdf

我遇到的问题是PDF中图像的大小比图像本身大。我不是在谈论文件大小,而是在原始图像文件和PDF上的缩放设置为100%时图像的大小。 pdf显示比原始图像大20%到30%的图像。

我在做什么错?

public void convertOtherImages2pdf(byte[] in, OutputStream out, String title, String author) throws IOException { 
     Image image = Image.getInstance(in); 
     Rectangle imageSize = new Rectangle(image.width() + 1f, image.height() + 1f); 
     image.scaleAbsolute(image.width(), image.height()); 
     com.lowagie.text.Document document = new com.lowagie.text.Document(imageSize, 0, 0, 0, 0); 
     PdfWriter writer = PdfWriter.getInstance(document, out); 
     document.open(); 
     document.add(image); 
     document.close(); 
     writer.close(); 
    } 
+0

两者都是100%。如果我把它们放在一起,你可以清楚地看到不同之处(或通过alt + tab)。 –

回答

2

您需要通过dpi来缩放图像。

float scale = 72/dpi; 

我不知道这样一个古老的iText具有图像信息,最近的iText的版本有它。

+0

Paulo表示,1.3是古老的(2005年4月),所以请考虑升级,即使您正在生产。我非常确定,切换罐子比使用10年前的罐子更安全。 –

+0

Thansk的小费。在旧版本的iText中,我能够检索所需的所有数据,但不幸的是,它不能正常工作:(。 –

+0

编辑您的问题,向我们展示您的新代码。 –