title: Read BLOB (PDF Content) from database and edit and output PDF edited file without creating physical file
body: <p>I am using an Oracle Database and storing PDF content in a BLOB field.</p>
  
  <p>I want to read the BLOB content and then edit and output the edited content.</p>
  
  <p>The editing I need to do are:</p>
  
  <ul>
  <li>add a title above the BLOB content</li>
  <li>add a Water mark on every page</li>
  <li>add footer on every page</li>
  </ul>
  
  <p>Then I need to output the file without any physical file getting created that is within the response stream.</p>
  
  <p>I tried to achieve this using itext but was not reaching anywhere with it. I am stuck and not sure where to start with.</p>
  
  <p>Also sometimes I might have to combine blob contents into one, buts thats some thing that is bound to happen Once in a million..so that not a concern now...</p>
  
  <p>How can I achieve my primary requirements of the above three steps using in java? Is it possible with Itext?? Or is some other library available that would help?</p>
  
  <p>Database : Oracle 10g Release 2</p>
  
  <p>OS: Linux Fedora/Redhat</p>
  
  <p>Front-end: Java/Servlet/JSP</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is what I tried to do</p>
  
  <pre><code>oracle.sql.BLOB blob = (BLOB) rs.getBlob("MYPDF");
  byte[] bytes = blob.getBytes(1, (int) blob.length());
  InputStream is = blob.getBinaryStream();
  Document document=new Document();
  ServletOutputStream servletOutputStream = response.getOutputStream();
  PdfWriter writer=PdfWriter.getInstance(document, servletOutputStream);
  document.open();
  document.add(new Paragraph("Some title"));
  document.add(new Paragraph("Some title"));
  response.setContentType("application/pdf");
  response.setHeader("Content-Disposition", "attachment; filename=output.pdf");
  servletOutputStream.write(bytes, 0, bytes.length);
  servletOutputStream.flush();
  servletOutputStream.close();
  document.close();
  </code></pre>
  
  <p>The program outputs the pdf content in BLOB field in the database and without the title.</p>
  
  <p>and when I change a bit in the code (change the order of the last few lines) to: </p>
  
  <pre><code>document.close();
  servletOutputStream.flush();
  servletOutputStream.close();
  </code></pre>
  
  <p>I get the document with the title content in it and no pdf content of BLOB field.
  Its the first thing(servletoutputstream/document) that is closed is been thrown as the output.</p>
  
  <p>And when I closed the document before putting the blob content in outputstream:</p>
  
  <pre><code>document.close();
  response.setContentType("application/pdf");
  response.setHeader("Content-Disposition", "attachment; filename=output.pdf");
  servletOutputStream.write(bytes, 0, bytes.length);
  servletOutputStream.flush();
  servletOutputStream.close();
  </code></pre>
  
  <p>I got the browser displaying something like this: </p>
  
  <pre><code>%PDF-1.4 %���� 2 0 obj &lt;&gt;stream x�+�r �26S�00SI�2P�5��1���BҸ4��sSJ2KrR5C��*P�B�5�+��k)&amp;� endstream endobj 4 0 obj &lt;&lt;&lt;&gt;&gt;&gt;/MediaBox[0 0 595 842]&gt;&gt; endobj 1 0 obj &lt;&gt; endobj 3 0 obj &lt;&gt; endobj 5 0 obj &lt;&gt; endobj 6 0 obj &lt;&gt; endobj xref 0 7 0000000000 65535 f 0000000304 00000 n 0000000015 00000 n 0000000392 00000 n 0000000147 00000 n 0000000443 00000 n 0000000488 00000 n trailer &lt;]/Info 6 0 R/Size 7&gt;&gt; startxref 620 %%EOF 
  </code></pre>
  
  <p>I need the file to be outputted with the pdf content and the title as well.</p>
  
  <p>Hope this edit helps a little bit...</p>
  
  <p><strong>UPDATE(File thrown out in response with the title and the BLOB Content) :</strong></p>
  
  <pre><code>Document document = new Document(PageSize.A4, 108, 72, 30, 72);
  PdfWriter writer = PdfWriter.getInstance(document, outputstream);
  
  document.open();
  
  ///-----Added Some Title----///
  
  rs = stmt.executeQuery(queryToGetBLOBCONTENT);
  
  if (rs.next()) {
  
  
  response.setContentType("application/pdf");
  response.setHeader("Content-Disposition", "attachment; filename=watermark.pdf");
  oracle.sql.BLOB blob = (BLOB) rs.getBlob("MYPDF");
  byte[] bytes = blob.getBytes(1, (int) blob.length());
  InputStream is = blob.getBinaryStream();
  PdfReader pdfReader = new PdfReader(is, bytes);
  BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
  PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
  PdfImportedPage page;
  int currentPageNumber = 0;
  int pageOfCurrentReaderPDF = 0;
  while (pageOfCurrentReaderPDF &lt; pdfReader.getNumberOfPages()) {
      if (pageOfCurrentReaderPDF &gt; 0) {
          document.newPage();
      }
      pageOfCurrentReaderPDF++;
      currentPageNumber++;
      page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
      cb.addTemplate(page, 0, 0);
  }
  pageOfCurrentReaderPDF = 0;
  outputstream.flush();
  document.close();
  outputstream.close();
  
  }
  </code></pre>
  
  <p>This gives me a file in response that has a the BLOB from DB with title on the top and that is done without any physical files getting generated.</p>
  
  <p>Now to generate the water mark and I need to pass the document to the PDfreader how can I achieve that before closing the document (i.e. executing <code>document.close()</code> , which would out put the file w/o water mark as the stream got closed)</p>
  
  <p>What am I doing wrong in this code? How can I achieve the same file with the watermark and that too without a file getting created at the background.</p>