func drawOnPDF(path: String)
{
// Get existing Pdf reference
let pdf = CGPDFDocumentCreateWithURL(NSURL(fileURLWithPath: path))
// Get page count of pdf
Swift 3:
func convertPDFPageToImage(page:Int) {
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let filePath = documentsURL.appendingPathComponent("pathLocation").path
do {
let pdfdata = try NSData(contentsOfFile: filePath, options: NSData.ReadingOptions.init(rawValue: 0))
let pdfData = pdfdata as CFData
let provider:CGDataProvider = CGDataProvider(data: pdfData)!
let pdfDoc:CGPDFDocument = CGPDFDocument(provider)!
pageCount = pdfDoc.numberOfPages;
let pdfPage:CGPDFPage = pdfDoc.page(at: page)!
var pageRect:CGRect = pdfPage.getBoxRect(.mediaBox)
pageRect.size = CGSize(width:pageRect.size.width, height:pageRect.size.height)
print("\(pageRect.width) by \(pageRect.height)")
UIGraphicsBeginImageContext(pageRect.size)
let context:CGContext = UIGraphicsGetCurrentContext()!
context.saveGState()
context.translateBy(x: 0.0, y: pageRect.size.height)
context.scaleBy(x: 1.0, y: -1.0)
context.concatenate(pdfPage.getDrawingTransform(.mediaBox, rect: pageRect, rotate: 0, preserveAspectRatio: true))
context.drawPDFPage(pdfPage)
context.restoreGState()
let pdfImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
self.imageView.image = pdfImage
}
catch {
}
}
I have found a cool solution. Borrowed from here: https://www.hackingwithswift.com/example-code/core-graphics/how-to-render-a-pdf-to-an-image
func drawPDFfromURL(url: URL) -> UIImage? {
guard let document = CGPDFDocument(url as CFURL) else { return nil }
guard let page = document.page(at: 1) else { return nil }
let pageRect = page.getBoxRect(.mediaBox)
let renderer = UIGraphicsImageRenderer(size: pageRect.size)
let img = renderer.image { ctx in
UIColor.white.set()
ctx.fill(pageRect)
ctx.cgContext.translateBy(x: 0.0, y: pageRect.size.height)
ctx.cgContext.scaleBy(x: 1.0, y: -1.0)
ctx.cgContext.drawPDFPage(page)
}
return img
}
I Got the Solution for Pdf page convert in UIImage. This code PDF Page convert in Uiimage and save in image document directory. PDF Download And Page Count to separate Page vise image convert.
func drawOnPDF(path: String)
{
var urlstr: NSURL = NSURL.fileURLWithPath(path)!
var pdf: CGPDFDocumentRef = CGPDFDocumentCreateWithURL(urlstr)
var page: CGPDFPageRef;
var frame: CGRect = CGRectMake(0, 0, 100, 200)
var pageCount: Int = CGPDFDocumentGetNumberOfPages(pdf);
for (var i = 0; i < pageCount; i++)
{
var mypage: CGPDFPageRef = CGPDFDocumentGetPage(pdf, i+1)
frame = CGPDFPageGetBoxRect(mypage, kCGPDFMediaBox)
UIGraphicsBeginImageContext(CGSizeMake(600, 600*(frame.size.height/frame.size.width)))
var ctx: CGContextRef = UIGraphicsGetCurrentContext()
CGContextSaveGState(ctx)
CGContextTranslateCTM(ctx, 0.0, frame.size.height)
CGContextScaleCTM(ctx, 1.0, -1.0)
CGContextSetGrayFillColor(ctx, 1.0, 1.0)
CGContextFillRect(ctx, frame)
page = CGPDFDocumentGetPage(pdf, i + 1)
var pdfTransform: CGAffineTransform = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, frame, 0, true)
CGContextConcatCTM(ctx, pdfTransform);
CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh)
CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault)
CGContextDrawPDFPage(ctx, page)
var thumbnailImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
CGContextRestoreGState(ctx)
var documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as! String
documentsPath = documentsPath.stringByAppendingFormat("/Page%d.png", i+1)
UIGraphicsEndImageContext()
imagedata = UIImagePNGRepresentation(thumbnailImage)
imagedata.writeToFile(documentsPath, atomically: true)
array.addObject(documentsPath)
}
let dirPath = array.objectAtIndex(0) as? String
let image = UIImage(contentsOfFile: dirPath!)
NSUserDefaults.standardUserDefaults().setObject(array, forKey: "Array")
println("\(array)")
}