Possible to show PDF over a PageViewController in an iOS app? - ios

I am developing a mobile app in which I need to highlight PDF text after searching and I am using PDF kitten library for this.
Library link: https://github.com/KurtCode/PDFKitten
From this I got number of selections on the PDF as well as the content of the PDF but I am unable to highlight text as the PDF is loaded on the webview. Is this possible, to add my PDF document over pageview controller and highlight text over it.

First, create a new instance of the scanner.
CGPDFPageRef page = CGPDFDocumentGetPage(document, 1);
Scanner *scanner = [Scanner scannerWithPage:page];
Set a keyword (case-insensitive) and scan a page.
NSArray *selections = [scanner select:#"happiness"];
Finally, scan the page and draw the selections.
for (Selection *selection in selections)
{
// draw selection
}
and then highlight the selections using core graphics framework.

Related

How to change default hyper link colours in UIWebview

I am generating pdf from word document using UIPrintPageRenderer. for this, I am loading doc file in UIwebview and on WebviewdidFinishLoad method, I am generating pdf usingUIPrintPageRenderer. this part is working fine.
Next, I need to detect if generated pdf file is Monochrome or coloured, to decide print price within app.
Problem is,UIWebview is displaying all Hyperlinks in words docs in blue fonts by default and hence generated pdf become coloured. I want to render hyperlinks with Black color as default.
I have tried following but not working:
1) set tint color of UIWebview.
webview.tintColor = [UIColor blackColor];
2) Styling hyper link in uiweview with javascript
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *javaScriptCodeToExecute = #"var link = window.document.getElementById('urlId'); link.style['text-decoration'] = 'none'; link.style.color = 'black';";
[_webView stringByEvaluatingJavaScriptFromString:javaScriptCodeToExecute];
}

Scroll To Specific Page using CGPDF

I am struck with this past two days i don't find any proper documents relating to CGPDF. I tried every possible way but i failed. Here is what i am trying to do. I have a PDF to display and i am displaying it in a UIWebView. I have created a CORE GRAPHICS PDF document reference using the path where the pdf is located (NSURL). I created a UIView over it and handled a single touch event. When the PDF loads and user clicks the view i want the page to scroll to a specific page. I know this can be done via calculating Page height and width. I wanted to know if there is a Way in CGPDF to pass a page number and it scroll to the relevant page. Below is my Code
- (void)viewDidLoad
{
[super viewDidLoad];
// set the pdfPafeHeight to -1 so it gets calculated.
self.pdfPageHeight = -1;
// set the delegate of the UIWebView's underlying UIScrollView to self.
self.webView.scrollView.delegate = self;
// create an NSURLRequest to load the PDF file included with the project
_filePath = [[NSBundle mainBundle] pathForResource:#"Sample" ofType:#"pdf"];
_url = [NSURL fileURLWithPath:_filePath isDirectory:NO];
_urlRequest = [NSURLRequest requestWithURL:_url];
// create a Core Graphics PDF Document ref using the same NSURL
_pdf = CGPDFDocumentCreateWithURL((CFURLRef) _url);
// use CGPDFDocumentGetNumberOfPages to get the number of pages in the document
self.pdfPageCount = (int)CGPDFDocumentGetNumberOfPages(_pdf);
// load the PDF file into the UIWebVie
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];
[self.webView loadRequest:_urlRequest];
}
HERE IS HOW I WANTED HANDLE SINGLE TAP ON UIVIEW. I just wanted to know if there is any method in CGPDF to scroll to specific page when i pass a page number to it
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
}
There's no API for what you're trying to achieve, and even if you're able to use private API if this app is just for Enterprise install and doesn't need to go to the App Store - this will be tricky and any workaround will likely break with a major iOS version update. If you want to go this route, you can introspect the view controller hierarchy and will find that Apple internally has a framework called CorePDF which has an internal private API that you might be able to access - but be careful and know that this is not a solution for App Store apps.
The alternative is to start from scratch and use CGContextDrawPDFPage to draw PDF pages, building your own cache, scroll views and view controllers+gesture handling. We did that with the commercial PSPDFKit SDK which I work on since 2010 - it's more work than you'd think. We also eventually moved on and use a custom renderer to improve compatibility with the wide range of PDF files available and to offer a better performance than Apple's renderer, however not every use case might require this.

Formatting strings in UIWebView

I'm building an iOS app which supports iOS 9 and above.
I'm using a UIWebView to display text stored in RTF documents which I am including in my app's bundle. I use the following code to insert the contents of the RTF files into the web view:
NSURL *filePath = [[NSBundle mainBundle] URLForResource:self.detailItem withExtension:#"rtf"];
NSURLRequest *request = [NSURLRequest requestWithURL:filePath];
[self.detailWebView loadRequest:request];
I am also using this to format the text to the correct size:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *fontSize = #"80";
NSString *jsString = [[NSString alloc] initWithFormat:#"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", [fontSize intValue]];
[self.detailWebView stringByEvaluatingJavaScriptFromString:jsString];
}
I never usually use JavaScript, so I'm not claiming to know how that works, but it enables me to adjust the fontSize string and achieve exactly the size that I want. The text displays in the same font as the rest of the stings in my app and everything looks fine.
The problem is that I want to be able to display some other text in the web view but I want to load it from an NSString in code, so I can append other strings and manipulate what the user sees. When I do this instead of loading from the RTF file, I get very different formatting:
[self.detailWebView loadHTMLString:#"This is a string" baseURL:nil];
This comes out in some nasty Times New Roman style font, and the text way smaller than the text that is loaded from the RTF files. I realise I can build some html tags into my string to add formatting, but I want to understand why the js formatting in webViewDidFinishLoad is not being applied, and what I can do to achieve universal formatting across all the strings I use in my web view.
If you need some UIWebView text formatting on iOS, you can look at my approach with SWIFT. https://github.com/Vanyaslav/Swiftyyjsformatter
Hope, it helps.

Update webview page number when updating font

I'm working on a book reader iOS application and using webview for displaying epub books. The 3rd party library I'm using for parsing epub book is "KFEpubKit".
I've to give option to user to adjust the font size (as in iBooks), and I've achieved that feature by updating the font of webview as:
let textSizeRule = "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '\(fontPercentage)%'"
self.webView.stringByEvaluatingJavaScriptFromString(textSizeRule)
The problem I'm facing is that by increasing/decreasing font size, page number of book must also change. But I'm unable to do so.
How can I achieve this functionality in the way other book reading applications are doing it (i.e. kindle, iBook etc)?
You can create a second WebView object which is hidden. You do the same thing ("For time being, i'm reloading the book and computing page number.") in the background webview.
In your webViewDidFinishLoad method, check which webview is loaded
func webViewDidFinishLoad(webView: UIWebView) {
if webView == backgroundWebview {
//Compute number of pages
} else {
//Foreground webview
}
}
So the user is not disturbed by a reloading webview.
Hope this helps!

Adding custom text to UITextField buffer

Background: I have got a custom alert part of the HomeKit API. I'd like to allow the user to paste the code requested rather than inputting it manually.
Whenever there is text in the buffer it is possible to paste it by tapping on the textfield. How can I add custom text (from my App) to "this" buffer so the "paste" option appears?
You can populate the iPhone clipboard using below code. Put this code to a suitable place in your code.
UIPasteboard *clipboard = [UIPasteboard generalPasteboard];
clipboard.string = #"<Assign Some value>";

Resources