Pdf文件页码

时间:2013-01-30 04:54:29

标签: objective-c pdf pdf-generation

我是开发iOS应用的新开发人员。我正在使用以下代码创建pdf。创建pdf并同时显示数据。但我需要显示每个pdf页面的页码。请帮帮我。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *saveDirectory = [paths objectAtIndex:0];
    NSString *filename =@"Gp By SalesPerson Summary";
    filename = [filename stringByAppendingString:@".pdf"]; 
    NSLog(@"Filename= %@",filename);
    NSString *saveFileName = filename;
    NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName];
    const char *filenm = [newFilePath UTF8String];

    CGRect pageRect = CGRectMake(0, 0, 712,792);

    // This code block sets up our PDF Context so that we can draw to it
    CGContextRef pdfContext;
    CFStringRef path;
    CFURLRef url;
    CFMutableDictionaryRef myDictionary = NULL;

    // Create a CFString from the filename we provide to this method when we call it
    path = CFStringCreateWithCString (NULL, filenm,
                                      kCFStringEncodingUTF8);

    // Create a CFURL using the CFString we just defined
    url = CFURLCreateWithFileSystemPath (NULL, path,
                                         kCFURLPOSIXPathStyle, 0);
    NSLog(@"path= %@",path);
    CFRelease (path);

    // This dictionary contains extra options mostly for 'signing' the PDF
    myDictionary = CFDictionaryCreateMutable(NULL, 0,
                                             &kCFTypeDictionaryKeyCallBacks,
                                             &kCFTypeDictionaryValueCallBacks);
    CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
    CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));

    // Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary 
    pdfContext = CGPDFContextCreateWithURL(url, &pageRect, myDictionary);


    NSLog(@"%@",url);
    CFRelease(myDictionary);
    CFRelease(url);


    //pdf page creation code starts here…

    //I have stored the product details in array aryPrdCode,aryPrdName,aryPrdImageData
    filterArray = [[NSMutableArray alloc]init];
    NSLog(@"arrayCoutnt:%d",[array_spName count]);
    int count ;
    for(int i = 0; i<[namesArray count]; i++)
    {
         [filterArray removeAllObjects];
        int perPage;
        for(int j=0; j<[filArray count]; j++)
        {
            if([[namesArray objectAtIndex:i] isEqualToString:[[filArray objectAtIndex:j]objectForKey:@"EmployeeName"]])
            {

                [filterArray addObject:[filArray objectAtIndex:j]];


            }
        }
        revenueSum = 0;
        cogsSum = 0;
        marginSum = 0;
        marginPerSum = 0;
        for(int j=0; j<4; j++)
        {
            for(int i=0; i<[filterArray count]; i++)
            {
                if(j==0)
                    revenueSum = revenueSum +[[[filterArray objectAtIndex:i] objectForKey:@"Revenue"] floatValue];
                else if(j==1)
                    cogsSum = cogsSum +[[[filterArray objectAtIndex:i] objectForKey:@"COGS"] floatValue];
                else if(j==2)
                    marginSum = marginSum +[[[filterArray objectAtIndex:i] objectForKey:@"GP"] floatValue];
                else if(j==3)
                    marginPerSum = marginPerSum +[[[filterArray objectAtIndex:i] objectForKey:@"MarginPer"] floatValue];

            }
        }

        NSLog(@"filterArray:%@ %d",filterArray,[filterArray count]);
        perPage = 17; //Number of products to be shown per page
        count = [filterArray count];
        float pages = ((float)count / (float)perPage);

        int pagesInPdf = [[NSNumber numberWithFloat:ceilf(pages)] intValue];

        CGContextBeginPage (pdfContext, &pageRect);
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"dd/MM/yyyy"];
        NSString *theDate = [dateFormat stringFromDate:[NSDate date]];
        // Header fields like customerId, REP name etc

        const char *repName = [[NSString stringWithFormat:@"%@",[[filterArray objectAtIndex:0]objectForKey:@"EmployeeName"]] UTF8String];
        //const char *salesOrder = [[NSString stringWithFormat:@"Sales: %@",@"For Ship Out"] UTF8String];
        //const char *customerId = [[NSString stringWithFormat:@"Customer-Id: %@",@"444"] UTF8String];
        const char *dolOn = [[NSString stringWithFormat:@"Date: %@",theDate] UTF8String];
        // const char *orderAmount = [[NSString stringWithFormat:@"Order Total: $%@",@"10000"] UTF8String];

        CGContextSelectFont (pdfContext, "Helvetica", 14, kCGEncodingMacRoman);

        CGContextShowTextAtPoint (pdfContext, 10 , 760, repName, strlen(repName));
        //CGContextShowTextAtPoint (pdfContext, 230 , 780, salesOrder, strlen(salesOrder));
        // CGContextShowTextAtPoint (pdfContext, 500 , 780, customerId, strlen(customerId));   
        CGContextShowTextAtPoint (pdfContext, 230 , 760, dolOn, strlen(dolOn));
        // CGContextShowTextAtPoint (pdfContext, 270 , 760 , orderAmount, strlen(orderAmount));


        CGContextSelectFont (pdfContext, "Helvetica", 15, kCGEncodingMacRoman); // this code generates some error check the error below think coz it is unable to find font
        CGContextSetTextDrawingMode (pdfContext, kCGTextFill);

        //Drawing Headings in a Table

        CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1);  

        UIImage *headingStrip = [UIImage imageNamed:@"gray-strip-horizontal.png"];

        CGContextDrawImage(pdfContext, CGRectMake(0, 715, 712, 39), headingStrip.CGImage);

        CGContextShowTextAtPoint (pdfContext, 20 , 730, [@"SalesPerson" UTF8String], strlen([@"SalesPerson" UTF8String]));
        CGContextShowTextAtPoint (pdfContext, 211 , 730, [@"Revenue $" UTF8String], strlen([@"Revenue $" UTF8String]));
        CGContextShowTextAtPoint (pdfContext, 330 , 730, [@"COGS $" UTF8String], strlen([@"COGS $" UTF8String]));   
        CGContextShowTextAtPoint (pdfContext, 460 , 730, [@"GP Margin $" UTF8String], strlen([@"GP Margin $" UTF8String]));
        CGContextShowTextAtPoint (pdfContext, 600 , 730 ,[@"GP Margin %" UTF8String], strlen([@"GP Margin %" UTF8String]));


        const char *borderLine = [@"-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------" UTF8String];
        const char *borderLine1 = [@"  -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------" UTF8String];

        UIImage *columnImg = [UIImage imageNamed:@"gray-strip-virticle.png"];



        int k=0;
        //Drawing Rows in a Table

        CGContextSelectFont (pdfContext, "Helvetica", 12, kCGEncodingMacRoman);
        CGContextSetRGBFillColor (pdfContext, 0., 0, 0, 1);
        NSLog(@"pagesInPdf;%d",pagesInPdf);

        for(int j=0;j<pagesInPdf;j++)
        {


            if(j!=0)
            {  
                //from 2nd page this code will call

                CGContextBeginPage (pdfContext, &pageRect);
                CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
                CGContextSelectFont (pdfContext, "Helvetica", 12, kCGEncodingMacRoman);
                CGContextSetRGBFillColor (pdfContext, 0., 0, 0, 1);  

            }


            int noOfRowsInPage;
            if(j!=(pagesInPdf-1)){
                noOfRowsInPage= perPage;
                NSLog(@"Number of Rows In Pages=-=-:%d",noOfRowsInPage);}
            else
            {
                noOfRowsInPage = [filterArray count]-j*perPage;
                NSLog(@"Number of Rows In Pages:%d",noOfRowsInPage);
            }
            NSLog(@"numberOfRows In Pages:%d",noOfRowsInPage);
            for(int i =0;i<noOfRowsInPage;i++)
            {

                NSLog(@"kkkkkkk:%d  %d",k,j);
                  k++;
                const char *itemNo = [[[filterArray objectAtIndex:j*perPage+i] objectForKey:@"EmployeeName"]  UTF8String];
                const char *description = [[[filterArray objectAtIndex:j*perPage+i] objectForKey:@"Revenue"] UTF8String];
                const char *price = [[[filterArray objectAtIndex:j*perPage+i] objectForKey:@"COGS"] UTF8String];
                const char *maxinv = [[[filterArray objectAtIndex:j*perPage+i] objectForKey:@"GP"] UTF8String];
                const char *qtyorder = [[[filterArray objectAtIndex:j*perPage+i] objectForKey:@"MarginPer"] UTF8String];





                if(j==0)
                {
                    //1st Page
                    CGContextShowTextAtPoint (pdfContext, 20 , 695-i*40, itemNo, strlen(itemNo));
                    CGContextDrawImage(pdfContext, CGRectMake(200, 675-i*40, 1, 40), columnImg.CGImage);
                    CGContextShowTextAtPoint (pdfContext, 211 , 695-i*40, description, strlen(description));
                    CGContextDrawImage(pdfContext, CGRectMake(320, 675-i*40, 1, 40), columnImg.CGImage);
                    CGContextShowTextAtPoint (pdfContext, 330 , 695-i*40, maxinv, strlen(maxinv));   

                    CGContextDrawImage(pdfContext, CGRectMake(450, 675-i*40, 1, 40), columnImg.CGImage);
                    CGContextShowTextAtPoint (pdfContext, 460 , 695-i*40, qtyorder, strlen(qtyorder));
                    CGContextDrawImage(pdfContext, CGRectMake(590, 675-i*40, 1, 40), columnImg.CGImage);
                    CGContextShowTextAtPoint (pdfContext, 600 , 695-i*40, price, strlen(price));


                    CGContextShowTextAtPoint (pdfContext, 0 , 673-i*40, borderLine, strlen(borderLine));
                    CGContextShowTextAtPoint (pdfContext, 0 , 673-i*40, borderLine1, strlen(borderLine1));

                }

                else
                {
                    //2nd,3rd etc..Pages
                    CGContextShowTextAtPoint (pdfContext, 20 , 750-i*40, itemNo, strlen(itemNo));
                    CGContextDrawImage(pdfContext, CGRectMake(200, 735-i*40, 1, 40), columnImg.CGImage);
                    CGContextShowTextAtPoint (pdfContext, 211 , 750-i*40, description, strlen(description));
                    CGContextDrawImage(pdfContext, CGRectMake(320, 735-i*40, 1, 40), columnImg.CGImage);
                    CGContextShowTextAtPoint (pdfContext, 330 , 750-i*40, maxinv, strlen(maxinv));   
                    CGContextDrawImage(pdfContext, CGRectMake(450, 735-i*40, 1, 40), columnImg.CGImage);
                    CGContextShowTextAtPoint (pdfContext, 460 , 750-i*40,qtyorder, strlen(qtyorder));
                    CGContextDrawImage(pdfContext, CGRectMake(590, 735-i*40, 1, 40), columnImg.CGImage);
                    CGContextShowTextAtPoint (pdfContext, 600 , 750-i*40, price, strlen(price));

                    CGContextShowTextAtPoint (pdfContext, 0 , 733-i*40, borderLine, strlen(borderLine));
                    CGContextShowTextAtPoint (pdfContext, 0 , 733-i*40, borderLine1, strlen(borderLine1));
                }


            }
            int row;
            row = noOfRowsInPage;
            NSString *str = @"Total";
            if(row == noOfRowsInPage)
            {
                NSLog(@"MarginSum:%f",marginSum);
                NSLog(@"CogsSum:%f",cogsSum);
                NSLog(@"RevenueSum:%f",revenueSum);
                NSLog(@"MargenPer:,%f",marginPerSum);
                const char *itemNo = [str UTF8String];
                const char *description = [[NSString stringWithFormat:@"$%0.2f",revenueSum] UTF8String];
                const char *price = [[[NSString stringWithFormat:@"%0.2f",(marginSum/revenueSum *100)]stringByAppendingString:@"%"] UTF8String];
                const char *maxinv = [[NSString stringWithFormat:@"$%0.2f",cogsSum] UTF8String];
                const char *qtyorder = [[NSString stringWithFormat:@"$%0.2f",marginSum] UTF8String];

                if([[NSString stringWithFormat:@"$%f",revenueSum]  length]>17)
                {
                    CGContextSelectFont (pdfContext, "Helvetica-Bold", 10, kCGEncodingMacRoman);
                    CGContextSetRGBFillColor (pdfContext, 0., 0, 0, 1);  
                    NSLog(@"rowCount is:%d",row); 
                }
                else {
                    CGContextSelectFont (pdfContext, "Helvetica-Bold", 13, kCGEncodingMacRoman);
                    CGContextSetRGBFillColor (pdfContext, 0., 0, 0, 1);  
                    NSLog(@"rowCount is:%d",row);
                }


                if(j == pagesInPdf-1)
                {
                    if(j == 0)
                    {
                        //1st Page Total---->> Footer
                        CGContextShowTextAtPoint (pdfContext, 20 , 695-row*40, itemNo, strlen(itemNo));
                        CGContextDrawImage(pdfContext, CGRectMake(200, 675-row*40, 1, 40), columnImg.CGImage);
                        CGContextShowTextAtPoint (pdfContext, 211 , 695-row*40, description, strlen(description));
                        CGContextDrawImage(pdfContext, CGRectMake(320, 675-row*40, 1, 40), columnImg.CGImage);
                        CGContextShowTextAtPoint (pdfContext, 330 , 695-row*40, maxinv, strlen(maxinv));   
                        CGContextDrawImage(pdfContext, CGRectMake(450, 675-row*40, 1, 40), columnImg.CGImage);
                        CGContextShowTextAtPoint (pdfContext, 460 , 695-row*40, qtyorder, strlen(qtyorder));
                        CGContextDrawImage(pdfContext, CGRectMake(590, 675-row*40, 1, 40), columnImg.CGImage);
                        CGContextShowTextAtPoint (pdfContext, 600 , 695-row*40, price, strlen(price));


                        CGContextShowTextAtPoint (pdfContext, 0 , 673-row*40, borderLine, strlen(borderLine));
                        CGContextShowTextAtPoint (pdfContext, 0 , 673-row*40, borderLine1, strlen(borderLine1)); 
                    }
                    else {
                        //2nd,3rd etc.. Pages Total---->>Footer
                        CGContextShowTextAtPoint (pdfContext, 20 , 750-row*40, itemNo, strlen(itemNo));
                        CGContextDrawImage(pdfContext, CGRectMake(200, 735-row*40, 1, 40), columnImg.CGImage);
                        CGContextShowTextAtPoint (pdfContext, 211 , 750-row*40, description, strlen(description));
                        CGContextDrawImage(pdfContext, CGRectMake(320, 735-row*40, 1, 40), columnImg.CGImage);
                        CGContextShowTextAtPoint (pdfContext, 330 , 750-row*40, maxinv, strlen(maxinv));   
                        CGContextDrawImage(pdfContext, CGRectMake(450, 735-row*40, 1, 40), columnImg.CGImage);
                        CGContextShowTextAtPoint (pdfContext, 460 , 750-row*40, qtyorder, strlen(qtyorder));
                        CGContextDrawImage(pdfContext, CGRectMake(590, 735-row*40, 1, 40), columnImg.CGImage);
                        CGContextShowTextAtPoint (pdfContext, 600 , 750-row*40, price, strlen(price));


                        CGContextShowTextAtPoint (pdfContext, 0 , 733-row*40, borderLine, strlen(borderLine));
                        CGContextShowTextAtPoint (pdfContext, 0 , 733-row*40, borderLine1, strlen(borderLine1));
                    }


                }
            }
            CGContextEndPage (pdfContext);

        }

    }



    CGContextRelease (pdfContext);  

1 个答案:

答案 0 :(得分:1)

用于显示页码:

- (void) generatePdfWithFilePath: (NSString *)thefilePath
{
    UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);

    NSInteger currentPage = 0;


        //Draw a page number at the bottom of each page.
        currentPage++;
        [self drawPageNumber:currentPage];


}

- (void)drawPageNumber:(NSInteger)pageNumber
{
    NSString* pageNumberString = [NSString stringWithFormat:@"Page %d", pageNumber];
    UIFont* theFont = [UIFont systemFontOfSize:12];

    CGSize pageNumberStringSize = [pageNumberString sizeWithFont:theFont
                                   constrainedToSize:pageSize
                                       lineBreakMode:UILineBreakModeWordWrap];

    CGRect stringRenderingRect = CGRectMake(20,
                                   pageSize.height - 40.0,
                                   pageSize.width - 2*20,
                                   pageNumberStringSize.height);

    [pageNumberString drawInRect:stringRenderingRect withFont:theFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
}