0

I'm trying to create a header with an image; the header is added, but the image is missing in header. My application is deployed on an Oracle Weblogic server (using Java EE and Hibernate).

I'm trying to create the image like this. getImage(seasonalFilter.getPictureFileId()).getAbsolutePath(). The image path is something like this: /tmp/6461346546165461313_65464.jpg.

Note that I want to add text under the image in the header (for every page).

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);
    }

  }
4

1 回答 1

1

I really dislike your false allegation that "Every tutorial is based on C:\imagelocation\dsadsa.jpg"

I'm the author of two books and many tutorials about iText and I know for a fact that what you say doesn't make any sense. Take a look at my name: "Bruno Lowagie." You are using my name in your code, so please believe me when I say you're doing it completely wrong.

Instead of HTMLWorker, you should use XML Worker. HTMLWorker is no longer supported and will probably be removed from iText in the near future.

I see that you're also using the HeaderFooter class. This class has been removed several years ago. Please take a look at the newer examples: http://www.itextpdf.com/themes/keyword.php?id=221

These examples are written in Java; if you need the C# version, please look for the corresponding C# examples in the SVN repository.

Regarding images, you may want to read chapter 10 of my book.

Finally: please read http://lowagie.com/itext2

于 2012-12-29T09:12:07.650 回答