gpt4 book ai didi

iphone - 将pdf文件转换为文本文件

转载 作者:太空狗 更新时间:2023-10-30 03:54:20 28 4
gpt4 key购买 nike

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

1) 显示pdf

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

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

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

提前致谢

最佳答案

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

但是将字符串转回 PDF 是不同的。如果您想保留原始 PDF 的格式,我敢肯定这是不可能的,因为 NSAttributedString 在 iOS 上的作用不大。但这将适用于纯文本或 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;
}

关于iphone - 将pdf文件转换为文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6869516/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com