You say you already have examples for applying watermarks using iText. As you already have a PDF, you should use code from an example that adds watermarks to existing PDFs. This should be an example that works with a PdfReader / PdfStamper pair, e.g. those here, which all have the structure
PdfReader reader = new PdfReader(SOME_SOURCE);
PdfStamper stamper = new PdfStamper(reader, SOME_TARGET_STREAM);
[... add watermark to all pages in stamper ...]
stamper.close();
reader.close();
To make these example fit into your addWatermark method, simply use your byte[] instead of SOME_SOURCE and a ByteArrayOutputStream instead of SOME_TARGET_STREAM:
byte[] addWatermark(byte[] generatedBytes) {
try (ByteArrayOutputStream target = new ByteArrayOutputStream()) {
PdfReader reader = new PdfReader(generatedBytes);
PdfStamper stamper = new PdfStamper(reader, target);
[... add watermark to all pages in stamper ...]
stamper.close();
reader.close();
return target.toByteArray();
}
}
PS As you used only the tag itext and not the tag itext7, I assumed you were looking for a solution for iText 5.5.x. But the same principle as applied here, i.e. using your byte[] as source argument and a ByteArrayOutputStream as target argument, will also allow you to make iText 7.x examples fit into your method frame.
PdfReader. Create aPdfStamperwith a different byte output stream, apply watermark using the example you found and return the bytes from the outputstream after closing the PdfStamper - Samuel Huylebroeck