Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show and edit existing PDF files in ios application

I do not want to create new PDF file,that I had already done but want to show and edit existing pdf file in iOS through code..

Is this possible or not and if possible than how can I do this...

Update

Suppose there will be text document pdf and we want to enter some other text in that file and save it..so how to do this by programming?

Please help me to solve this problem...

Thanks in advance...

like image 761
Mehul Mistri Avatar asked Oct 04 '11 07:10

Mehul Mistri


1 Answers

I was able to do this by creating a copy of the original PDF and modifying it during the build process.

PS: In my solution I needed to edit the document info of the new PDF, so I used the subject parameter to do this.

In Swift 3

func createPDF(on path: String?, from templateURL: URL?, with subject: String?) {
    guard let newPDFPath = path,
        let pdfURL = templateURL else { return }

    let options = [(kCGPDFContextSubject as String): subject ?? ""] as CFDictionary

    UIGraphicsBeginPDFContextToFile(newPDFPath, .zero, options as? [AnyHashable : Any])

    let templateDocument = CGPDFDocument(pdfURL as CFURL)
    let pageCount = templateDocument?.numberOfPages ?? 0

    for i in 1...pageCount {

        //get bounds of template page
        if let templatePage = templateDocument?.page(at: i) {
            let templatePageBounds = templatePage.getBoxRect(.cropBox)

            //create empty page with corresponding bounds in new document
            UIGraphicsBeginPDFPageWithInfo(templatePageBounds, nil)
            let context = UIGraphicsGetCurrentContext()

            //flip context due to different origins
            context?.translateBy(x: 0.0, y: templatePageBounds.height)
            context?.scaleBy(x: 1.0, y: -1.0)

            //copy content of template page on the corresponding page in new file
            context?.drawPDFPage(templatePage)

            //flip context back
            context?.translateBy(x: 0.0, y: templatePageBounds.height)
            context?.scaleBy(x: 1.0, y: -1.0)


            // -->>>>  Do your change here  <<<<--
            /* Here you can do any drawings */

        }
    }
    UIGraphicsEndPDFContext()
}
like image 116
Enrique Avatar answered Nov 15 '22 05:11

Enrique