Okay, after a bunch of struggles, I got my code to work. What I wanted was to make a multi-page pdf document from a bunch of text. I wanted a method that would take an array of strings and make a pdf page from each string, then join them all together into a single pdf document.

My original plan was to iterate through the text chunk array, making PDFPages from each one, and adding them to the PDFDocument, but that did not work (I forget now why not, although that is how I am doing it now. Should have taken notes.) Since that was not working, and since I saw another example on CocoaDev, I tried putting the PDFPages in a separate array, then iterating through that array to get the pages to add to the document. That seemed to work on my Tiger- PowerBook but not on my Leopard-intelMini, so I went back to the add- pdfpage-to-doc-as-you-go method, which now works. I must have made some changes on the way back, but I did not keep track of them.

So, this method seems to work, but I still have some questions. I have not mastered ObjC memory management, and I am not convinced that I am doing this correctly. Can any take a look at this and comment on how I am allocating, initing, releasing and autoreleasing the various objects here?

Here is the working code, with the nonworking code commented out, and some other comments, too:
//==========================================================
- (BOOL) renderToPDFFile: (NSArray*) textChunks :(NSString*) file {
// NSMutableArray *pdfpages = [[NSMutableArray alloc] init]; // not using this anymore
        NSRect r = NSMakeRect(0, 0, 600, 1);
        NSTextView* view = [[NSTextView alloc] initWithFrame:r];
        r = [view bounds];
PDFDocument *outputDoc = [[PDFDocument alloc] initWithData:[view dataWithPDFInsideRect:r]];

        int i;
        for(i = 0; i < [textChunks count]; i++) {
                NSTextView* v = [[NSTextView alloc] initWithFrame:r];
                NSString *s = [textChunks objectAtIndex:i];
                [v insertText:s];
                [v sizeToFit];          // this is important for me
                r = [v bounds];         // and it seems to work fine
                                
PDFDocument *tempDoc = [[PDFDocument alloc] initWithData:[v dataWithPDFInsideRect:r]];
                PDFPage *page = [[PDFPage alloc] initWithDocument:tempDoc];
                page = [tempDoc pageAtIndex:0];
        
                [outputDoc insertPage:page
                        atIndex:[outputDoc pageCount]]; // this now works

// [pdfpages addObject:page]; // originally, I was trying to hold on to the page objects in an array
//              [page autorelease];             // << WOULD THIS BE CORRECT?  
My reading of the
// mmgt stuff tells me yes, since the page is added to an array, its refcount is incremmented, // WAIT!! now it works without being released or autoreleased. HELP! // the rule says if I alloc it, I need to release it, so what's correct? Am I leaking?
                [tempDoc autorelease];
                [v autorelease];
        }
        
//      id p;
// NSEnumerator *e = [pdfpages objectEnumerator]; // here I intended to take each pdf page in the array and add it
//      while(p = [e nextObject]) {                             // to the main 
pdf document
// [outputDoc insertPage:p // this was crashing. seemed to be an array out of bounds error // atIndex:[outputDoc pageCount]]; // but I could not decipher the error message.
//      }       

        [outputDoc removePageAtIndex:0];                        // the first 
page was really a dummy
        NSData *data = [outputDoc dataRepresentation];
        BOOL b = [data writeToFile:file atomically:YES];        // this works 
fine

        [pdfpages autorelease];         // coments anyone?
[outputDoc autorelease]; // is this okay, or should I release instead, or ...? [view autorelease]; // if(alloced) release, right? isn't that the rule?
        return b;
}

If anyone has a few minutes to comment on this I would appreciate it. Some aspects of Cocoa memory management are still odd to me, like how to handle _______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to