简体   繁体   English

如何在iPhone中以PDF显示和创建文本和图像?

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

I want to create & display PDF by importing text & images in it. 我想通过导入文本和图像来创建和显示PDF。 Is there any code or document which can help me ? 有什么代码或文档可以帮助我吗? Thanks .... 谢谢 ....

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. 可以使用CoteText在iOS中创建PDF。 There is a fairly good tutorial on how to create a PDF and add some text to it in Apple's iOS developer library . 在Apple的iOS开发人员库中,有一个相当不错的教程,介绍如何创建PDF并向其中添加一些文本。 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. 我发现本教程有些过时了,还没有完全回答问题,因此我整理了一些代码,内容涉及创建PDF,绘制文本,绘制图像和显示PDF。

The code below will draw a PDF with an image and text. 下面的代码将绘制带有图像和文本的PDF。 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. 下面的函数将使用UIWebView呈现PDF。

-(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. 如果您有兴趣渲染PDF文件,我在这里还有其他文章,这可能会很有用。

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

John 约翰

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM