-1

Hii Guys !! Below is my code to export database into excel file .Now as per my requirement i want to add header image of company at the top of the page .plz guys help me and guide me to complete the task.thanks in advance .below is my code...

        Document document = new Document(PageSize.A2);

        PdfWriter.getInstance(document, new FileOutputStream("d:/".concat(datum1).concat(" ").concat("To").concat(" ").concat(datum2).concat(".pdf")));
        document.open();

        Image logo = Image.getInstance("d:/header.png");
        logo.setAlignment(Image.MIDDLE);
        logo.scaleAbsoluteHeight(20);
        logo.scaleAbsoluteWidth(20);
        logo.scalePercent(100);
        Chunk chunk = new Chunk(logo, 0, -45);
        HeaderFooter header = new HeaderFooter(new Phrase(chunk), false);
        header.setAlignment(Element.ALIGN_CENTER);
        header.setBorder(Rectangle.NO_BORDER);
        document.setHeader(header);

        PdfPTable table = new PdfPTable(9);
        table.setWidthPercentage(110);
        table.addCell("calldate");
        table.addCell("src");
        table.addCell("dst");
        table.addCell("dstchannel");
        table.addCell("lastapp");
        table.addCell("duration");
        table.addCell("disposition");
        table.addCell("amaflags");
        table.addCell("cdrcost");


        String strQuery = "";
        ResultSet rs = null;

        conexion conexiondb = new conexion();
        conexiondb.Conectar();

        strQuery = "SELECT * FROM cdrcost where date(calldate) between '" + datum1 + "' and '" + datum2 + "'";

        // strQuery = "SELECT * FROM cdrcost where date(calldate) between '2011-09-01' and '2012-01-01'";

        rs = conexiondb.Consulta(strQuery);
        while (rs.next()) {
            table.addCell(rs.getString("calldate"));
            table.addCell(rs.getString("src"));
            table.addCell(rs.getString("dst"));
            table.addCell(rs.getString("dstchannel"));
            table.addCell(rs.getString("lastapp"));
            table.addCell(rs.getString("duration"));
            table.addCell(rs.getString("disposition"));
            table.addCell(rs.getString("amaflags"));
            table.addCell(rs.getString("cdrcost"));
        }

        document.add(table);
        document.close();
Adarsh
  • 117
  • 1
  • 2
  • 9
  • Duplicate question, answered yesterday: http://stackoverflow.com/questions/13465657/itext-add-content-to-the-bottom-of-an-existing-page – Bruno Lowagie Nov 21 '12 at 09:13
  • @BrunoLowagie Sir i went through the link but did not solved my issue plz sir help me – Adarsh Nov 21 '12 at 09:17

1 Answers1

0
  1. As explained in add content to the bottom of an existing page, the method setHeader() has been removed from iText a long time ago. Please don't use it!
  2. You can easily add an image as a header by using page events. Page events are explained in Chapter 5 of iText in Action. You can find references to examples on SO

As you don't seem to understand the answer given previously on SO, let me repeat:

You need to create a PdfPageEvent implementation, for instance by extending the PdfPageEventHelper class overriding the onEndPage() method. When you do so, take into account the following caveats:

  • Do not use onStartPage() to add content,
  • Do not add anything to the Document object passed to the page event,
  • Unless you specified a different page size, the lower-left corner has the coordinate x = 0; y = 0. You need to take that into account when adding the footer. The y-value for the footer is lower than the y-value for the header.

In your code, you need to use the setPageEvent() method on the PdfWriter object before opening the document. Every time a page is finished the onEndPage() method you wrote will be called, so that's where you need to add the image using PdfContentByte.addImage(). If you want the image to be on the top of the page, you need to ask the Document for its dimensions, and set the absolute position for the Image object accordingly.

If you don't understand this elaborate explanation, please read my book or hire an iText developer to do this assignment for you.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165