3131import java .util .List ;
3232import java .util .Map ;
3333import java .util .Set ;
34+ import org .apache .logging .log4j .LogManager ;
35+ import org .apache .logging .log4j .Logger ;
3436
3537import org .apache .pdfbox .Loader ;
3638import org .apache .pdfbox .cos .COSArray ;
5456 */
5557public class Overlay implements Closeable
5658{
59+ private static final Logger LOG = LogManager .getLogger (Overlay .class );
60+
5761 /**
5862 * Possible location of the overlaid pages: foreground or background.
5963 */
@@ -565,8 +569,9 @@ private COSStream createOverlayStream(PDPage page, LayoutPage layoutPage, COSNam
565569
566570 /**
567571 * Calculate the transform to be used when positioning the overlay. The default implementation
568- * centers on the destination. Override this method to do your own, e.g. move to a corner, or
569- * rotate.
572+ * centers on the destination, and this is calculated from the lower left of the media box of
573+ * the destination (this has been changed from 3.0 and 2.0, see PDFBOX-6048 for details).
574+ * Override this method to do your own, e.g. move to a corner, rotate, or zoom.
570575 *
571576 * @param page The page that will get the overlay.
572577 * @param overlayMediaBox The overlay media box.
@@ -576,8 +581,9 @@ protected AffineTransform calculateAffineTransform(PDPage page, PDRectangle over
576581 {
577582 AffineTransform at = new AffineTransform ();
578583 PDRectangle pageMediaBox = page .getMediaBox ();
579- float hShift = (pageMediaBox .getWidth () - overlayMediaBox .getWidth ()) / 2.0f ;
580- float vShift = (pageMediaBox .getHeight () - overlayMediaBox .getHeight ()) / 2.0f ;
584+ float hShift = pageMediaBox .getLowerLeftX () + (pageMediaBox .getWidth () - overlayMediaBox .getWidth ()) / 2.0f ;
585+ float vShift = pageMediaBox .getLowerLeftY () + (pageMediaBox .getHeight () - overlayMediaBox .getHeight ()) / 2.0f ;
586+ LOG .debug ("Overlay position: ({},{})" , hShift , vShift );
581587 at .translate (hShift , vShift );
582588 return at ;
583589 }
0 commit comments