The 15-year-old victim found it extremely distressing "and understandably so", Judge Emma Smith told a man who did an indecent act in the Dunedin Public Library.
The victim was in the teen area on the second floor. She was in what should have been a safe place "and you violated that", the judge said.
Adam Charlett, 23, of Dunedin, had been convicted of doing an indecent act.
He had pleaded guilty.
The police summary said the victim was lying on a couch in the teen area watching a movie on her phone with her headphones in her ears, about 3.10pm.