iText: Jak wstawić obraz tła w tym samym dokumencie, aby został opróżniony do odpowiedzi

W odpowiedzi tworzę plik PDF i zapisuję strumień. Przed zapisaniem w strumieniu chcę dodać obraz tła jako znak wodny na wszystkich stronach, tak aby dokument PDF przepłukany przez odpowiedź był ostatnim ze znakiem wodnym.

Cześć, to moja próbka kodu. Każda pomoc byłaby bardzo ceniona

private static String generatePDF(HttpServletRequest request, HttpServletResponse   response, String fileName) throws Exception
{
    Document document = null;
    PdfWriter writer = null;
    FileOutputStream fos = null;
    try
    {
       fos = new FileOutputStream(fileName);
       Document document = new Document(PageSize.A4);
       writer = PdfWriter.getInstance(document, fos);
       document.open();

       /**
        * Adding tables and cells and other stuff required
        **/

       return pdfFileName;
  } catch (Exception e) {
       FileUtil.deleteFile(fileName);
       throw e
  } finally {
    if (document != null) {
        document.close();
    }
    fos.flush();
  }
}

Chciałbym teraz dodać obraz tła przy użyciu poniższego kodu i zapisać wyjściowy plik PDF w tym samym strumieniu

PdfReader sourcePDFReader = null;
try
{
   sourcePDFReader = new PdfReader(sourcePdfFileName);
   int noOfPages = sourcePDFReader.getNumberOfPages();
   PdfStamper stamp = new PdfStamper(sourcePDFReader, new FileOutputStream(destPdfFileName));
   int i = 0;
   Image templateImage = Image.getInstance(templateImageFile);
   templateImage.setAbsolutePosition(0, 0);
   PdfContentByte tempalteBytes;
   while (i < noOfPages) {
       i++;
       tempalteBytes = stamp.getUnderContent(i);
       tempalteBytes.addImage(templateImage);
   }
   stamp.close();
   return destPdfFileName;
} catch (Exception ex) {
   LOGGER.log(Level.INFO, "Error when applying tempalte image as watermark");
} finally {
     if (sourcePDFReader != null) {
         sourcePDFReader.close();
     }
}

questionAnswers(3)

yourAnswerToTheQuestion