Example Edit PDF: Create a PDF with only selected pages in Swift 3.0 with CGPDFDocument
Whenever you want to edit PDF in iOS applications which is very straight forward.
- Get the URL of PDF file using ‘NSURL(fileURLWithPath:)’
- Create a PDF document of type CGPDFDocument from the URL ‘CGPDFDocument(pdfURL as CFURL)’
- Create a new URL to save editable PDF
- Give page size of the page to the new PDF
- Get the write context to write in new PDF using ‘ CGContext(_url, mediaBox: &_mediaBox, nil)’
- Run a loop to get number of pages from the existing PDF
- Get the PageRect of each page
- This is important, you have to write context between methods ‘writeContext!.beginPage(mediaBox: &_pageRect)’ and _writeContext!.endPage();
- draw existing PDF page to new PDF
That’s it the new PDF will store at the URL given by you!
This is the example GitHubGist :
If you have any questions/comments/concern please reach out to me Ravi Shah