How to Enable Scanning to PDF in your iPhone App

It’s the mobile age. Users tend to do everything via mobile devices for its convenience.

So how do we make document scanning mobile? How to turn your iPhone camera into a scanner? In this post, we will talk about how to create a new iOS app for scanning documents to PDF using iPhone with Dynamsoft Camera SDK iOS Edition, a document scanning library for iOS.

Key Features of Dynamsoft Camera SDK for iOS

  • Automatic edge detection and perspective correction
  • Image enhancement which includes image cleanup, removal of noise, shadows, distortions etc.
  • Export images as JPEG, PNG or PDF to local or web server
  • Customizable UI

Prerequisites

  • Dynamsoft Camera SDK for iOS
  • Xcode 6.0 or above

Embed Document Scan in your iPhone APP

  1. Activate your app in the Dynamsoft portal.
  2. Install Dynamsoft Camera SDK for iOS using CocoaPods or install it manually.
  3. Add document capture to your app. Here we use Swift as an example.Please open ViewController.swift file and add the following code:
    class ViewController: UIViewController {
        var openVideoViewButton:UIButton!
        var dcsView:DcsView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            dcsView = DcsView.self.init(frame:CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height));
    
            //Show VideoView when DcsView is loaded
            dcsView.currentView=DVE_VIDEOVIEW
    
            ///////////////////video view settting//////////////////
            //Set the videoview capture mode to document mode
            dcsView.videoView.mode = DME_DOCUMENT
    
            //Set the next view after the cancel or capture button is clicked.
            dcsView.videoView.nextViewAfterCancel = DVE_IMAGEGALLERYVIEW
            dcsView.videoView.nextViewAfterCapture = DVE_EDITORVIEW
    
            view.addSubview(dcsView)
    
            openVideoViewButton = UIButton(frame:CGRect(x:self.view.center.x-100, y:self.view.center.y-20, width: 200, height: 40))
            openVideoViewButton.setTitle("Back to Camera", for: .normal)
            openVideoViewButton.setTitleColor(UIColor.blue, for: .normal)
            openVideoViewButton.addTarget(self, action:#selector(onClick), for:UIControlEvents.touchUpInside);
            dcsView.imageGalleryView.addSubview(openVideoViewButton)
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        @objc func onClick(){
            dcsView.currentView=DVE_VIDEOVIEW
        }
    }
    
  4. See document scan in action.Now if you launch the Hello World project, it will look like this:

    scan document in an iOS App

    Click the capture button, the document will show in the built-in editor view.

    document editor view

Scan to PDF

Now let’s save the captured document as PDF.

// Save the images of index 1-5 in buffer as a multi-page PDF 
let documentDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
let fileURL = documentDir!.appendingPathComponent("filename.pdf")
var indices = [Int]()
for i in 1...5 {
    if i < dcsView.buffer.count() {
    indices.append(i)
    }
}
let pdfEncode = DcsPDFEncodeParameter()
dcsView.io.save(indices, file: fileURL.path, encodeParameter: pdfEncode)

Try for Free

We provide 30-day free trial SDK, free demo app and also sample code for you to get started smoothly.

Subscribe Newsletter

Subscribe to our mailing list to get the monthly update.

Subscribename@email.com