将PDF文件转换为文本文件

5

大家好,我正在使用Objective-C编程。我的上一个问题是如何在iOS应用程序中编辑PDF文件? 经过大量的搜索,我发现以下方法:在UIWebView中显示PDF,使用C / javascript提取数据并进行编辑。但我仍然不确定这个过程是否可行。现在我计划:

1)显示PDF

2)当用户想要编辑PDF时,将PDF转换为文本并允许他进行编辑

3)尝试保存将把内容转换回PDF。

这种方法可行吗?我已经完成了第一步。现在如何将PDF转换为文本,以及将文本转换为PDF呢?

提前致谢

1个回答

2
当您将自定义文档类型(doc、ppt、pdf等)加载到UIWebView中时,即使通过JavaScript,webview也会返回一个空的HTML字符串。有一些建议可以在此处提取PDF文本。链接 但是将字符串转换回PDF是不同的。如果您想保留原始PDF的格式,则我非常确定这是不可能的,因为iOS上的NSAttributedString并没有做很多事情。但是,如果可能的话,这对于纯文本或NSAttributedString将起作用。
NSData *PDFDataFromString(NSString *str) {
    NSMutableData *data = [NSMutableData data];

    //Create an NSAttributedString for CoreText. If you find a way to translate
    //PDF into an NSAttributedString, you can skip this step and simply use an
    //NSAttributedString for this method's argument.

    NSAttributedString* string = [[[NSAttributedString alloc] initWithString:str] autorelease];

    //612 and 792 are the dimensions of the paper in pixels. (8.5" x 11")
    CGRect paperRect = CGRectMake(0.0, 0.0, 612, 792);

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef) string);
    CGSize requiredSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [string length]), NULL, CGSizeMake(paperRect.size.width - 144, 1e40), NULL);

    //Subtract the top and bottom margins (72 and 72), so they aren't factored in page count calculations.
    NSUInteger pageCount = ceill(requiredSize.height / (paperRect.size.height - 144));
    CFIndex resumePageIndex = 0;
    UIGraphicsBeginPDFContextToData(data, paperRect, nil);

    for(NSUInteger i = 0; i < pageCount; i++) 
    {

    //After calculating the required number of pages, break up the string and
    //draw them into sequential pages.

        UIGraphicsBeginPDFPage();
        CGContextRef currentContext = UIGraphicsGetCurrentContext();
        CGContextSaveGState (currentContext);
        CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
        CGMutablePathRef framePath = CGPathCreateMutable();

        //72 and 72 are the X and Y margins of the page in pixels.
        CGPathAddRect(framePath, NULL, CGRectInset(paperRect, 72.0, 72.0));

        CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, CFRangeMake(resumePageIndex, 0), framePath, NULL);
        resumePageIndex += CTFrameGetVisibleStringRange(frameRef).length;
        CGPathRelease(framePath);
        CGContextTranslateCTM(currentContext, 0, paperRect.size.height);
        CGContextScaleCTM(currentContext, 1.0, -1.0);
        CTFrameDraw(frameRef, currentContext);
        CFRelease(frameRef);    
        CGContextRestoreGState (currentContext);
    }
    CFRelease(framesetter);
    UIGraphicsEndPDFContext();
    return data;
}

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