CGPDFScannerScan doesn't call callback function

554 views Asked by At

I am trying to get strings from a pdfFile sent to my app via open in... menu in from another app. I wrote a pdfscanner to which I pass a pdfPage and the scanner should fire a callBack "getString" i am using SWIFT mostly but the scanner code is in objective-C the problem is that when i send a pdfDoc to my app from another app, the "getString" callBack doesn't fire. and when i send the same document to my app using a URL from a directory in my macbook pro, then it works and the "getString" callBack is called. when i check the debugger, for both i always pass in a valid CGPDFPageRef, to the "extractStringsFromPDFPage:" method. i also in both cases have a valid CGPDFContentStream and CGPDFOperatorTable

here's the code for the scanner:

@implementation FFStringExtractor

- (instancetype)init
{
self = [super init];
if (self) {
    self.pdfStrings = [[NSMutableArray alloc] init];
}

return self;
}

- (void) extractStringsFromPDFPage: (CGPDFPageRef) page {

CGPDFContentStreamRef contentStream = CGPDFContentStreamCreateWithPage(page);

// get the strings
CGPDFOperatorTableRef operatorTable = CGPDFOperatorTableCreate();
CGPDFOperatorTableSetCallback(operatorTable, "Tj", getString);

FFStringExtractor *currentExtractor = self;

CGPDFScannerRef scanner = CGPDFScannerCreate(contentStream, operatorTable, (__bridge void*)currentExtractor);
CGPDFScannerScan(scanner);

//releases
CGPDFOperatorTableRelease(operatorTable);
CGPDFScannerRelease(scanner);
CGPDFContentStreamRelease(contentStream);

}

@end


void getString (CGPDFScannerRef pageScanner, void *info) {

CGPDFStringRef pdfString;
CGPDFScannerPopString(pageScanner, &pdfString);

const unsigned char *byteString = CGPDFStringGetBytePtr(pdfString);

NSString *macRomanDecode = [NSString stringWithCString:byteString encoding:NSMacOSRomanStringEncoding];

FFStringExtractor *currentObject = (__bridge FFStringExtractor*) info;
[currentObject.pdfStrings addObject:macRomanDecode];

}

here's how i send the documents to my app from the appDelegate

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {

    //Get the Data from the preview open in... menu

    //let macBookUrl = NSURL(fileURLWithPath: "/Users/vmalterre/Documents/Xcodes/TESTDOC.pdf")

    let doc = CGPDFDocumentCreateWithURL(url)
    //let doc = = CGPDFDocumentCreateWithURL(macBookUrl)

    let sharedStore = FFDataStore.sharedStore
    sharedStore.addFileWithDoc(doc)

    return true
}

i've been on this all day, any help would be greatly appreciated.

1

There are 1 answers

0
Vincent On

probleme solved, the text operator was not "TJ" but simply "'". This can happen in older PDF Versions