4
3
2

How can you add a page number to every page of the PDF receipt generated from Maestro?

    CommentAdd your comment...

    1 answer

    1.  
      1
      0
      -1

      Hi Ben ... I did this with a non-fluent render service (iText wasn't allowed with Fluent); here's a function that does it, plus puts tracking code and submit date in the header area of page 1.

      def insertPageXofY(byte[] originalReceipt, String submittedDate, String trackingCode) {
          PdfReader reader = new PdfReader(originalReceipt)
          ByteArrayOutputStream byteStream = new ByteArrayOutputStream()
          PdfStamper stamper = new PdfStamper(reader, byteStream)
          logger.debug "  PDF input bytes: ${originalReceipt.size()}"
          // set font size and coordinates for header line
          int fontSize = 10
          int headerCount = 2
          List<Phrase> headers = []
          headers.add(new Phrase())
          headers.add(new Phrase())
          headers[0].add(new Chunk("Online Reference #: ", new Font(Font.FontFamily.HELVETICA, fontSize, Font.BOLD)))
          headers[0].add(new Chunk(trackingCode, new Font(Font.FontFamily.HELVETICA, fontSize, Font.NORMAL)))
          headers[1].add(new Chunk("Submitted: ", new Font(Font.FontFamily.HELVETICA, fontSize, Font.BOLD)))
          headers[1].add(new Chunk(submittedDate, new Font(Font.FontFamily.HELVETICA, fontSize, Font.NORMAL)))
          float x = reader.getPageSize(1).getWidth() - 20
          List<Float> yHeader = []
          yHeader.add(reader.getPageSize(1).getTop(40) as Float)
          yHeader.add(yHeader[0] - 15 as Float)
          logger.debug "  header (x,y)   : (${x},${yHeader})"
          for (int i=0; i < headerCount; i++) {
              ColumnText.showTextAligned(stamper.getOverContent(1), Element.ALIGN_RIGHT, headers[i], x, yHeader[i], 0)
          }
          // ColumnText.showTextAligned(stamper.getOverContent(1), Element.ALIGN_RIGHT, header, x, y, 0)
          // set font size and coordinates for page # footer
          int n = reader.getNumberOfPages()
          fontSize = 8
          x = reader.getPageSize(1).getRight(20)
          float yFooter = reader.getPageSize(1).getBottom(20)
          logger.debug "  footer (x,y)   : (${x},${yFooter})"
          logger.debug "  number of pages: ${n}"
          // loop though adding Page X of Y to each page
          for (int i = 1; i <= n; i++) {
              Phrase footer = new Phrase()
              String pageText = "Page ${i} of ${n}"
              footer.add(new Chunk(pageText, new Font(Font.FontFamily.HELVETICA, fontSize, Font.NORMAL)))
              ColumnText.showTextAligned(
                      stamper.getOverContent(i),
                      Element.ALIGN_RIGHT,
                      footer, x, yFooter, 0)
          }
          stamper.close()
          reader.close()
          return byteStream.toByteArray()
      }

       

      Jamie

        CommentAdd your comment...