• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Print / Save an NSView in a given size
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Print / Save an NSView in a given size


  • Subject: Re: Print / Save an NSView in a given size
  • From: "Marc Wan" <email@hidden>
  • Date: Mon, 15 Jan 2007 10:55:58 +0800

On 1/15/07, email@hidden <email@hidden> wrote:
Hello,

how can I print / save the content of an NSView in a given size (e.g.
3 x 3cm)?

so, I'm not 100% clear on what you're asking, but if i'm reading your message correctly, you want to:

1 - render some NSView data
2 - print it in a 3x3cm square
3 - print as PDF

And it sounds like you know how to do 1 and 3, so it's just really
step 2 that's the tricky part.

The good news is that it's not really hard.  As far as I understand
it (and testing in my program seems to confirm this), printing output
defaults to 72dpi.  Soooo, if you want to print at a different
resolution, you just need to scale the bounds of your NSView (not the
frame, just the bounds).

So, if you know your output rect is 200x200 pixels, then that would
become ~2.8 inches or so, or about 7.1cm.  To make this fit within
3cm, you would just add the following somewhere in your PRINTER
NSView:

/**
* I.E. printingSize = NSMakeSize(1.18, 1.18)  (1.18" = 3cm)
*/
- (void)adjustSizingForPrinting: (NSSize)printingSize
{
NSSize outputSize = NSMakeSize(200, 200);  // 200x200 pixel output
int dpi;

if (outputSize.width / outputSize.height >= (printingSize.width /
printingSize.height))
{
  dpi = (int)(outputSize.width / printingSize.width);
}
else
{
  dpi = (int)(outputSize.height / printingSize.height);
}

NSSize cs = [self frame].size;
NSSize nn = NSMakeSize(cs.width * dpi / 72, cs.height * dpi / 72);

[self setBoundsSize: nn];
}


The extra IF state at the top of this code is just there to help handle rectangular shapes, etc. You just need to add a call to [self adjustSizingForPrinting: ....] before you print.

The strategy is to just resize your bounds so that your drawing
pixels end up as the desired size.  The printing engine takes care of
matching it to device resolution based on capabilities and user print
settings (quality, etc).  It's actually extremely cool.


Jens Beuckenhauer
email@hidden
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Print / Save an NSView in a given size
      • From: Jens Beuckenhauer <email@hidden>
References: 
 >Print / Save an NSView in a given size (From: email@hidden)

  • Prev by Date: Re: NSURLConnection and HTTP Headers
  • Next by Date: Re: StatusItem for PreferencePane
  • Previous by thread: Print / Save an NSView in a given size
  • Next by thread: Re: Print / Save an NSView in a given size
  • Index(es):
    • Date
    • Thread