首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将pdf文件转换为文本文件

将pdf文件转换为文本文件
EN

Stack Overflow用户
提问于 2011-07-29 14:39:11
回答 1查看 3.1K关注 0票数 5

大家好,我正在开发Objective-C。我之前的问题是How can I edit PDF files in an iOS application?,经过大量的谷歌搜索,我发现了以下内容。在UIWebView中显示pdf,使用C/javascript提取数据并编辑它。我对这个过程还是不太确定。现在我的计划是

1)显示pdf

2)当用户想要编辑pdf时,我将pdf转换为文本并允许他编辑它。

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

这是一个很好的方法吗??我是k与步骤1。现在我如何转换pdf-->文本和文本-->pdf。

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2011-07-30 15:34:19

当您将自定义文档类型(doc、ppt、pdf等)加载到UIWebView中时,webview会返回一个nil HTML字符串,即使是通过javascript。这里有一些关于提取PDF text here的建议。

但是把字符串转换成PDF格式就不一样了。如果你想保留原始PDF的格式,我敢肯定这是不可能的,因为iOS上的NSAttributedString不会做太多事情。但这将适用于纯文本或NSAttributedString,如果可能的话:

代码语言:javascript
复制
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;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6869516

复制
相关文章

相似问题

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