2013-07-12 133 views
1

当我通过Java代码访问时,图像无法在我的PDF(iText)中正确显示。 它部分显示图像正面显示的前半部分,其余半部分显示图像顶部有很多线条。 (当其他文本快速显示时,图像似乎下载速度非常慢)。部分显示的iText PDF图像

我使用iTextPdf版本5.4.0 jar文件,并通过URL访问图像(获取图像URL) 在我的java代码中。

请让我知道为什么会发生这种情况。如果您需要任何其他信息,请让我知道,我可以提供。

在此先感谢您的帮助。

+1

应该有谁知道为什么会发生这种情况?你没有应用源代码,没有样本PDF,没有什么可走的。 – mkl

+0

我们已经使用5.4.1中的GIF解决了类似的问题:http://itextpdf.com/history/?branch=54&node=541请参阅'GifImage':修复远程加载的图像。存储在远程服务器上的GIF并不总是完全加载(当然,如果你不是在讨论GIF图像,很难猜测出了什么问题)。 –

+0

谢谢。将尝试。但是,当我尝试上传示例PDF Stackoverflow说,我需要至少10声望发布图像。我怎样才能上传样本PDF? – user1650179

回答

0

我也面临同样的问题,后来我们解决了。请找到下面的代码,希望它对你有帮助。

HTML文件


<html> 
<body> 
<font color="green">Test</font><br/> 
<table> 
<tr><td><img src="Desert.jpg" height="300" width="300"/></td></tr> 
</table> 
</body> 
</html> 

Java文件


class PageWithRectangle extends PdfPageEventHelper 
{ 
     public void onEndPage(PdfWriter writer, Document document) 
     { 
      PdfContentByte cb = writer.getDirectContent(); 
      Rectangle pageSize = writer.getPageSize(); 
      cb.rectangle(pageSize.getLeft() + 3, pageSize.getBottom() + 3, 
      pageSize.getWidth() - 6, pageSize.getHeight() - 6); 
      cb.stroke(); 
     } 
} 
public class pdfTest { 
    private static String getUrlSource(String url) throws IOException { 
     URL webpage = new URL(url); 
     URLConnection yc = webpage.openConnection(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(
       yc.getInputStream(), "UTF-8")); 
     String inputLine; 

     StringBuilder a = new StringBuilder(); 
     while ((inputLine = in.readLine()) != null) 
     { 
      a.append(inputLine); 
      System.out.println(inputLine); 
      } 
     in.close(); 
     return a.toString(); 
    } 
    public static void main(String[] args) { 
     try { 
      File baseDir = new File("."); 
      File outDir = new File(baseDir, "out"); 
      outDir.mkdirs(); 
      String k = getUrlSource("file:\\C:\\test.html"); 
      OutputStream file = new FileOutputStream(new File(outDir+"/Test.pdf")); 
      Document document = new Document(); 
      PdfWriter writer = PdfWriter.getInstance(document, file); 
      writer.setPageEvent(new PageWithRectangle()); 
      document.open(); 
      HTMLWorker htmlWorker = new HTMLWorker(document); 
      htmlWorker.parse(new StringReader(k)); 
      document.close(); 
      file.close(); 
      System.out.println("\nSuccess"); 
     } catch (Exception e) { 

      e.printStackTrace(); 
     } 
    } 

    } 

我的旧的HTML代码(给出了错误的PDF,同时通过Java生成)

<html> 
<body> 
<font color="green">Test</font><br/> 
<img src="Desert.jpg" height="300" width="300"/> 
</body> 
</html> 

解决方案:给图像标签下表标签

问候, 普利文

0

我也经历了一个利用iText 5.5.5这个问题,发现影响GIF的alpha通道设置问题。删除alpha或尝试保存为jpg。这对我有效。