0

私は、複数ページのPDFを作成し、plistからデータを作成し、そのようなPDFを電子メールで送信する必要があるアプリケーションを開発しています。PDFに変換されるコンテンツには、テキストといくつかの画像が含まれています。私はこれに関するいくつかの投稿を見ましたが、それからあまり収集することができませんでした。誰かがこれを導くことができれば素晴らしいでしょう。

これが私が行ったPDF作成のコードです。

// create some sample data. In a real application, this would come from the database or an API. 
NSString* path = [[NSBundle mainBundle] pathForResource:@"sampleData" ofType:@"plist"];
NSDictionary* data = [NSDictionary dictionaryWithContentsOfFile:path];

NSLog(@"collections %@",data);
NSArray* students = [data objectForKey:@"Students"];
NSLog(@"collections students %@",students);



// get a temprorary filename for this PDF
path = NSTemporaryDirectory();

NSLog(@"path here %@",path);

self.pdfFilePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.pdf", [[NSDate date] timeIntervalSince1970]]];

NSLog(@"pdf file path and Name %@",self.pdfFilePath);

// Create the PDF context using the default page size of 612 x 792.
// This default is spelled out in the iOS documentation for UIGraphicsBeginPDFContextToFile
UIGraphicsBeginPDFContextToFile(self.pdfFilePath, CGRectZero, nil);

// get the context reference so we can render to it. 
CGContextRef context = UIGraphicsGetCurrentContext();

int currentPage = 0;

// maximum height and width of the content on the page, byt taking margins into account. 
CGFloat maxWidth = kDefaultPageWidth - kMargin * 2;
CGFloat maxHeight = kDefaultPageHeight - kMargin * 2;

// we're going to cap the name of the class to using half of the horizontal page, which is why we're dividing by 2
CGFloat classNameMaxWidth = maxWidth / 3;

// the max width of the grade is also half, minus the margin
CGFloat gradeMaxWidth = (maxWidth / 3) - kColumnMargin;
CGFloat yearMaxWidth = (maxWidth / 3) - kColumnMargin;


// only create the fonts once since it is a somewhat expensive operation
UIFont* studentNameFont = [UIFont boldSystemFontOfSize:17];
UIFont* classFont = [UIFont systemFontOfSize:15];

CGFloat currentPageY = 0;


UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil);
currentPageY = kMargin;

// iterate through out students, adding to the pdf each time. 
for (NSDictionary* student in students)
{

    // every student gets their own page 
    // Mark the beginning of a new page.


    NSString* title = [NSString stringWithFormat:@"Order List"];

    CGSize size2 = [title sizeWithFont:studentNameFont forWidth:maxWidth lineBreakMode:UILineBreakModeWordWrap];

    [title drawAtPoint:CGPointMake(kMargin, currentPageY) forWidth:maxWidth withFont:studentNameFont lineBreakMode:UILineBreakModeWordWrap];
    currentPageY += size2.height;

    CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]);
    CGContextMoveToPoint(context, kMargin, currentPageY);
    CGContextAddLineToPoint(context, kDefaultPageWidth - kMargin, currentPageY);
    CGContextStrokePath(context);

    // draw the student's name at the top of the page. 
    NSString* name = [NSString stringWithFormat:@"%@ %@", 
                      [student objectForKey:@"FirstName"], 
                      [student objectForKey:@"LastName"]];

    CGSize size = [name sizeWithFont:studentNameFont forWidth:maxWidth lineBreakMode:UILineBreakModeWordWrap];
    [name drawAtPoint:CGPointMake(kMargin, currentPageY) forWidth:maxWidth withFont:studentNameFont lineBreakMode:UILineBreakModeWordWrap];
    currentPageY += size.height;

    NSString* dob = [NSString stringWithFormat:@"Grade"];

    CGSize size1 = [name sizeWithFont:studentNameFont forWidth:maxWidth lineBreakMode:UILineBreakModeWordWrap];
    [dob drawAtPoint:CGPointMake(kMargin+ classNameMaxWidth + kColumnMargin, currentPageY) forWidth:maxWidth withFont:studentNameFont lineBreakMode:UILineBreakModeWordWrap];
    currentPageY += size1.height;

    NSString* year = [NSString stringWithFormat:@"Years"];

    CGSize size3 = [dob sizeWithFont:studentNameFont forWidth:maxWidth lineBreakMode:UILineBreakModeWordWrap];
    [year drawAtPoint:CGPointMake(kMargin+ classNameMaxWidth +gradeMaxWidth+ kColumnMargin, currentPageY) forWidth:maxWidth withFont:studentNameFont lineBreakMode:UILineBreakModeWordWrap];
    currentPageY += size3.height;

    // draw a one pixel line under the student's name
    CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]);
    CGContextMoveToPoint(context, kMargin, currentPageY);
    CGContextAddLineToPoint(context, kDefaultPageWidth - kMargin, currentPageY);
    CGContextStrokePath(context);

    // iterate through the list of classes and add these to the PDF. 
    NSArray* classes = [student objectForKey:@"Classes"];
    for(NSDictionary* class in classes)
    {
        NSString* className = [class objectForKey:@"Name"];
        NSString* grade = [class objectForKey:@"Grade"];
        NSString *year=[class objectForKey:@"Year"];

        // before we render any text to the PDF, we need to measure it, so we'll know where to render the
        // next line.
        size = [className sizeWithFont:classFont constrainedToSize:CGSizeMake(classNameMaxWidth, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

        // if the current text would render beyond the bounds of the page, 
        // start a new page and render it there instead
        if (size.height + currentPageY > maxHeight) {
            // create a new page and reset the current page's Y value
            UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil);
            currentPageY = kMargin;
        }

        // render the text
        [className drawInRect:CGRectMake(kMargin, currentPageY, classNameMaxWidth, maxHeight) withFont:classFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];

        // print the grade to the right of the class name
        [grade drawInRect:CGRectMake(kMargin + classNameMaxWidth + kColumnMargin, currentPageY, gradeMaxWidth, maxHeight) withFont:classFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
       [year drawInRect:CGRectMake(kMargin +classNameMaxWidth*2+ kColumnMargin , currentPageY, yearMaxWidth, maxHeight) withFont:classFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];


        currentPageY += size.height;

    }


    // increment the page number. 
    currentPage++;

}
4

2 に答える 2

1

FDPF を使用できます。 http://www.fpdf.org/

マニュアルを読んでください。途中で問題が発生した場合は、投稿してください。

喜んでお手伝いします:-)

于 2012-04-23T09:35:09.727 に答える
0
CGPDFDocumentRef PDFfile;
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("iPhoneAppProgrammingGuide.pdf"), NULL, NULL);
    PDFfile = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
    CFRelease(pdfURL);
    CGContextRef context;
   totalpagenumper=CGPDFDocumentGetNumberOfPages(PDFfile);

 for( i=1;i<=totalpagenumper;i++)
    {

      imageview=[[UIImageView alloc]initWithFrame:CGRectMake((self.frame.size.width/3),30+n,self.frame.size.width/3,160)];

        UILabel *lable=[[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width/1.5,n+20+self.frame.size.height/6 , 30, 20)];
        [scrollview addSubview:lable];
        lable.text=[NSString stringWithFormat:@"%i", i];
        lable.backgroundColor=[UIColor clearColor];
        lable.textAlignment=UITextAlignmentCenter;
        n=n+30+self.frame.size.height/6;
        imageview.userInteractionEnabled=YES;
        //_________________________________***************************_______________**************************_____________________________
         totalpagenumper=CGPDFDocumentGetNumberOfPages(PDFfile);

        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        context = CGBitmapContextCreate(NULL,self.bounds.size.width,self.bounds.size.height,8,self.bounds.size.width * 4,colorSpace,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
        CGColorSpaceRelease(colorSpace);
        CGContextClipToRect(context, CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height));

        CGPDFPageRef page = CGPDFDocumentGetPage(PDFfile,i);
        context = UIGraphicsGetCurrentContext();
        CGContextSaveGState(context);
        CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
        CGContextFillRect(context,[self bounds]);   
        CGContextTranslateCTM(context, -1.0, [self bounds].size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFMediaBox),
                                                CGContextGetClipBoundingBox(context));

        CGContextConcatCTM(context, transform);

        CGContextDrawPDFPage(context, page);

        CGContextRestoreGState(context);
        UIGraphicsBeginImageContext(CGSizeMake(self.bounds.size.width, self.bounds.size.height));


        UIGraphicsEndImageContext();

        //_________________________________***************************_______________**************************_____________________________
        CGImageRef imgref;
        imgref=CGBitmapContextCreateImage(context);       

        imageview.tag=i;

        image= [[UIImage alloc]initWithCGImage:imgref ];
        imageview.image=image;

        scrollview.backgroundColor=[UIColor orangeColor];
        [scrollview addSubview:imageview];
        tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(returnpage:)];
        [imageview addGestureRecognizer:tapGR];                

        [scrollview addSubview:imageview1];
        imageview1.frame=CGRectMake(self.frame.size.width/1.5,30, 30, 30);
        arrow=[UIImage imageNamed:@"selectarrow.png"];
        imageview1.image=arrow; 

    }

i am use this source cod to convert pdf file in to image,to change the imageview size to get large images in image view
于 2012-09-03T13:02:36.757 に答える