【问题标题】:iOS: Drawing text on multiple pages in a PDF documentiOS:在 PDF 文档的多个页面上绘制文本
【发布时间】:2012-08-22 16:05:11
【问题描述】:

在 pdf 文档的两页或多页上绘制一长串文本的推荐方法是什么?

当您绘制的文本字符串对于单个页面来说太长时,文本会在文档末尾被剪裁。我最初的想法是计算可以在当前页面上绘制多少字符串,然后将字符串拆分为两个子字符串:(1)可以在当前页面上绘制的部分和(2)被剪裁的部分。下一步将重复此过程,直到绘制整个字符串。

问题仍然是如何计算可以在当前页面上绘制多少字符串。我是否忽略了某些东西,或者这是一个相当乏味且容易出错的过程?

【问题讨论】:

    标签: ios pdf drawing


    【解决方案1】:

    很久以前我做了这个函数,它可能会帮助你,传递间距和字体,它会返回有限宽度和所需大小的分割字符串。

    - (NSMutableArray *)getSplittedString:(NSString *)largeStr:(UIFont *)font:(NSInteger)horizontalSpace:(float)width {
        NSArray *strWords = [largeStr componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        //finding space width
    
        NSInteger currIndex = 0;
        NSString *tmpStr = @"";
        NSString *compareStr = @"";
        NSInteger currWidth;
    
        NSMutableArray *strArray = [[NSMutableArray alloc] init];
        NSMutableArray *strFrames = [[NSMutableArray alloc] init];
    
        while(TRUE) {
            while(TRUE) {
                compareStr = tmpStr;
                compareStr = [compareStr stringByAppendingString:[strWords objectAtIndex:currIndex]];
                currWidth = [compareStr sizeWithFont:font].width;
                if(currWidth < width) {
                    currIndex = currIndex + 1;
                    if(currIndex == [strWords count]) {
                        [strArray addObject:compareStr];
                        [strFrames addObject:[NSValue valueWithCGRect:[self findCenterAdjustedLocation:compareStr :width :font :horizontalSpace]]];
                        break;
                    }
                    tmpStr = [NSString stringWithFormat:@"%@ ",compareStr];
    
                } else if (currWidth == width) {
                    [strArray addObject:compareStr];
                    [strFrames addObject:[NSValue valueWithCGRect:[self findCenterAdjustedLocation:compareStr :width :font :horizontalSpace]]];
                    tmpStr = @"";
                    if(currIndex == [strWords count]) {
                        break;
                    }
                } else {
                    if([compareStr rangeOfString:@" "].location == NSNotFound) {
                        while(TRUE) {
                            NSInteger loc = [self findAdjustedLocation:compareStr :width :font];
                            if(loc == 0) {
                                tmpStr = [compareStr substringFromIndex:loc];
                                if(currIndex == [strWords count]) {
                                    [strArray addObject:tmpStr];
                                    [strFrames addObject:[NSValue valueWithCGRect:[self findCenterAdjustedLocation:tmpStr :width :font :horizontalSpace]]];
                                    break;
                                }
                                tmpStr = [NSString stringWithFormat:@"%@ ",tmpStr];
                                break;
                            } else {
                                tmpStr = [compareStr substringWithRange:NSMakeRange(0, loc)];
                                [strArray addObject:tmpStr];
                                [strFrames addObject:[NSValue valueWithCGRect:[self findCenterAdjustedLocation:tmpStr :width :font :horizontalSpace]]];
                                tmpStr = [NSString stringWithFormat:@"%@ ",[compareStr substringFromIndex:loc]];
                                compareStr = tmpStr;
                                if ([tmpStr sizeWithFont:font].width < width) {
                                    [strArray addObject:tmpStr];
                                    [strFrames addObject:[NSValue valueWithCGRect:[self findCenterAdjustedLocation:tmpStr :width :font :horizontalSpace]]];
                                    currIndex = currIndex + 1;
                                    break;
                                }
                            }
                        }
                    } else {
                        [strArray addObject:tmpStr];
                        [strFrames addObject:[NSValue valueWithCGRect:[self findCenterAdjustedLocation:tmpStr :width :font :horizontalSpace]]];
                        tmpStr = @"";
                    }
                    break;
                }
            }
            if(currIndex == [strWords count]) {
                return [[NSMutableArray alloc] initWithObjects:strArray, strFrames, nil];
                break;
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以尝试通过 UIMarkupTextPrintFormatter 使用 iOS 打印子系统生成 PDF。我在这里解释该技术:

      Is there any way to generate PDF file from a XML/HTML template in iOs

      我还没有尝试过看看会发生什么。分页,但理论上它会做正确的事情。

      【讨论】:

      • 我不知道普通文本,它可能工作。对于我的一个应用程序,我使用带有行和列的 HTML 表格使用 iOS 打印子系统打印到 PDF。结果是在某些页面上,表的其中一行会随机偏移。但是,是的,它会为您将文本移动到单独的页面。
      【解决方案3】:

      一旦绘制到 PDF 上,我尝试计算文本的实际高度,但它非常烦人,因为您无法真正获得“真实”行高。我最终放弃了 CoreGraphics 并使用@TomSwift 技术绘制了所有内容。甚至比 CoreGraphics 更简单,您可以使用 CSS 样式设置字体、文本颜色……

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-30
        • 2022-08-23
        • 2017-12-26
        • 1970-01-01
        • 1970-01-01
        • 2018-06-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多