The attacks began shortly before 1 p.m. Paris time, when the headquarters was bustling with activity: document seekers getting papers signed, police officials piling out of their offices to go to lunch and all around a swirling crowd of visitors who fill the island in the middle of the Seine river.

The building houses not only the offices of the police, but also numerous other agencies, including those regulating the presence of foreigners in Paris. It is where Americans living in Paris, for instance, apply for residency permits.

The suspect, a 20-year-veteran of the force, was not immediately identified by the police. He struck in his own office first before going upstairs to kill two more colleagues, Mr. Jacob, the union official, said. By then, the alarm had been raised. As the attacker entered the sprawling courtyard, traversed by thousands of visitors every day, a police officer took out his weapon and shot him, officials said.

The suspect had not previously exhibited signs of trouble, officials said.

“He set himself on a murderous path,” the interior minister, Christophe Castaner, told reporters at the site. “There had never been problems with his behavior.” Prosecutors said his home was being searched.

The sound of gunshots was the first warning many in the building had of the attack, and it brought employees rushing out of their offices. The police quickly locked down the island, barring all visitors.

Youssouf Dramé, a human resources employee who worked in the building, was having lunch at his desk when he heard noises in the courtyard. “I heard shouting: ‘Drop your weapons! Drop your weapons!’” Mr. Dramé said. He then heard two shots. Two colleagues in the office began to cry.