How to detect change in pdf loaded using PDF KIT iOS - ios

I have loaded an editable pdf in iOS. But not able to figure out if user have done any changes to that pdf.
NSData *fileData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"PdfFormExample" ofType:#"pdf"]];
PDFDocument *pdfDocument = [[PDFDocument alloc] initWithData:fileData];
PDFView *pdfView = [[PDFView alloc] initWithFrame:self.view.frame];
pdfView.document = pdfDocument
[self.view addSubView:pdfView];
Thanks in Advance.

You can use PDFKit for controlling and loading the pdf into iOS app. Its provided by the apple. In that you have to first setup delegate for the PDFs
PDFDocumentDelegate
PDFKit can send the notifications for different operations such as begin writing, end writing, etc. So you can use these methods to get local notification get determine that whether the user started writing or modifying the PDF. For that you can use,
PDFDocumentDidBeginPageWriteNotification
PDFDocumentDidEndPageWriteNotification
any of the above according to when you wanna make changes.
If you want more reference to this here is the link:
PDFKit / PDF Document

Related

UIActivityViewController not showing PDF option

I know there are many posts on this type of thing. And I believe I have tried all of the suggestions. I have a very simple problem. I have a url to a PDF and I want to be able to "share" this to the adobe pdf reader app. To remove one of the possible issues I took the PDF I was downloading and put it into my bundle so it was local. I am on iOS 8.4. Here is the code I tried:
NSURL * url2 = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource: # "data" ofType: # "pdf"]];
//Tried with the data in the file
NSData *data2 = [NSData dataWithContentsOfURL:url2];
UIActivityViewController *act=[[UIActivityViewController alloc]initWithActivityItems:#[data2] applicationActivities:nil];
//Tried with just the URL
UIActivityViewController *act=[[UIActivityViewController alloc]initWithActivityItems:#[url2] applicationActivities:nil];
act.popoverPresentationController.barButtonItem = button;
act.popoverPresentationController.sourceView = self.view;
[self presentViewController:act animated:YES completion:nil];
return;
Neither of these show the PDF option. I of course have it installed and have run it once to make sure its registered properly. Both of these I can share to Mail and the actual file is sent fine but the only share options I get are Mail, Copy and Print. I would like to see the other options like Open in iBooks and Open in Adobe Acrobat like I get from safari.
Ideas?

iOS create pdf from UIWebview content

In my app for iOS i need to create a pdf document from my webview content. I watched these posts: Creating PDF file from UIWebView and https://coderchrismills.wordpress.com/2011/06/25/making-a-pdf-from-a-uiwebview/
I wonder if there is a simpler way to do it. For example for my project for Mac i use this:
NSData *pdf = [[[[webView mainFrame] frameView] documentView] dataWithPDFInsideRect:[[[webView mainFrame] frameView] documentView].frame];
PDFDocument *doc = [[PDFDocument alloc] initWithData:pdf];
Is there any simple way to do this in iOS?
Which is the best option to obtain best quality pdf document from a webview content?
There isn't a method that allows this directly via the SDK like there is on Mac however you may wish to take a look at BNHtmlPdfKit which allows you to save the contents of URLs, web views and also html strings as PDFs.
For example, as follows:
self.htmlPdfKit = [BNHtmlPdfKit saveUrlAsPdf:[NSURL URLWithString:#"http://itsbrent.net"] toFile:#"...itsbrent.pdf" pageSize:BNPageSizeA6 success:^(NSString *pdfFileName) {
NSLog(#"Done");
} failure:^(NSError *err) {
NSLog(#"Failure");
}];
It makes use of a custom UIPrintPageRenderer which overrides paperRect and printableRect thus causing the UIPrintFormatter to return a pageCount as well as render the document.
I found good answer by AnderCover at
"Creating PDF file from UIWebView"
also it's not using any third party api. To create pdf from webview.
Hope it help's you.

how to save existing pdf file in NSDocumentDirectory in ios

I have one pdf file and I want to save that file using NSDocumentDirectory and retrieve it.how do i convert pdf file into NSData so that I can save. Please help me. Thanks in advance.
Brad,
Welcome to Stack Overflow.
This is not a site where people give you solutions to your problems "out of whole cloth". You need to show what you have attempted, and the specific places where you are stuck.
You say you "have one pdf file". Is it a file on disk somewhere, in memory, or what? If it's already on disk then you can use the file manager to copy it to the documents directory. Take a look in the Xcode docs under NSFileManager and read the class reference. There are tons of useful methods for creating and copying files.
You say "..how do i convert pdf file into NSData so that I can save." Erm, if it's a file, why do you need to convert it to NSData in order to save it? It's already a file.
NSData has methods for creating a data object with the contents of a file (dataWithContentsOfFile and dataWithContentsOfURL) as well as methods for writing an NSData object to a file (again using a path or an NSURL, like the dataWith... methods)
As you said you have PDF file so i guess it will be a ready file.
Now in that case you don't need to put it in document directory. Just put it into your resources folder where you placed images & other files.
After that to read that pdf file Simply do this:
UIWebView *myweb = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
myweb.scalesPageToFit = YES;
NSString *path = [[NSBundle mainBundle] pathForResource:#"myPDF" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[myweb loadRequest:request];
[self.view addSubview:myweb];
Hope it will work for you.

How to open a PDF file in my iPad/iPhone using my iOS application?

How can I open a PDF file that was stored in my iPad/iPhone, using my own application?
You can use UIwebview to load it. It is very simple. If you want more flexibility you should use Quartz framework classes.
EDIT:
To view downloaded PDF, you can provide open-in functionality in your app. This is how you add "open-in" to your app.
Look here for complete tutorial.
There is a good tutorial available here which demonstrates how to open .pdf, .xls files in your application.
The main class you have to refer for this is QLPreviewController. here
This is the Datasource Method you would have to call for that
- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
// Break the path into it's components (filename and extension)
NSArray *fileComponents = [[arrayOfDocuments objectAtIndex: index] componentsSeparatedByString:#"."];
// Use the filename (index 0) and the extension (index 1) to get path
NSString *path = [[NSBundle mainBundle] pathForResource:[fileComponents objectAtIndex:0] ofType:[fileComponents objectAtIndex:1]];
return [NSURL fileURLWithPath:path];
}
Also someone would like to refer this SO question :iPhone - Opening word and excel file without using UIWebview.
You can use Core Graphics for this task.
Look at PDFView sample from apple resources

Reading text and images from a pdf document in iOS

I'm displaying locally stored pdf in my iPad application. Here's the code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"About Downloads" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[pdfWebView loadRequest:request];
Now, by default, you can't copy text or images from the PDF which is rendered by the UIWebView. Is there a way to let users copy text and/or images out of pdf?
I'm not familiar with CATitledLayer, so i'm just wondering if it can help in this case?
There's no simple answer to this. PDF's are nested dictionaries composed of more dictionaries & arrays. You'll have to dig into CGPDFDocument. Voyeur is an excellent tool to use while digging around in PDF's. Reader is a good suggested starting point for rendering PDF's.
To get at the text in a PDF Document, I use PDF Kitten (https://github.com/KurtCode/PDFKitten). It works quite well, but as the author notes, is incomplete and does not support all font types.

Resources