直接从Ipad中的UiTextView创建多页PDF [英] Create Multipage PDF directly from UiTextView in Ipad

查看:95
本文介绍了直接从Ipad中的UiTextView创建多页PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了具有特定布局的pdf文件.问题是,当文本视图的内容很长时,Pdf将不会自动生成新页面,并且我无法预定义在运行时将创建多少个页面.谁能帮我吗?

I have created pdf with specific layout. The problem is that When the contents of text view is very long, Pdf will not automatically generate new page and I can not predefine how many pages will be created at run time. Can anyone please help me?

推荐答案

您可以尝试使用以下代码,这可能会对您有所帮助 在界面"构建器中保留一个textview和一个按钮,并使用按钮操作方法并生成pdf 导入这些框架

You can try by using this code this may help you Keep a textview and a button in Interface builder and use the button action method and generate pdf import these frameworks

#import <QuartzCore/QuartzCore.h>
#import <CoreText/CoreText.h>



    -(NSString*)getPDFFileName
    {
        NSString* fileName = @"sample.PDF";
        NSArray *arrayPaths =
        NSSearchPathForDirectoriesInDomains(
                                            NSDocumentDirectory,
                                            NSUserDomainMask,
                                            YES);
        NSString *path = [arrayPaths objectAtIndex:0];
        NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
        return pdfFileName;

    }
    -(IBAction)PdfGeneration:(id)sender{
        // Prepare the text using a Core Text Framesetter.
        CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, (__bridge CFStringRef)txtObj.text, NULL);
        if (currentText) {
                CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
                if (framesetter) {

                NSString *pdfFileName = [self getPDFFileName];

                // Create the PDF context using the default page size of 612 x 792.

                UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
                CFRange currentRange = CFRangeMake(0, 0);
                NSInteger currentPage = 0;
                BOOL done = NO;
                do {

                    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
                    currentPage++;
                    [self drawPageNumber:currentPage];
                    currentRange = [self renderPage:currentPage withTextRange:currentRange andFramesetter:framesetter];
                    if (currentRange.location == CFAttributedStringGetLength((CFAttributedStringRef)currentText))

                        done = YES;

                } while (!done);
                UIGraphicsEndPDFContext();
               CFRelease(framesetter);
            } else {

                NSLog(@"Could not create the framesetter needed to lay out the atrributed string.");

            }

            // Release the attributed string.

            CFRelease(currentText);

        } else {

            NSLog(@"Could not create the attributed string for the framesetter");

        }
    }
    // Use Core Text to draw the text in a frame on the page.

    - (CFRange)renderPage:(NSInteger)pageNum withTextRange:(CFRange)currentRange

           andFramesetter:(CTFramesetterRef)framesetter

    {

        // Get the graphics context.

        CGContextRef    currentContext = UIGraphicsGetCurrentContext();
        CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
        CGRect    frameRect = CGRectMake(72, 72, 468, 648);
        CGMutablePathRef framePath = CGPathCreateMutable();
        CGPathAddRect(framePath, NULL, frameRect);
        CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
        CGPathRelease(framePath);
        CGContextTranslateCTM(currentContext, 0, 792);
        CGContextScaleCTM(currentContext, 1.0, -1.0);
        CTFrameDraw(frameRef, currentContext);
        currentRange = CTFrameGetVisibleStringRange(frameRef);
        currentRange.location += currentRange.length;
        currentRange.length = 0;
        CFRelease(frameRef);
        return currentRange;

    }
    - (void)drawPageNumber:(NSInteger)pageNum

    {

        NSString *pageString = [NSString stringWithFormat:@"Page %d", pageNum];
        UIFont *theFont = [UIFont systemFontOfSize:12];
        CGSize maxSize = CGSizeMake(612, 72);
        CGSize pageStringSize = [pageString sizeWithFont:theFont constrainedToSize:maxSize lineBreakMode:UILineBreakModeClip];
        CGRect stringRect = CGRectMake(((612.0 - pageStringSize.width) / 2.0),720.0 + ((72.0 - pageStringSize.height) / 2.0),pageStringSize.width,pageStringSize.height);
           [pageString drawInRect:stringRect withFont:theFont];

    }

这篇关于直接从Ipad中的UiTextView创建多页PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆