Skip to content

Instantly share code, notes, and snippets.

@tempire
Created April 25, 2015 09:24
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tempire/0bd8cb78290aa9bc6adf to your computer and use it in GitHub Desktop.
Save tempire/0bd8cb78290aa9bc6adf to your computer and use it in GitHub Desktop.
swift draw image on pdf
func drawOnPDF(path: String) {
// Get existing Pdf reference
let pdf = CGPDFDocumentCreateWithURL(NSURL(fileURLWithPath: path))
// Get page count of pdf, so we can loop through pages and draw them accordingly
let pageCount = CGPDFDocumentGetNumberOfPages(pdf);
// Write to file
UIGraphicsBeginPDFContextToFile(path, CGRectZero, nil)
// Write to data
//var data = NSMutableData()
//UIGraphicsBeginPDFContextToData(data, CGRectZero, nil)
for index in 1...pageCount {
let page = CGPDFDocumentGetPage(pdf, index)
let pageFrame = CGPDFPageGetBoxRect(page, kCGPDFMediaBox)
UIGraphicsBeginPDFPageWithInfo(pageFrame, nil)
var ctx = UIGraphicsGetCurrentContext()
// Draw existing page
CGContextSaveGState(ctx);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -pageFrame.size.height);
CGContextDrawPDFPage(ctx, page);
CGContextRestoreGState(ctx);
// Draw image on top of page
var image = UIImage(named: "signature3")
image?.drawInRect(CGRectMake(100, 100, 100, 100))
// Draw red box on top of page
//UIColor.redColor().set()
//UIRectFill(CGRectMake(20, 20, 100, 100));
}
UIGraphicsEndPDFContext()
}
@travoux
Copy link

travoux commented May 7, 2015

Thanks man.. have been searching for this. What if I want to draw a semi transparent rectangle not an image?

@suculent
Copy link

Exactly what I was looking for. Even the PDF-on-top transparency works. Thanks!

@lenichols
Copy link

how do you apply this to a uiview?

@X901
Copy link

X901 commented Feb 17, 2019

Thank you very much, you are a lifesaver!

I did convert it correctly to Swift 4.2

   func drawOnPDF(path: String , signatureImage:UIImage) {
        
        // Get existing Pdf reference
    let pdf = CGPDFDocument(NSURL(fileURLWithPath: path))
        
        // Get page count of pdf, so we can loop through pages and draw them accordingly
    let pageCount = pdf?.numberOfPages
    
    
        // Write to file
        UIGraphicsBeginPDFContextToFile(path, CGRect.zero, nil)
        
        // Write to data
        //var data = NSMutableData()
        //UIGraphicsBeginPDFContextToData(data, CGRectZero, nil)
        
        for index in 1...pageCount! {
            
            let page =  pdf?.page(at: index)

            let pageFrame = page?.getBoxRect(.mediaBox)

            
            UIGraphicsBeginPDFPageWithInfo(pageFrame!, nil)
                    
            let ctx = UIGraphicsGetCurrentContext()
            
            // Draw existing page
            ctx!.saveGState()
            
            ctx!.scaleBy(x: 1, y: -1)
          
            ctx!.translateBy(x: 0, y: -pageFrame!.size.height)
            //CGContextTranslateCTM(ctx, 0, -pageFrame.size.height);
            ctx!.drawPDFPage(page!)
            ctx!.restoreGState()
            
            // Draw image on top of page
            let image = signatureImage
            image.draw(in: CGRect(x: 100, y: 100, width: 100, height: 100))
            // Draw red box on top of page
            //UIColor.redColor().set()
            //UIRectFill(CGRectMake(20, 20, 100, 100));
        }
        
        
        UIGraphicsEndPDFContext()
    }

@shival
Copy link

shival commented Aug 2, 2019

This works only on first page of the pdf file. Using this code actually removes the actual content on pdf and replaces it with the new image if there more than one pages in the pdf.

@HsS-2019
Copy link

thank you, it work to me .

@MerrickWang1
Copy link

What is the path?

@ducnguyen6431
Copy link

@MerrickWang1, path is location to pdf file stored on local

@andreytorlopovold
Copy link

Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment