【问题标题】:How to create PDF document using iPhone SDK? [duplicate]如何使用 iPhone SDK 创建 PDF 文档? [复制]
【发布时间】:2011-07-07 18:37:24
【问题描述】:

如何使用 iPhone SDK 创建 PDF 文档?

任何可以做到这一点的教程和示例都会有所帮助。

谢谢!

【问题讨论】:

    标签: iphone objective-c cocoa-touch ios4 iphone-sdk-3.0


    【解决方案1】:
    【解决方案2】:

    有一个很棒的用于创建 PDF 的开源 api。您可以将文本、图像和标题添加到 PDF,然后发送 PDF 或将其保存到文件中。 Api 位于http://code.google.com/p/pdf-api-iphonesdk/wiki/PDFAPI 如果您对此有任何疑问,我们的支持非常好,请联系 maor.kern@mobice.org

    【讨论】:

      【解决方案3】:

      您可以使用 UIKit 生成 pdf。它真的很简单。您只需要启动一个 pdf 上下文并向其写入内容即可:

      看到这个:http://www.ioslearner.com/generate-pdf-programmatically-iphoneipad/

      【讨论】:

        【解决方案4】:
        - (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];
        }
        - (void) generatePdfWithFilePath: (NSString *)thefilePath
        {
            UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
        
            NSInteger currentPage = 0;
            BOOL done = NO;
            do
            {
                // Mark the beginning of 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();
        }
        - (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) 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) 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) 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)];
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-06-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-06
          相关资源
          最近更新 更多