gpt4 book ai didi

iphone - 如何使用 quartz 在pdf文档中搜索文本

转载 作者:搜寻专家 更新时间:2023-10-30 20:12:28 24 4
gpt4 key购买 nike

我正在使用 quartz 来显示 pdf。我需要获取我的搜索文本所在页面的索引。任何人都可以帮助我吗?谢谢。

解决方案:有一个代码示例可以从页面中提取文本并检查其序列。

#import <Foundation/Foundation.h>

@interface PDFSearcher : NSObject {
CGPDFOperatorTableRef table;
NSMutableString *currentData;
}

@property (nonatomic, retain) NSMutableString * currentData;
-(id)init;
-(BOOL)page:(CGPDFPageRef)inPage containsString:(NSString *)inSearchString;

@end

#import "PDFSearcher.h"

@implementation PDFSearcher
@synthesize currentData;
void arrayCallback(CGPDFScannerRef inScanner, void *userInfo)
{
PDFSearcher * searcher = (PDFSearcher *)userInfo;

CGPDFArrayRef array;

bool success = CGPDFScannerPopArray(inScanner, &array);

for(size_t n = 0; n < CGPDFArrayGetCount(array); n += 2)
{
if(n >= CGPDFArrayGetCount(array))
continue;

CGPDFStringRef string;
success = CGPDFArrayGetString(array, n, &string);
if(success)
{
NSString *data = (NSString *)CGPDFStringCopyTextString(string);
[searcher.currentData appendFormat:@"%@", data];
[data release];
}
}
}

void stringCallback(CGPDFScannerRef inScanner, void *userInfo)
{
PDFSearcher *searcher = (PDFSearcher *)userInfo;

CGPDFStringRef string;

bool success = CGPDFScannerPopString(inScanner, &string);

if(success)
{
NSString *data = (NSString *)CGPDFStringCopyTextString(string);
[searcher.currentData appendFormat:@"%@", data];
[data release];

}
}

-(id)init
{
if(self = [super init])
{
table = CGPDFOperatorTableCreate();
CGPDFOperatorTableSetCallback(table, "TJ", arrayCallback);
CGPDFOperatorTableSetCallback(table, "Tj", stringCallback);
}
return self;
}

-(BOOL)page:(CGPDFPageRef)inPage containsString:(NSString *)inSearchString
{
[self setCurrentData:[NSMutableString string]];
CGPDFContentStreamRef contentStream = CGPDFContentStreamCreateWithPage(inPage);
CGPDFScannerRef scanner = CGPDFScannerCreate(contentStream, table, self);
bool ret = CGPDFScannerScan(scanner);
CGPDFScannerRelease(scanner);
CGPDFContentStreamRelease(contentStream);
//NSLog(@"%u, %@", [self.currentData length], self.currentData);
return ([[self.currentData uppercaseString]
rangeOfString:[inSearchString uppercaseString]].location != NSNotFound);
}
@end

最佳答案

使用CGPDFDocument、CGPDFPage和CGPDFScanner扫描并解析页面内容为NSString。然后使用 NSString 函数查找该页面上的文本。如果存在,则将相应的页码存储在某个数组中。重复此扫描并在 for 循环中解析 pdf 中的页数

关于iphone - 如何使用 quartz 在pdf文档中搜索文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4737305/

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