While there is no perfect answer to getting a child to behave perfectly--especially in a doctor's office--the good news is that your pediatrician should be entirely used to the problem of fussy, misbehaving children, and might have some tips for you after watching your son in person. In fact, pediatricians
are well known as some of the nicest doctors
in medicine precisely because they manage to enjoy even the most difficult patient encounters (most of the time!). Obviously, corporal punishment (such as spanking) as a means of altering behavior is discouraged strongly.
Your idea to discuss the office visit with him before hand is excellent, as it can lay out some expectations of appropriate an inappropriate behaviors. Your son is reaching an age where such conversations will start to become better received on his part. Some parents find that positive reinforcement, such as a small reward for good behavior, often induce the best behavior in children. If his behavior is routinely poor, there might be a medical reason for this as well, and you should discuss these concerns with his pediatrician. And again, most importantly, your son's behavior should not deter you from visiting a pediatrician, as such behavior is more the norm than the exception!