title: how to create pdf file in java with alternate colored rows and picture at the top
body: <p>I am generating pdf file using <code>com.itextpdf.text.*</code> following is my code which creates the pdf file with title and header higlighted and rows, what i wanted to do is, create a pdf file with image on the top and rows with alternate color, how to do this in using <code>com.itextpdf.text.*</code> </p>
  
  <pre><code>response.setHeader("Content-disposition", "attachment; filename=\"" + reportName + ".pdf\"");
          response.setContentType("application/pdf");
          PdfWriter.getInstance(document,response.getOutputStream());
  
          try {
              document.open();
              addTitlePage(document, reportName);
  
              //float[] colsWidth = {1.5f,3f,4f,4f,2f};
  
              List&lt;Float&gt; colsWidth = new ArrayList&lt;Float&gt;();
              int iterator = 1;
             while (iterator &lt;= headerMap.size()) {
                 if(iterator==1){
                     colsWidth.add(1.5f); 
                 }else{
                  colsWidth.add(3f); 
                 }
                  iterator++;
              }
             float[] floatArray = ArrayUtils.toPrimitive(colsWidth.toArray(new Float[0]), 0.0F);
  
             PdfPTable table = new PdfPTable(floatArray);
              table.setWidthPercentage(98);
              table.setHorizontalAlignment(Element.ALIGN_CENTER);
  
              PdfPCell c1 = new PdfPCell();
              for (Iterator it = headerMap.keySet().iterator(); it.hasNext();) {
                  String headerName = (String) headerMap.get(it.next());
                  c1 = new PdfPCell(new Phrase(headerName, headerFont));
                  c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
                  table.addCell(c1);
              }
              table.setHeaderRows(1);
              table = custDAO.creadPDFTable(query, table);
              document.add(table);
              document.addAuthor(userViewModel.getUsername());
              document.addCreationDate();
              document.addCreator("POC");
              document.close();
              response.flushBuffer();
  
  public PdfPTable creadPDFTable(String query,PdfPTable table){
          int numberOfColumns=0,sno=1;
          Connection connection = getConnection();
          if (connection != null) {
              try {
                  PreparedStatement reportTablePS = connection.prepareStatement(query);
                  ResultSet reportTable_rst = reportTablePS.executeQuery();
                  ResultSetMetaData reportTable_rsmd = reportTable_rst.getMetaData();
                  numberOfColumns = reportTable_rsmd.getColumnCount();
                  while (reportTable_rst.next()) {
                      table.addCell(new PdfPCell(new Paragraph(String.valueOf(sno), textFont)));
                              for (int columnIterator = 1; columnIterator &lt;= numberOfColumns; columnIterator++) {
                                   String column = reportTable_rst.getString(columnIterator);
                                   table.addCell(new PdfPCell(new Paragraph(column, textFont)));
                              }
                              sno++;
              }
              } catch (Exception ex) {
                  ex.printStackTrace();
              }finally {
                  try {
                      closeConnection(connection, null, null);
                  } catch (Exception ex) {
                      ex.printStackTrace();
                  }
              }
          }
          return table;
      }
  
  private static void addTitlePage(Document document, String reportName) throws DocumentException {
          Paragraph preface = new Paragraph();
          addEmptyLine(preface, 1);
          /**
           * Lets write a big header
           */
          Paragraph paragraph = new Paragraph(reportName, titleFont);
          paragraph.setAlignment(Element.ALIGN_CENTER);
          document.add(paragraph);
  
          /**
           * Add one empty line
           */
          addEmptyLine(preface, 1);
          document.add(preface);
      }
      private static void addEmptyLine(Paragraph paragraph, int number) {
          for (int i = 0; i &lt; number; i++) {
              paragraph.add(new Paragraph(" "));
          }
      }
  </code></pre>
  
  <p>when i use the following i get the following exception 'getoutputstream() has already called for this response'</p>
  
  <p>i wanted to use this for inserting image.</p>
  
  <pre><code>            Image image = Image.getInstance(path+"images/abi.png");
              image.setAbsolutePosition(40f, 770f);
              image.scaleAbsolute(70f, 50f);
              document.add(image);
  </code></pre>
  
  <p>so how to go about doing this?</p>
  
  <p>UPDATE : </p>
  
  <p>i want to create a pdf file like <a href="http://java.cete.com/DynamicPDFv60/InvoiceMerger?invoiceno=10248&amp;invoiceno=10249&amp;invoiceno=10250&amp;invoiceno=10251&amp;invoiceno=10252&amp;invoiceno=10979&amp;btnCreateInvoices=Create%20Invoices" rel="nofollow">this</a> i just want to add image on the top and rows with alternate color like this.</p>