2012-12-28 82 views
0

我正尝试用图像创建标题;标题被添加,但图像在标题中缺失。我的应用程序部署在Oracle Weblogic服务器上(使用Java EE和Hibernate)。我添加到标题的图像不会出现

我试图创建像这样的图像。 getImage(seasonalFilter.getPictureFileId()).getAbsolutePath()。图像路径是这样的:/tmp/6461346546165461313_65464.jpg

请注意,我想在下添加文本图片的标题(每页)。

public File convertHtmlToPdf(String JSONString, ExportQueryTypeDTO queryType, String htmlText, ExportTypeDTO type) throws VedStatException { 
    try { 
     File retFile = null; 
     FilterDTO filter = null; 
     HashMap<Object, Object> properties = new HashMap<Object, Object>(queryType.getHashMap()); 
     filter = JSONCoder.decodeSeasonalFilterDTO(JSONString); 
     DateFormat formatter = new SimpleDateFormat("yyyy_MM_dd__HH_mm"); 
     //logger.debug("<<<<<< HTML TEXT: " + htmlText + " >>>>>>>>>>>>>>>>"); 
     StringBuilder tmpFileName = new StringBuilder(); 
     tmpFileName.append(formatter.format(new Date())); 
     retFile = File.createTempFile(tmpFileName.toString(), type.getSuffix()); 
     OutputStream out = new FileOutputStream(retFile); 
     com.lowagie.text.Document document = new com.lowagie.text.Document(com.lowagie.text.PageSize.LETTER); 
     com.lowagie.text.pdf.PdfWriter pdfWriter = com.lowagie.text.pdf.PdfWriter.getInstance(document, out); 
     document.open(); 
     com.lowagie.text.html.simpleparser.HTMLWorker htmlWorker = new com.lowagie.text.html.simpleparser.HTMLWorker(document); 
     String str = htmlText.replaceAll("ű", "&ucirc;").replaceAll("ő", "&otilde;").replaceAll("Ő", "&Otilde;").replaceAll("Ű", "&Ucirc;"); 
     htmlWorker.parse(new StringReader(str)); 
     if (filter instanceof SeasonalFilterDTO) { 
     SeasonalFilterDTO seasonalFilter = (SeasonalFilterDTO) filter; 
     if (seasonalFilter.getPictureFileId() != null) { 
      logger.debug("Image absolutePath: " + getImage(seasonalFilter.getPictureFileId()).getAbsolutePath()); 
      Image logo = Image.getInstance(getImage(seasonalFilter.getPictureFileId()).getAbsolutePath()); 
      logo.setAlignment(Image.MIDDLE); 
      logo.setAbsolutePosition(0, 0); 
      logo.scalePercent(100); 
      Chunk chunk = new Chunk(logo, 0, 0); 
      HeaderFooter header = new HeaderFooter(new Phrase(chunk), true); 
      header.setBorder(Rectangle.NO_BORDER); 
      document.setHeader(header); 
     } 
     } 
     document.close(); 

     return retFile; 
    } catch (Exception e) { 
     throw new VedStatException(e); 
    } 

    } 
+0

请考虑添加一个样本PDF缺少图像。由于您使用的是古老的iText版本(仍在com.lowagie包结构中),因此在当前iText环境中运行时,代码可能不会再现问题。如果您提供了PDF,我们可以检查图像是否在其中,仅仅是看不见的,或者毕竟没有图像。 – mkl

回答

1

我真的不喜欢你的虚假指控“每教程基于C:\ imagelocation \ dsadsa.jpg”

我的两本书和许多教程关于iText的和我的作者知道你说的话没有任何意义。看看我的名字:“Bruno Lowagie。”您在代码中使用我的名称,所以当我说你完全错误时请相信我。

而不是HTMLWorker,您应该使用XML Worker。 HTMLWorker不再受支持,并可能在不久的将来从iText中删除。

我看到你也在使用HeaderFooter类。这个班级在几年前已经被删除。请看新的例子:http://www.itextpdf.com/themes/keyword.php?id=221

这些例子都是用Java编写的,如果您需要C#版本,请查找相应的C# examples in the SVN repository。您可能想要阅读chapter 10 of my book

终于:请阅读http://lowagie.com/itext2

+0

嗨布鲁诺,我没有下载,没有将你的软件包导入到我们的项目中,我们只是继续以它结束的其他公司的项目。我们有407 MB项目(库+代码)。在proj中。属性iText包含在JasperReports库中。而我的工作是将文本和图像添加到现有库的每个页面的标题中。 –

+0

我不明白这是如何相关的。您正在使用iText的过时版本。无论是升级并遵循文档,还是使用*不支持的*版本自行解决问题。在后一种情况下:请不要责怪文档。我的建议:优秀的编码人员编写未来的证明代码。编写未来证明代码不包括使用不再受其作者认可的库版本。 –