2012-10-08 25 views
8

बनाने के लिए नमूना कोड मैं आईफोन के लिए नया हूं।पीडीएफ प्रोग्रामेटिक रूप से

क्या कोई नमूना कोड या ट्यूटोरियल है।

प्लिस्ट

के लिए नमूना कोड की भी आवश्यकता है कृपया मेरी मदद करें।

+1

उस प्रश्न का उत्तर देना असंभव है। आप क्या करना चाहते हैं? हमें बताए बिना, आप हमें क्या जवाब देने की उम्मीद करते हैं? – arkascha

+0

आप पहली बार Google के साथ खोज करते हैं .... टाइप प्रकारों से बचने का प्रयास करें http://www.ioslearner.com/generate-pdf-programmatically-iphoneipad/ पीडीएफ निर्माण – Spynet

+0

के लिए आपको वास्तव में क्या चाहिए एक अच्छी आईओएस प्रोग्रामिंग पुस्तक है, वास्तव में , लेकिन फिर भी मैं मदद कर सकता हूं जहां मैं कर सकता हूं। –

उत्तर

13

यहाँ मैं कैसे iPhone में .pdf फ़ाइल :)

PdfGenerationDemoViewController.h

#import <UIKit/UIKit.h> 

#define kBorderInset   20.0 
#define kBorderWidth   1.0 
#define kMarginInset   10.0 

//Line drawing 
#define kLineWidth    1.0 

@interface PdfGenerationDemoViewController : UIViewController 
{ 
    CGSize pageSize; 
} 

- (IBAction)generatePdfButtonPressed:(id)sender; 

@end 

PdfGenerationDemoViewController.m

#import "PdfGenerationDemoViewController.h" 

@interface PdfGenerationDemoViewController (Private) 
- (void) generatePdfWithFilePath: (NSString *)thefilePath; 
- (void)drawPageNumber:(NSInteger)pageNum; 
- (void) drawBorder; 
- (void) drawText; 
- (void) drawLine; 
- (void) drawHeader; 
- (void) drawImage; 
@end 

@implementation PdfGenerationDemoViewController 

#pragma mark - Private Methods 
- (void) drawBorder 
{ 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
    UIColor *borderColor = [UIColor brownColor]; 

    CGRect rectFrame = CGRectMake(kBorderInset, kBorderInset, pageSize.width-kBorderInset*2, pageSize.height-kBorderInset*2); 

    CGContextSetStrokeColorWithColor(currentContext, borderColor.CGColor); 
    CGContextSetLineWidth(currentContext, kBorderWidth); 
    CGContextStrokeRect(currentContext, rectFrame); 
} 

- (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(kBorderInset, 
            pageSize.height - 40.0, 
            pageSize.width - 2*kBorderInset, 
            pageNumberStringSize.height); 

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

- (void) drawHeader 
{ 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBFillColor(currentContext, 0.3, 0.7, 0.2, 1.0); 

    NSString *textToDraw = @"Pdf Demo - iOSLearner.com"; 

    UIFont *font = [UIFont systemFontOfSize:24.0]; 

    CGSize stringSize = [textToDraw sizeWithFont:font constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset) lineBreakMode:UILineBreakModeWordWrap]; 

    CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height); 

    [textToDraw drawInRect:renderingRect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft]; 
} 

- (void) drawText 
{ 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0); 

    NSString *textToDraw = @"Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum."; 

    UIFont *font = [UIFont systemFontOfSize:14.0]; 

    CGSize stringSize = [textToDraw sizeWithFont:font 
           constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset) 
            lineBreakMode:UILineBreakModeWordWrap]; 

    CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset + 50.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height); 

    [textToDraw drawInRect:renderingRect 
        withFont:font 
      lineBreakMode:UILineBreakModeWordWrap 
       alignment:UITextAlignmentLeft]; 

} 

- (void) drawLine 
{ 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    CGContextSetLineWidth(currentContext, kLineWidth); 

    CGContextSetStrokeColorWithColor(currentContext, [UIColor blueColor].CGColor); 

    CGPoint startPoint = CGPointMake(kMarginInset + kBorderInset, kMarginInset + kBorderInset + 40.0); 
    CGPoint endPoint = CGPointMake(pageSize.width - 2*kMarginInset -2*kBorderInset, kMarginInset + kBorderInset + 40.0); 

    CGContextBeginPath(currentContext); 
    CGContextMoveToPoint(currentContext, startPoint.x, startPoint.y); 
    CGContextAddLineToPoint(currentContext, endPoint.x, endPoint.y); 

    CGContextClosePath(currentContext);  
    CGContextDrawPath(currentContext, kCGPathFillStroke); 
} 

- (void) drawImage 
{ 
    UIImage * demoImage = [UIImage imageNamed:@"demo.png"]; 
    [demoImage drawInRect:CGRectMake((pageSize.width - demoImage.size.width/2)/2, 350, demoImage.size.width/2, demoImage.size.height/2)]; 
} 

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

    NSInteger currentPage = 0; 
    BOOL done = NO; 
    do 
    { 
     //Start a new page. 
     UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil); 

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

     //Draw a border for each page. 
     [self drawBorder]; 

     //Draw text fo our header. 
     [self drawHeader]; 

     //Draw a line below the header. 
     [self drawLine]; 

     //Draw some text for the page. 
     [self drawText]; 

     //Draw an image 
     [self drawImage]; 
     done = YES; 
    } 
    while (!done); 

    // Close the PDF context and write the contents out. 
    UIGraphicsEndPDFContext(); 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

- (IBAction)generatePdfButtonPressed:(id)sender 
{ 
    pageSize = CGSizeMake(612, 792); 
    NSString *fileName = @"Demo.pdf"; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName]; 

    [self generatePdfWithFilePath:pdfFileName]; 
} 
@end 
+1

यह वास्तव में एक अच्छा है। आपके उत्तर के लिए धन्यवाद। – THOMAS

+0

@THOMAS - सभी डी सर्वश्रेष्ठ ... और आपका स्वागत है :) –

+2

पीडीएफ – THOMAS

0

आप कर सकते हैं बनाने के लिए सबसे अच्छा उदाहरण है इसे थोड़ा उन्नत ट्यूटोरियल ढूंढें लेकिन this ठीक तुतीरी रे वेंडरलिच द्वारा अल वास्तव में आपकी मदद करेगा और जहां तक ​​प्लेट संबंधित है, आपको Google को पहले यह करना चाहिए क्योंकि आप उनमें से बहुत कुछ पा सकते हैं।

शुभकामनाएं,

फारुख Javeid

1

एप्पल के Drawing and Printing Guide अपने दोस्त है। असल में, यदि आप स्क्रीन पर आकर्षित कर सकते हैं, तो आप आईओएस (और मैक ओएस एक्स) पर एक पीडीएफ में आकर्षित कर सकते हैं।

0
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("iPhoneAppProgrammingGuide.pdf"), NULL, NULL); 
     PDFfile = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL); 
     CFRelease(pdfURL);  
CGPDFPageRef page = CGPDFDocumentGetPage(PDFfile,currentpage); 

     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); 
     CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFArtBox, [self bounds], 0, true)); 
     CGContextDrawPDFPage(context, page);  
     CGContextRestoreGState(context); 
     CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFMediaBox), 
                CGContextGetClipBoundingBox(context)); 

      CGContextConcatCTM(context, transform); 
      UIGraphicsBeginImageContext(CGSizeMake(self.bounds.size.width, self.bounds.size.height)); 
संबंधित मुद्दे