@Override public void handleEvent(Event event) { PdfDocumentEvent docEvent = (PdfDocumentEvent) event; PdfDocument pdfDoc = docEvent.getDocument(); PdfPage page = docEvent.getPage(); PdfCanvas canvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc); Rectangle area = page.getPageSize(); new Canvas(canvas, pdfDoc, area) .add(images.get(offset)); offset++; } }
/** * Convenience method for fast PdfCanvas creation by a certain page. * * @param page page to create canvas from. * @param wrapOldContent true to wrap all old content streams into q/Q operators so that the state of old * content streams would not affect the new one */ public PdfCanvas(PdfPage page, boolean wrapOldContent) { this(getPageStream(page), page.getResources(), page.getDocument()); if (wrapOldContent) { // Wrap old content in q/Q in order not to get unexpected results because of the CTM page.newContentStreamBefore().getOutputStream().writeBytes(ByteUtils.getIsoBytes("q\n")); contentStream.getOutputStream().writeBytes(ByteUtils.getIsoBytes("Q\n")); } if (page.getRotation() != 0 && page.isIgnorePageRotationForContent() && (wrapOldContent || !page.isPageRotationInverseMatrixWritten())) { applyRotation(page); page.setPageRotationInverseMatrixWritten(); } }