How to show and edit existing PDF files in ios application

前端 未结 3 1365
小鲜肉
小鲜肉 2020-12-09 06:46

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 possibl

相关标签:
3条回答
  • 2020-12-09 07:00

    You can use Quartz2D to manipulate your PDF, open them, make transformation and so on.

    Check out the official documentation

    Update :

    Create your pdf context as explained in the documentation, then you just have to write text :

    http://developer.apple.com/library/ios/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_text/dq_text.html#//apple_ref/doc/uid/TP30001066-CH213-TPXREF101

    0 讨论(0)
  • 2020-12-09 07:15

    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()
    }
    
    0 讨论(0)
  • 2020-12-09 07:20

    I know its too late for this question, however I'd like to add my solution.

    I am using UIGraphicsBeginPDFContextToFile to generate a PDF file. I've created a class called PDFCreator from base class UIViewController, in that I have two functions like this:

    - (void) beginContextWithFile:(NSString *)filename
    {
        pageSize = CGSizeMake(1024, 1424);
    
        UIGraphicsBeginPDFContextToFile(filename, CGRectZero, nil);
    
        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
    
        [self createInitialPart];
    }
    
    - (void) endContext
    {
        UIGraphicsEndPDFContext();
    }
    

    I've created an object of this class in the app delegate file, as it will retain the object until the app terminates (as was my requirement).

    Initially, I am calling beginContextWithFile: and it will create a pdf file in the document directory along with data I am adding through a call to the createInitialPart method.

    At some point later I need to update that file, so I have another method named secondPart though I called it for some more data to append to that file.

    Once I fill I've done with the creation, I need to call endContext of PDFCreator class, that will complete the pdf generation and yes, I will have the updated file.

    This is little tricky and I didn't perform any memory checks though I had the requirement, I found the solution :)

    0 讨论(0)
提交回复
热议问题