Using PDF's in Apps

I have been searching for a while on how to best add PDF’s in apps. I know about the services that help you do this but I do not want to use those. PDFTron and the like.
I want to make an app that’s really simple, I am new to swift.
I would like to just have a list view on the first page with a title and image. the list view will have categories. When I tap on of the items it will go to a new page and give info and on that page I will have a button to view the corresponding PDF. I do not want to leave the app and view that PDF in the IOS PDF viewer or Adobe Acrobat. It will not be editable just printable.
I started this in SwiftUI and got the UI pretty much worked out except the PDF. I was looking into storing the info in an API in GFirebase and then using JSON to bring that into the app but need it to be PDF format.
Not looking for code just a direction. Everything I see has an Idea on how to but no one really gives it more than a cursory look or just says use JSON and doesn’t show the code of the helper file… Thanks for reading this post and look forward to everyones thoughts.

1 Like

Hi @jel111 welcome to the community!

I have an app that I present four different PDFs to the user depending on their selection.

Just add the pdfs to your bundle (drag them in) and here is the code to display them.

//name below is the file name of the pdf, I passed the string in with the method property. 

if let path = Bundle.main.path(forResource: name, ofType: "pdf") {
            let url = URL(fileURLWithPath: path)
            if let pdfDocument = PDFDocument(url: url) {
                pdfView.displayMode = .singlePageContinuous
                pdfView.autoScales = true
                pdfView.displayDirection = .vertical
                pdfView.document = pdfDocument
            }
        }

You will need to create a PDFView in the ViewController you want to display them.

Blessings,
—Mark

2 Likes

Hi Mark,
I am a newbie, and I am trying to accomplish the above. (push a button and get to view a pdf file in another screen), but the answer you are providing here does not have enough detail for my low level of expertise. Could you help?
Thank you in advance for your time and consideration!
Regards,
Reto

Hi Reto Welcome to the community!

On your storyboard create a view that you want to assign as a PDFView. See pict. Set the custom class to PDFView.

Then in the viewController, first add an outlet

IBOutlet weak var pdfView: PDFView!

Then add your IBAction. Use the code above for the IBAction.

In the first line of the code above that says name, type in quotes “yourfilename” te name of your file. Caps count. Drag that PDF to the bundle where all your files reside, like viewcontrollers, stroryboard, etc. Be sure to check “Copy if Needed”, and “add to target”

If you want horizontal presentation, set .vertical to .horizontal

Build and run and you should be good to go.

Hope that helps.
Blessings,
—Mark

Thank you Mark, I managed to get it to work with your further details!

Now, I am attempting to have a search capability of the PFD file that we managed to display, but have an issue related to unwrapping with the code below. It is important to mention the code builds successfully.
Yet, when I press the search button to perform a search (in the simulator) the App crashes and gives me an error in the code that says: “Tread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value”

// Search for given text

func searchHighlight() {
let searchItem = searchField.text
let selections = pdfView.document?.findString(searchItem!,
withOptions: .caseInsensitive)

// Remove highlighted text from previous search result, if any
// In the line below is where I get the error: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value ( I tried first to use a “?” and it failed to built and got an error (Value of optional type’Int?’ must be unwrapped to a value of type ‘Int’) Then, I replace the first two lines “?” with “!” and it built successfully, but crashed while in the simulator

    for index in 0..<pdfView.document!.pageCount {
         let page: PDFPage = document!.page(at: index)!
         let annotations = page.annotations
         for annotation in annotations {
              page.removeAnnotation(annotation)
         }
    }

// Highlight the text that matches the search criteria
selections?.forEach { selection in
selection.pages.forEach { page in
let highlight = PDFAnnotation(bounds:
selection.bounds(for: page),
forType: .highlight, withProperties: nil)
highlight.endLineStyle = .square
highlight.color = UIColor.yellow.withAlphaComponent(0.5)
page.addAnnotation(highlight)
currentAnnotation = highlight
pdfView.go(to: page)
}
}
}

Thank you in advance!

Hi,

You are getting a crash because unwrapping with a ! says to Xcode, I know for sure that there will be data there. And in your case it was nil.

So before you use a force unwrap you need to check for nil. If not nil, then force unwrap away.

Chris has a video demonstrating the several ways to check for Nil.

At this point, I would set a break point and step through so you know what line of your code is crashing.

If you are searching text inside a PDF, sorry I can’t help there. Way above my skill level. Sorry.

Blessings,
—Mark

Thank you again Mark I do appreciate very much your response!