5

We can get selected text from a web view by using

- (NSString *)selectedText {
    return [self stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];
}

But this won't work if I'm loading a PDF file. So what I thought is that to copy the selected text and then pasting it from the UIPasteboard programmatically. It works fine when I click the copy button manually, but I don't want it to be like that. I want the copy cade to execute programatically. How can I make a call to it? Or simply, How can I get the reference of the selected text from a PDF file loaded in a web view.

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
bijesh
  • 55
  • 8

1 Answers1

0

There is no simple answer to this, correct me if I am wrong but I think to optimise memory footprint UIWebView actually draws the PDF pages as images so you don't have access to individual items (textfields, images etc). You could do this -

PDF's are nested dictionaries composed of more dictionaries & arrays. You'll have to dig into CGPDFDocument

Since UIWebView does not expose much in terms of customisation, Reader is a good suggested alternative & starting point for rendering PDF's. Since its open-source you can change the rendering logic to select text.

Another alternative is PSPDFKit. I have used it in one of my projects & its pretty awesome. It includes full-text search, automatic thumbnail creation, single or dual page mode, view outline, page links and more

I am thinking one or a mix of the above approaches should help you get what you want & then release it as open-source for others to benefit ;)

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • But then how i'm getting the text when I copy the selected one manually and use the UIPasteboard to display it? – bijesh Sep 10 '12 at 07:17
  • 1
    NSString *myString = [UIPasteboard generalPasteboard].string; NSLog(@"Copied String: %@",myString); Using this i get the copied string from PDF file – bijesh Sep 10 '12 at 07:24
  • If some how I call that copy code programmatically the I think the problem can be solved – bijesh Sep 10 '12 at 07:27
  • It can be done using UIPasteBoard, but the trick is to send the copy selector to the UIApplication not the web view. See http://stackoverflow.com/questions/12623334/how-to-copy-selected-data-from-a-pdf-displayed-in-a-uiwebview – Dale Feb 02 '17 at 06:53