Re: NSImage to PDFPage in Tiger?
Re: NSImage to PDFPage in Tiger?
- Subject: Re: NSImage to PDFPage in Tiger?
- From: Jerry LeVan <email@hidden>
- Date: Sat, 22 Dec 2007 22:28:56 -0500
On Dec 22, 2007, at 3:29 PM, Nick Zitzmann wrote:
On Dec 21, 2007, at 11:56 AM, Jerry LeVan wrote:
I don't seem to be able to find a recipe for generating a PDFPage
from a
NSImage using methods available in the 10.4 Universal SDK that give
the same
results as the -initWithImage method in PDFPage. ie generate a
PDFPage from
a NSImage.
Could someone point me in the right direction?
I haven't tried this, but the first thing I would try would be to
put the NSImage into an NSImageView, then call -
dataWithPDFInsideRect: to turn the image into a PDF, then create a
PDFDocument with the data, and then get the resulting PDFPage from
the PDFDocument... This is, of course, assuming the NSImage has only
a bitmap representation.
Nick Zitzmann
<http://www.chronosnet.com/>
It turned out to be a bit nasty since QuartzFilterManager is not
available in SDK 10.4
but a few minutes ago I ran the following code and it generated a nice
small pdf (1.8 MB) out
of a twenty four page graphic novel ( 24 jpgs)
Sorry for the formatting, I need to make a cleanup pass over the code...
#ifdef UNIVERSAL
-(IBAction) MakePDF:(id)sender
{
// lets try to make some pdf
NSImage * theImage;
NSString *outputFile; // user selected file name
NSString * theCommand = @"/System/Library/Printers/Libraries/
quartzfilter";
NSString * theFilter = [NSHomeDirectory() stringByAppendingString:@"/
Library/Filters/ImageBrowser.qfilter"];
NSString *tmpFile = [NSTemporaryDirectory() stringByAppendingString:
[[NSProcessInfo processInfo] globallyUniqueString]];
//NSLog(@"tmpName: %@",tmpName);
// get the filename for output
NSSavePanel * panel= [NSSavePanel savePanel];
[panel setRequiredFileType:@"pdf"];
if( [panel runModal] ==NSFileHandlingPanelOKButton){
outputFile = [NSString stringWithString:[panel filename]]; //
need to use "files"?
}else return;
NSMutableArray *args = [NSMutableArray array];
[args addObject:tmpFile];
[args addObject:theFilter];
[args addObject:outputFile];
NSBundle *thisBundle = [NSBundle bundleForClass:[self class]];
NSString * emptypdf = [thisBundle pathForResource:@"Empty.pdf"
ofType:nil] ;
PDFDocument * myBook = [[PDFDocument alloc] initWithData:[NSData
dataWithContentsOfFile:emptypdf]];
[myBook removePageAtIndex:0];
//Load the progress counter
int cnt= [fileNameList count];
if(!thePanel) {
[NSBundle loadNibNamed: @"FileCount" owner:self];
}
[thePanel orderFront:self];
[progressInd setUsesThreadedAnimation:YES];
[progressInd startAnimation:self];
int fcnt = [fileNameList count];
int i;
for(i=0;i< cnt;i++) {
theImage =[ [NSImage alloc] initWithContentsOfFile: [fileNameList
objectAtIndex:i]];
NSImageView *pdfView = [[NSImageView alloc]
initWithFrame:NSMakeRect(0,0,
[theImage size].width,
[theImage size].height)];
[pdfView setImage:theImage];
NSData *pdfData;
pdfData = [pdfView dataWithPDFInsideRect:[pdfView bounds]];
PDFDocument * tmpPage = [[PDFDocument alloc] initWithData: pdfData];
[myBook insertPage:[ tmpPage pageAtIndex: 0] atIndex: i];
[pdfView release];
[tmpPage release];
[theImage release];
[filesLeft setIntValue: --fcnt];
[filesLeft displayIfNeeded];
}
[progressInd stopAnimation:self];
[thePanel orderOut:self]; // hide the panel
if(!theSavePanel) [NSBundle loadNibNamed:@"writePDF" owner: self];
[theSavePanel orderFront:self];
[saveInd setUsesThreadedAnimation:YES];
[saveInd startAnimation:self];
BOOL writeOK;
/* This won't work in the Universal SDK
NSArray *filters = [QuartzFilterManager filtersInDomains:[NSArray
arrayWithObject:@"QuartzFilterPDFWorkflowDomain"]];
int search = [self findSpecialFilter: filters byName:@"ImageBrowser"];
if ( search != -1 ) {
QuartzFilter *f = [filters objectAtIndex:search];
NSDictionary * optionsDictWithQuartzFilter = [NSDictionary
dictionaryWithObject: f forKey:@"QuartzFilter"];
writeOK=[myBook writeToFile:outputFile
withOptions:optionsDictWithQuartzFilter ];
}
else {
writeOK=[myBook writeToFile:outputFile];
}
*/
writeOK=[myBook writeToFile:tmpFile];
// run special filter to compress jpgs and lighten images
[self runCommand: theCommand withArguments:args];
NSFileManager *manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:tmpFile ])
[manager removeFileAtPath:tmpFile handler:nil];
[saveInd stopAnimation:self];
[theSavePanel orderOut:self];
if(writeOK)
NSRunInformationalAlertPanel(@"Write Complete.
",outputFile,@"OK",nil,nil);
else
NSRunInformationalAlertPanel(@"Write Failed.
",outputFile,@"OK",nil,nil);
[myBook release];
}
#else
-(IBAction) MakePDF:(id)sender
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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