

In the code example, we are creating the PDFView at a specific size, then setting up a PDFDocument with a PDF from the main bundle. PDFDocument manages the details of the document itself, as well as handling searching, writing, and selecting data within the PDF file. The PDFView object is the primary one you’ll be working with and controls the display area, nagivation, which PDF is loaded, and which page of the PDF is shown. Here’s a quick explanation of the code above: NSString *pathToPDF = *pdfDoc = initWithURL:] īe sure to read the official PDFKit documentation to better understand how all of these objects work. The returned value is the y-coordinate for the bottom of that text so that I know where to start writing the next line of text. Where titleStringRect is the CGRect containing the string. Then add this code to viewDidLoad or whatever other function you want to do this work: PDFView *pdfView = initWithFrame: Typically I can get the height of a single text item such as a title by doing the following: return +. Import PDFKit into your project at the top of your file: #import It displays a PDF file in the default vertical scroll view at the size of the current view. And it’ll create a simple PDF file which consists. The PDF composer simply presents a form as below for the user to enter the title and body text. Please refer to our previous tutorial about MVVM, if you’re new to it.
Pdfkit swift tutorial how to#
To start, here is the simplest implementation you can get, with no customization or options. In this tutorial, we are going to look at how to do it with SwiftUI and create a simple PDF composer app in the MVVM manner. Along with that, I’ll be including all of my code in a GitHub repository for reference and testing. With that in mind, I’m planning a series of blog posts to cover the use of PDFKit, to help other developers who are implementing it using Objective-C. In fact, even some of Apple’s documentation only includes Swift examples. While working with PDFKit on a project this year, I found the general online support to be somewhat lacking, especially for Objective-C.
