My #iOS PDF programming posts are coming to and end. This last tells you how to to finish it.

Pdf-logo

I've combined the iPhone 4 Development Essesials for XCode  Chapters 32 and 33, with  Apple's print tutorial for iOS. Most of it is wrapped into the following savePdfFile method:


- (IBAction)savePDFFile:(id)sender

{

    NSString* path = [[NSBundle mainBundle] pathForResource:@"sampleData" ofType:@"plist"];

    

    // get a temprorary filename for this PDF

    path = NSTemporaryDirectory();

    self.pdfFilePath = [path stringByAppendingPathComponent:

                        [NSString stringWithFormat:@"%d.pdf"

                         [[NSDate date

                          timeIntervalSince1970] ]];

    

    // Prepare the text using a Core Text Framesetter

    CFAttributedStringRef currentText = CFAttributedStringCreate(NULL

                                                                 (CFStringRef)textView.text, NULL);

    if (currentText) {

        CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);

        if (framesetter) {

            

            NSString* pdfFileName = self.pdfFilePath; //[NSString stringWithString:@"test.pdf"];

            

            // Create the PDF context using the default page: currently constants at the size 

            // of 612 x 792.

            UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);

            

            CFRange currentRange = CFRangeMake(0, 0);

            NSInteger currentPage = 0;

            BOOL done = NO;

            

            do {

                // Mark the beginning of a new page.

                UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth

                                                          kDefaultPageHeight), nil);

                

                // Draw a page number at the bottom of each page

                currentPage++;

                [self drawPageNumber:currentPage];

                

                // Render the current page and update the current range to

                // point to the beginning of the next page.

                currentRange = [self renderPage:currentPage withTextRange:

                                currentRange andFramesetter:framesetter];

                

                // If we're at the end of the text, exit the loop.

                if (currentRange.location == CFAttributedStringGetLength

                    ((CFAttributedStringRef)currentText))

                    done = YES;

            } while (!done);

            

            // Close the PDF context and write the contents out.

            UIGraphicsEndPDFContext();

            

            // Release the framewetter.

            CFRelease(framesetter);

            

        } else {

            NSLog(@"Could not create the framesetter needed to lay out the atrributed string.");

        }

        // Release the attributed string.

        CFRelease(currentText);

    } else   NSLog(@"Could not create the attributed string for the framesetter");

}


The whole source is available at https://github.com/mortjac/PDF-tutorial. So now you can contribute and make the project better. Thank you for patiently reading my posts!

       

Populære innlegg fra denne bloggen

Predicates in Core Data

How To Disable Google’s Personalization Of Search Results

InvestorForum by VentureLab in Oslo a success story #startups