首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从UITableView生成PDF,ios中的Plist

从UITableView生成PDF,ios中的Plist
EN

Stack Overflow用户
提问于 2013-12-12 10:49:46
回答 2查看 878关注 0票数 2

当数据存储在PList,UITableView中时,如何生成PDF?

我对ios非常陌生,这也会生成pdf。

提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-12 11:28:40

编辑:

假设您的plist结构为:

首先,================应该添加框架、coreText和石英核心。它们导入并定义如下宏:

代码语言:javascript
运行
复制
#import <QuartzCore/QuartzCore.h>
#import <CoreText/CoreText.h>

#define LEFT_MARGIN 25
#define RIGHT_MARGIN 25
#define TOP_MARGIN 35
#define BOTTOM_MARGIN 50
#define BOTTOM_FOOTER_MARGIN 32
#define DOC_WIDTH 1004
#define DOC_HEIGHT 768

您可以以这种方式获取plist数据:

代码语言:javascript
运行
复制
NSString *path = [[NSBundle mainBundle] pathForResource:@"recipes" ofType:@"plist"];    

// Load the file content and read the data into arrays
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];

//编辑

代码语言:javascript
运行
复制
tableData = [dict objectForKey:@"RecipeName"];//tabledata is global array
self.textToDraw = [tableData objectAtIndex:0];

//编辑

但是,下面的代码可以创建动态pdf:

代码语言:javascript
运行
复制
 -(void) btnDrawPdf
{ 

 //    pageSize = CGSizeMake(612, 792); 

 NSString *fileName = @"DemoNetwrk.pdf"; 
 NSString *fontName = @"Helvetica"; 

// NSString *textToDraw =@“您的数据来自plist ";现在注释一下

代码语言:javascript
运行
复制
//[self generatePdfWithFilePath:pdfFileName];
 [self createPDF:fileName **withContent:self.textToDraw**  forSize:14 forFontName:fontName andColor:[UIColor blackColor]];
}

//多页pdf和pdf将保存在doc目录library>Application Support>iPhoneSimulator>library>Documents (完美在IOS4.3中)

代码语言:javascript
运行
复制
- (void) createPDF:(NSString *)fileName withContent:(NSString *)content forSize:(int)fontSize forFontName:(NSString *)fontName andColor:(UIColor *)color
{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *newFilePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileName];

CGRect a4Page = CGRectMake(0, 0, DOC_WIDTH, DOC_HEIGHT);

NSDictionary *fileMetaData = [[NSDictionary alloc] init];

if (!UIGraphicsBeginPDFContextToFile(newFilePath, a4Page, fileMetaData )) {
    NSLog(@"error creating PDF context");
    return;
}

BOOL done = NO;
CGContextRef context = UIGraphicsGetCurrentContext();

CFRange currentRange = CFRangeMake(0, 0);

CGContextSetTextDrawingMode (context, kCGTextFill);
CGContextSelectFont (context, [fontName cStringUsingEncoding:NSUTF8StringEncoding], fontSize, kCGEncodingMacRoman);
CGContextSetFillColorWithColor(context, [color CGColor]);
// Initialize an attributed string.
CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CFAttributedStringReplaceString (attrString, currentRange, (CFStringRef)content);

// Create the framesetter with the attributed string.
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);

do {
    UIGraphicsBeginPDFPage();



    CGMutablePathRef path = CGPathCreateMutable();

    CGRect bounds = CGRectMake(LEFT_MARGIN,
                               TOP_MARGIN,
                               DOC_WIDTH - RIGHT_MARGIN - LEFT_MARGIN,
                               DOC_HEIGHT - TOP_MARGIN - BOTTOM_MARGIN);

    CGPathAddRect(path, NULL, bounds);



    // Create the frame and draw it into the graphics context
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, currentRange, path, NULL);

    if(frame) {
        CGContextSaveGState(context);
        CGContextTranslateCTM(context, 0, bounds.origin.y);
        CGContextScaleCTM(context, 1, -1);
        CGContextTranslateCTM(context, 0, -(bounds.origin.y + bounds.size.height));
        CTFrameDraw(frame, context);
        CGContextRestoreGState(context);

        // Update the current range based on what was drawn.
        currentRange = CTFrameGetVisibleStringRange(frame);
        currentRange.location += currentRange.length;
        currentRange.length = 0;

        CFRelease(frame);
    }

     // If we're at the end of the text, exit the loop.
    if (currentRange.location == CFAttributedStringGetLength((CFAttributedStringRef)attrString))
        done = YES;
}
while(!done);

UIGraphicsEndPDFContext();

//    [fileMetaData release];  
 CFRelease(attrString);  
 CFRelease(framesetter);

}

我终于明白了这一点:

它会帮助你..。

票数 2
EN

Stack Overflow用户

发布于 2013-12-12 11:00:04

This可能就是你要找的东西。

在将其转换为PDF之前,您应该将Plist呈现为屏幕(加载数据并将其显示在UIView或从UIView继承的类中)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20541412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档