Convert or Print CGPDFStringRef string

2.3k views Asked by At

How to convert a CGPDFStringRef to unicode char? I have used CGPDFStringCopyTextString to get the string and then [string characterAtIndex:i] to cast to unichar, is this the right way? or is there any way to get the bytes of the string and convert to unicode directly? Need some guidance here.

3

There are 3 answers

4
UPT On BEST ANSWER

NSString is capable of handling of unicode characters itself, you just need to convert the CGPDFString to NSString and further you can use it as follows:

NSString *tempStr = (NSString *)CGPDFStringCopyTextString(objectString);
2
Jaffa On

It's not a bad idea, even if you can access the CGPDFString directly using CGPDFStringGetBytePtr. You will also need CGPDFStringGetLength to get the string length, as it may not be null-terminated.

See the documentation for more info

0
Ismael On

although UPT's answer is correct, it will produce a memory leak

from the documentation: CGPDFStringCopyTextString "...You are responsible for releasing this object."

the correct way to do this would be:

CFStringRef _res = CGPDFStringCopyTextString(pdfString);
NSString *result = [NSString stringWithString:(__bridge NSString *)_res];
CFRelease(_res);