如何在iOS中以编程方式创建PDF?

7

我希望在iOS中创建一个PDF文件,其中应该有一个表格,该表格从一个数组中填充。我已经在Google上搜索过,但没有成功。非常感谢您的帮助。


2
这是一个非常流行的问题,只需搜索:iOS SDK - 以编程方式生成PDF文件PDF文件文本读取和搜索 - beryllium
1
@ beryllium:我已经搜索并成功在视图中生成了PDF文件。但是我想在PDF文件中显示我们的数组数据。 - Vikas S Singh
1个回答

1

类似这样的例程来渲染文本:

- (CFRange)renderTextRange:(CFRange)currentRange andFrameSetter:(CTFramesetterRef)frameSetter intoRect:(CGRect)frameRect {
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
CTFrameRef frameRef = CTFramesetterCreateFrame(frameSetter, currentRange, framePath, NULL);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(currentContext);
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);  
CGContextTranslateCTM(currentContext, 0, 792); 
CGContextScaleCTM(currentContext, 1.0, -1.0);
CTFrameDraw(frameRef, currentContext);
CGContextRestoreGState(currentContext);
CGPathRelease(framePath);
currentRange = CTFrameGetVisibleStringRange(frameRef);
currentRange.location += currentRange.length;
currentRange.length = 0;
CFRelease(frameRef);
return currentRange;
}

以下是调用它的代码片段,假设您已经创建了上下文和任何字体等,并将其放在适当的变量中。 以下循环逐行构建文本到NSMutableAttributedString中,然后您可以进行渲染:

CTFontRef splainFont = CTFontCreateWithName(CFSTR("Helvetica"), 10.0f, NULL);
CGFloat margin = 32.0f;
CGFloat sink = 8.0f;

NSMutableAttributedString *mainAttributedString = [[NSMutableAttributedString alloc] init];
NSMutableString *mainString = [[NSMutableString alloc] init];

// Ingredients is an NSArray of NSDictionaries
// But yours could be anything, or just an array of text.
for (Ingredient *ingredient in ingredients) {
    NSString *ingredientText = [NSString stringWithFormat:@"%@\t%@
        \n",ingredient.amount,ingredient.name];
    [mainString appendString:ingredientText];
    NSMutableAttributedString *ingredientAttributedText =
        [[NSMutableAttributedString alloc] initWithString:ingredientText];
    [ingredientAttributedText addAttribute:(NSString *)(kCTFontAttributeName)
        value:(id)splainFont
        range:NSMakeRange(0, [ingredientText length])];
    [mainAttributedString appendAttributedString:ingredientAttributedText];
    [ingredientAttributedText release];
}

现在你已经将数组写成带有换行符的NSMutableAttributedString,你可以根据需要呈现它。根据你的文本内容,你可能需要在循环中呈现它,直到呈现位置与文本长度相匹配。类似这样:
// Render Main text.
CTFramesetterRef mainSetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)mainAttributedString);
currentRange = [KookaDIS renderTextRange:currentRange 
    andFrameSetter:mainSetter 
    intoRect:pageRect];

// If not finished create new page and loop until we are.
while (!done) {
    UIGraphicsBeginPDFPageWithInfo(pageRect, nil);

    currentRange = [self renderTextRange:currentRange 
        andFrameSetter:mainSetter 
        intoRect:pageRect];

    if (currentRange.location >= [mainString length]) {
        done = TRUE;
    }
}

以上代码需要进行相当多的调整,因为它是从我的一个项目中剪切出来的,所以一些变量(如框架设置器)不存在,您需要关闭PDF上下文并释放变量等。请注意,mainString用于确定何时将文本呈现出来。

它应该清楚地表明如何循环遍历数组或任何其他组,以将任意长度的文本呈现到文档中。

对while循环和进入之前的一个渲染进行轻微修改,也可以让您在多列中呈现文本。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接