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

发布于 2024-12-11 07:44:37 字数 72 浏览 0 评论 0原文

我想在iOS中创建一个PDF文件,PDF中应该有一张表格,它是由一个数组填充的。我已经在谷歌上搜索过,但没有成功。任何帮助表示赞赏

I want to create a PDF file in iOS ,there should be one table in the PDF, which is filled from one array. I already searched on Google, but didn't succeed. Any help is appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笑百媚生 2024-12-18 07:44:37

像这样的例程来渲染文本:

- (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 循环和输入之前的渲染稍加修改也可以让您在多列中渲染文本。

Something like this routine to render the text:

- (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;
}

And the following code snippet which calls it, assuming you have the context and any fonts etc created and in the appropriate variables. The following loop simply builds up the text line by line into an NSMutableAttributedString which you can then render:

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];
}

Now you have your array written out with newlines to one NSMutableAttributedString you can render it, depending on your text you might want to render it out in a loop until the rendered location matches your text length. Something like:

// 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;
    }
}

The above code will need quite a bit of adapting I'm sure as it's hacked out of a project of my own so some variables (like the frame setter) won't exist and you need to close off the PDF context and release variables etc. Note how mainString is used to determine when the text has been rendered out.

It should give a clear enough indication of how to loop around an array or any other group to render an arbitrary length of text into a document.

Slight modifications to the while loop and the one render before entering it will let you render text out in multiple columns too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文