Perfectly matched layers (PMLs) are widely used in particle-in-cell simulations, in order to absorb electromagnetic waves that propagate out of the simulation domain. However, when charged particles cross the interface between the simulation domain and the PMLs, a number of numerical artifacts can arise. In order to mitigate these artifacts, we introduce a PML algorithm whereby the current deposited by the macroparticles in the PML is damped by an analytically derived optimal coefficient. The benefits of this algorithm are illustrated in practical simulations. In particular, it is shown that this algorithm is well suited for particles exiting the box in near-normal incidence, in the sense that the fields behave as if the exiting particle is propagating in an infinite vacuum.