IText: how to insert a background image in the same document that needs to be cleared of the answer

I create a pdf file and write the stream in response. Before writing in the stream, I want to add a background image as a watermark on all pages, so that the pdf document cleared of the answer is the last with a watermark.

Hi, this is my sample code. Any help will be ambiguous

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(); } } 

Now I would like to add a background image using the code below and write the pdf output to the same stream

  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(); } } 
+4
source share
3 answers

I solved this using Bruno's first (recommended) approach.

1) Create an auxiliary event element for the page with the onEndPage event:

 class PDFBackground extends PdfPageEventHelper { @Override void onEndPage(PdfWriter writer, Document document) { Image background = Image.getInstance("myimage.png"); // This scales the image to the page, // use the image width & height if you don't want to scale. float width = document.getPageSize().getWidth(); float height = document.getPageSize().getHeight(); writer.getDirectContentUnder() .addImage(background, width, 0, 0, height, 0, 0); } } 

2) When creating your writer, register the auxiliary event server for the page:

 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); writer.setPageEvent(new PDFBackground()); 
+11
source

I solved this with Bruno's second option. Here is the code.

 public static String addBackgroundImageToPDF(ByteArrayOutputStream bos, String destPdfFileName, String templateImageFile) { PdfReader sourcePDFReader = null; try { sourcePDFReader = new PdfReader(bos.toByteArray()); 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 template image as watermark"); } finally { if (sourcePDFReader != null) { sourcePDFReader.close(); } } } 
+4
source

You can choose one of two options:

  • Use the background image in the page event (to the "under" content in the onEndPage( ) method) /
  • Create the first PDF file in memory, then add the background image in the second pass using the code you sent.

I prefer option 1.

+2
source

Source: https://habr.com/ru/post/1437231/


All Articles