开发者

How to display and create text and images into PDF in iPhone?

I want to开发者_如何学JAVA create & display PDF by importing text & images in it. Is there any code or document which can help me ? Thanks ....


Have a look at Adobe PDF Library SDK - http://www.adobe.com/devnet/pdf/library.html

John


I do realize that this question is quite old, bud I noticed it hasn't been answered.

A PDF can be created in iOS using CoteText. There is a fairly good tutorial on how to create a PDF and add some text to it in Apple's iOS developer library. I've found this tutorial to be slightly outdated, and not answering the question completely, so I've put together some code that covers creating a PDF, drawing text, drawing an image and displaying the PDF.

The code below will draw a PDF with an image and text. Then save it to the provided.

+(void)drawPDF:(NSString*)fileName
{
    // Create the PDF context using the default page size of 612 x 792
    UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil);
    // Mark the beginning of a new page with a sample size
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

    [self drawTextWithString:@"Hello World" inFrame:CGRectMake(20, 20, 300, 50)];

    UIImage* image = [UIImage imageNamed:@"helloWorld.png"];
    CGRect frame = CGRectMake(30, 30, 200, 50);

    [self drawImage:image inRect:frame];

    UIGraphicsEndPDFContext();
}

This is the code that draws the text. Note that it doesn't take into account clipping or wrapping, so if the string is larger than the frame, it will be still drawn.

+(void)drawTextWithString:(NSString *)stringToDraw inFrame:(CGRect)frameRect {
    UIFont *theFont = [UIFont systemFontOfSize:12];

    NSDictionary *attributes = @{ NSFontAttributeName: theFont};

    // The text will be drawin in the frameRect, where (0,0) is the top left corner
    [stringToDraw drawInRect:frameRect withAttributes:attributes];
}

And this is the code that draws the image. It's pretty simple.

+(void)drawImage:(UIImage*)image inRect:(CGRect)rect {
    [image drawInRect:rect];
}

The function below will render a PDF using a UIWebView.

-(void)renderPdfFile:(NSString *)fileName
{
    // This is here for demo purposes only. In a real world example the UIWebView
    // will probably be linked directly from the story board
    UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

    NSURL *url = [NSURL fileURLWithPath:fileName];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView setScalesPageToFit:YES];
    [webView loadRequest:request];

    [self.view addSubview:webView];
}

If you are interested in rendering PDF files I have another posts here, that might be useful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜