A gunman opened fire inside the newsroom of a Maryland newspaper in a targeted attack Thursday afternoon, killing five people and gravely injuring others, police said.

Bill Krampf, acting police chief in Anne Arundel County, said at a news conference that the suspect, a white male in his late 30s, roamed the lower level of the building that houses the Capitol Gazette in Annapolis armed with a shotgun and looking for potential victims.

NBC News and other outlets, citing sources, identified the shooter as Jerrod Ramos, who in 2012 had unsuccessfully sued the newspaper for defamation over an unflattering article.

Steve Schuh, Anne Arundel county executive, told reporters that police found the suspect hiding under a desk in the building. He had dropped his weapon elsewhere.

The suspect reportedly altered or otherwise mutilated his fingerprints to avoid being identified, but was identified anyway using facial-recognition software.

Police later said they recovered what was believed to be an explosive device from the building.

Krampf said that threats were made against the newspaper on social media, including some posted on Thursday.

The first reports of the shooting were around 2:40 p.m., according to the Baltimore Sun, which owns the newspaper.

Phil Davis, a reporter at the Gazette and Rowan University alumnus, told the Sun that it "was like a war zone" inside the newspaper's offices.

"I'm a police reporter. I write about this stuff — not necessarily to this extent, but shootings and death — all the time," he said. "But as much as I'm going to try to articulate how traumatizing it is to be hiding under your desk, you don't know until you're there and you feel helpless."

Davis tweeted about the shooting shortly after it started.

"Gunman shot through the glass door to the office and opened fire on multiple employees. Can't say much more and don't want to declare anyone dead, but it's bad," he wrote.

The Capital Gazette, located south of Baltimore, serves Anne Arundel County, Annapolis and Kent Island.

Liam Stack, a New York Times politics reporter, tweeted a photo of police guarding the entrance of his newspaper in response to the Maryland shooting.

President Trump tweeted that he had been briefed on the shooting. "My thoughts and prayers are with the victims and their families," he wrote.

Jimmy DeButts, an editor at the newspaper, tweeted after the shooting that he was "devasted & heartbroken."

He added: "There are no 40 hour weeks, no big paydays – just a passion for telling stories from our community."

Maryland Gov. Larry Hogan released a statement Thursday evening saying he was "terribly saddened by the loss of five members of our community in today's heinous shooting" and thanked first responders for their swift response.

"The Capital Gazette is my hometown paper, and I have the greatest respect for the fine journalists, and all the men and women, who work there. They serve each day to shine light on the world around us so that we might see with more clarity and greater understanding," Hogan said.

"There is no place in our society for this kind of hatred and violence, and the individual responsible for this horrendous crime must be prosecuted to the fullest extent of the law," he said.