A solution for -drawPageBorderWithSize:
A solution for -drawPageBorderWithSize:
- Subject: A solution for -drawPageBorderWithSize:
- From: Bill Cheeseman <email@hidden>
- Date: Wed, 06 Aug 2003 10:52:43 -0400
I'm posting this for general information, and to ensure that it will be
found by people searching the mamasam archive for information about this
method.
Last year, there was a flurry of posts generally suggesting that the
-drawPageBorderWithSize: method is broken and doesn't work. However, I have
found a simple solution. It is so simple and so sensible (once you think of
it) that I suspect this is the way it was designed to work.
If I'm right, the documentation ought to be rewritten to make this technique
clear, because a lot of people have wasted a lot of time attempting to
figure out how it works.
Basically, printing normally involves programmatically creating a subclass
of NSView specifically for printing. It is customary to initialize the
printing view with -initWithFrame:, creating a frame sized by subtracting
all four margins from the paperSize rect obtained from NSPrintInfo
sharedPrintInfo (or from a document's printInfo). In essence, the frame of
the printing view encompasses the printable body text area of each page,
excluding the margins. Your printing view's -drawRect: override will then
automatically confine all printing to the body text area.
The purpose of -drawPageBorderWithSize:, according to the documentation, is
to allow you to print in the margins on every page -- so you can print
headers and footers, for example. Here's how to do it:
in your printing view, override -drawPageBorderWithSize: and do this in it:
First, save the view's body text frame (in order to be able to restore it at
the end of the method). Then resize the frame temporarily to a rect with
origin 0,0 and size equal to the incoming borderSize parameter to the
-drawPageBorderWithSize: method (which happens to be the full paper size
from print info). Then draw whatever you want to in the new, enlarged frame,
typically using -drawAtPoint:. At the end of the method, restore the view's
frame to its original size.
The Cocoa printing system will call your override of
-drawPageBorderWithSize: every time it draws a new page. If your override
draws headers or footers in the top or bottom margin, or crop marks in any
margin, they will be printed in the margins on every page because you
enlarge the frame. You can even draw in the body text area of each page, for
example, to print a semi-transparent watermark. To confine your printing to
the margins, just make appropriate use of the printInfo paperSize and margin
information.
Watch out for the usual gotchas. For example, some printers won't print all
the way to the paper edge. And remember to call -lockFocus and -unlockFocus
around your -drawAtPoint: messages in -drawPageBorderWithSize:, or it won't
draw (this is only necessary in -drawPageBorderWithSize:; you don't have to
lock focus in your override of -drawRect:).
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
http://www.quecheesoftware.com
The AppleScript Sourcebook -
http://www.AppleScriptSourcebook.com
Vermont Recipes -
http://www.stepwise.com/Articles/VermontRecipes
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.