Adobe Systems said Monday it had agreed to buy Macromedia for about $3.4 billion in stock, in order to extend its lead in the market for creating and distributing digital documents.