Re: Creating images from PDF files

13 views
Skip to first unread message

Dave Verwer

unread,
Aug 28, 2012, 4:09:12 AM8/28/12
to nsmanc...@googlegroups.com
Hi Will

I believe the solution to both of these issues will be the same. You need to create a new bitmap in memory and render the image into it. You will have to drop down to Core Graphics (C) to get this done though but it's not too hard.

Something like this:

* Create an image with CGBitmapContextCreate of the size you want.
* Fill the image with white using CGContextSetFillColorWithColor and CGContextFillRect.
* Draw your image over the top with CGContextDrawImage.
* Create an image back from your context using CGBitmapContextCreateImage and - initWithCGImage:size: on NSImage.
* Don't forget to release all of your core foundation objects as ARC does not apply to core graphics/core foundation. See CGContextRelease, CGImageRelease, etc...

Hope this helps.
Dave


---
twitter: @daveverwer
mail: dave....@shinydevelopment.com
web: http://shinydevelopment.com

Shiny Development Ltd. is registered in England, Registration Number: 05805802. Registered Office: 4 Claridge Court, Lower Kings Road, Berkhamsted, Herts., HP4 2AF.


On Monday, 27 August 2012 at 18:16, Will Jessop wrote:

> Hi All,
>
> At work we've recently started to need to turn PDF files into images. We are using the gs (ghostscript) command and it was taking 40+ seconds to create an image of a large PDF. I figured Cocoa could do better so I wrote a PDF Imaging class. Here's the .h, .m and a main.m to drive it (uses ARC):
>
> https://gist.github.com/e1473f229c3b86ec8499
>
> It works fine, though there are a couple of issues that are beyond my current cocoa abilities and could use some pointers.
>
> 1) When I create PNG files the background is transparent, I can't work out how to make it white, line 32 in the .m:
>
> NSData *imageData = [imageRep representationUsingType:NSPNGFileType properties:imageProps];
>
> 2) The image created has relatively small dimensions. I need to scale the PDF up before creating the image.
>
> I haven't worked out how to do either of these things so would appreciate some help from the more knowledgable here. Critique of my Obj-c code also welcome.
>
> Thanks in advance,
> Will.
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "NSManchester - Cocoa Developers Meetup" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/nsmanchester/-/nM52-g4emwcJ.
> To post to this group, send email to nsmanc...@googlegroups.com (mailto:nsmanc...@googlegroups.com).
> To unsubscribe from this group, send email to nsmanchester...@googlegroups.com (mailto:nsmanchester...@googlegroups.com).
> For more options, visit this group at http://groups.google.com/group/nsmanchester?hl=en.



Ciarán Walsh

unread,
Aug 28, 2012, 10:39:57 AM8/28/12
to nsmanc...@googlegroups.com
I’m not sure how addRepresentation: works but you could try just filling the NSImage with white before calling it. Since the result is transparent, it might be added over the existing contents of the image?

It would be something like this:

[pdfImage lockFocus];
[[NSColor whiteColor] set];
NSRectFill((NSRect){ NSZeroPoint, size });
[pdfImage unlockFocus];

If that works, then presumably to handle the resizing you would be able to just -setSize: on the image before writing it out (as you’ve already called -setScalesWhenResized:YES).

Ciarán
> To post to this group, send email to nsmanc...@googlegroups.com.
> To unsubscribe from this group, send email to nsmanchester...@googlegroups.com.

Will Jessop

unread,
Aug 28, 2012, 11:09:54 AM8/28/12
to nsmanc...@googlegroups.com
Thanks Dave, I'll give this a go tonight.

Will Jessop

unread,
Aug 28, 2012, 11:09:26 AM8/28/12
to nsmanc...@googlegroups.com
Thanks for the suggestion, I tried this just now as it was a pretty
simple change.

On Tue, Aug 28, 2012 at 3:39 PM, Ciarán Walsh <cia...@gmail.com> wrote:
> I’m not sure how addRepresentation: works but you could try just filling the NSImage with white before calling it. Since the result is transparent, it might be added over the existing contents of the image?
>
> It would be something like this:
>
> [pdfImage lockFocus];
> [[NSColor whiteColor] set];
> NSRectFill((NSRect){ NSZeroPoint, size });
> [pdfImage unlockFocus];

If I do that before calling addRepresentation: the image still has a
transparent background. Calling it after the and image is just white.

> If that works, then presumably to handle the resizing you would be able to just -setSize: on the image before writing it out (as you’ve already called -setScalesWhenResized:YES).

I tried setSize anyway. The image is bigger, but the scaling has
happened after rasterisation so the quality is crap. I'll give Dave's
suggestion a go later today.

Thanks,
Will.

Dave Verwer

unread,
Aug 28, 2012, 12:44:39 PM8/28/12
to nsmanc...@googlegroups.com
> I’m not sure how addRepresentation: works but you could try just filling the NSImage with white before calling it. Since the result is transparent, it might be added over the existing contents of the image?
>
> It would be something like this:
>
> [pdfImage lockFocus];
> [[NSColor whiteColor] set];
> NSRectFill((NSRect){ NSZeroPoint, size });
> [pdfImage unlockFocus];
>
> If that works, then presumably to handle the resizing you would be able to just -setSize: on the image before writing it out (as you’ve already called -setScalesWhenResized:YES).

See that's the iOS developer in me talking where we would have to drop down to CG to get this done. I didn't even consider using the higher level Mac methods for this ;)

Dave

---
twitter: @daveverwer
mail: dave....@shinydevelopment.com
web: http://shinydevelopment.com

Shiny Development Ltd. is registered in England, Registration Number: 05805802. Registered Office: 4 Claridge Court, Lower Kings Road, Berkhamsted, Herts., HP4 2AF.


On Tuesday, 28 August 2012 at 15:39, Ciarán Walsh wrote:

> I’m not sure how addRepresentation: works but you could try just filling the NSImage with white before calling it. Since the result is transparent, it might be added over the existing contents of the image?
>
> It would be something like this:
>
> [pdfImage lockFocus];
> [[NSColor whiteColor] set];
> NSRectFill((NSRect){ NSZeroPoint, size });
> [pdfImage unlockFocus];
>
> If that works, then presumably to handle the resizing you would be able to just -setSize: on the image before writing it out (as you’ve already called -setScalesWhenResized:YES).
>
> Ciarán
>
> On 28 Aug 2012, at 09:09, Dave Verwer wrote:
>
> > Hi Will
> >
> > I believe the solution to both of these issues will be the same. You need to create a new bitmap in memory and render the image into it. You will have to drop down to Core Graphics (C) to get this done though but it's not too hard.
> >
> > Something like this:
> >
> > * Create an image with CGBitmapContextCreate of the size you want.
> > * Fill the image with white using CGContextSetFillColorWithColor and CGContextFillRect.
> > * Draw your image over the top with CGContextDrawImage.
> > * Create an image back from your context using CGBitmapContextCreateImage and - initWithCGImage:size: on NSImage.
> > * Don't forget to release all of your core foundation objects as ARC does not apply to core graphics/core foundation. See CGContextRelease, CGImageRelease, etc...
> >
> > Hope this helps.
> > Dave
> >
> >
> > ---
> > twitter: @daveverwer
> > mail: dave....@shinydevelopment.com (mailto:dave....@shinydevelopment.com)
Reply all
Reply to author
Forward
0 new messages