src/main/java/uk/gov/hmcts/reform/sendletter/services/pdf/DuplexPreparator.java
public byte[] prepare(byte[] pdf) {
        logger.info("File size is {} KB", pdf.length / 1024);
        try (PDDocument pdDoc = PDDocument.load(pdf)) {
            if (pdDoc.getNumberOfPages() % 2 == 1) {
                PDRectangle lastPageMediaBox = pdDoc.getPage(pdDoc.getNumberOfPages() - 1).getMediaBox();
                pdDoc.addPage(new PDPage(lastPageMediaBox));
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                pdDoc.save(out);

                return out.toByteArray();

            } else {
                return pdf;
            }
        } catch (IOException exc) {
            throw new DuplexException(exc);
        }
    }
/**
     * Adds an extra blank page if the total number of pages is odd.
     */
Adds an extra blank page if the total number of pages is odd.
Adds an extra blank page if the total number of pages is odd .
public byte[] prepare(byte[] pdf) {
        logger.info("File size is {} KB", pdf.length / 1024);
        try (PDDocument pdDoc = PDDocument.load(pdf)) {
            if (pdDoc.getNumberOfPages() % 2 == 1) {
                PDRectangle lastPageMediaBox = pdDoc.getPage(pdDoc.getNumberOfPages() - 1).getMediaBox();
                pdDoc.addPage(new PDPage(lastPageMediaBox));
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                pdDoc.save(out);
                return out.toByteArray();
            } else {
                return pdf;
            }
        } catch (IOException exc) {
            throw new DuplexException(exc);
        }
    }
public byte [ ] prepare ( byte [ ] pdf ) { logger . info ( "File size is {} KB" , pdf . length / 1024 ) ; try ( PDDocument pdDoc = PDDocument . load ( pdf ) ) { if ( pdDoc . getNumberOfPages ( ) % 2 == 1 ) { PDRectangle lastPageMediaBox = pdDoc . getPage ( pdDoc . getNumberOfPages ( ) - 1 ) . getMediaBox ( ) ; pdDoc . addPage ( new PDPage ( lastPageMediaBox ) ) ; ByteArrayOutputStream out = new ByteArrayOutputStream ( ) ; pdDoc . save ( out ) ; return out . toByteArray ( ) ; } else { return pdf ; } } catch ( IOException exc ) { throw new DuplexException ( exc ) ; } }
Adds an extra blank page if the total number of pages is odd.
Adds an extra blank page if the total number of pages is odd .