Answers for "swift 5 show pdf in image view"

0

PDF Preview in Swift iOS

import UIKit
import PDFKit

class ViewController: UIViewController {

    @IBOutlet var pdfView: PDFView!

    override func viewDidLoad() {
        super.viewDidLoad()

        if let path = Bundle.main.path(forResource: "sample", ofType: "pdf") {
            if let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path)) {
                pdfView.displayMode = .singlePageContinuous
                pdfView.autoScales = true
                pdfView.displayDirection = .vertical
                pdfView.document = pdfDocument
            }
        }
    }
}
Posted by: Guest on December-18-2021
0

swift pdf preview image

import PDFKit

func pdfThumbnail(url: URL, width: CGFloat = 240) -> UIImage? {
  guard let data = try? Data(contentsOf: url),
  let page = PDFDocument(data: data)?.page(at: 0) else {
    return nil
  }

  let pageSize = page.bounds(for: .mediaBox)
  let pdfScale = width / pageSize.width

  // Apply if you're displaying the thumbnail on screen
  let scale = UIScreen.main.scale * pdfScale
  let screenSize = CGSize(width: pageSize.width * scale, 
                          height: pageSize.height * scale)

  return page.thumbnail(of: screenSize, for: .mediaBox)
}
Posted by: Guest on December-14-2020

Code answers related to "swift 5 show pdf in image view"

Code answers related to "Swift"

Browse Popular Code Answers by Language