![]() ![]() Below is an example comparison of how a PDF appears using PDFView and PSPDFKit. Our iOS PDF SDK, on the other hand, is a feature-rich, battle-tested framework trusted by some of the largest companies in the world. Another downside of using PDFKit is that the framework update frequency isn’t too high, meaning that the introduction of newer features and fixing of existing bugs could take some time. While it’s relatively easy to do, the PDFKit framework was only introduced in 2017, and it still lacks some features that certain users might require. This blog post covered how to create and modify a PDF in Swift using PDFKit. PDFKit has methods on PDFView to convert the coordinates to the PDF space, but there’s one issue with them, which is that if the bounds of the PDFView are bigger than that of the page size in the document, you could end up making changes outside the bounds of the drawn page and losing your content. One thing that needs to be considered is that the coordinate system of a PDF is different than that of the views: The origin of the coordinates in a PDF is in the bottom-left corner, instead of top-left corner like in the views. let squareAnnotation = PDFAnnotation(bounds: CGRect(x: 200, y: 100, width: 100, height: 100), forType: PDFAnnotationSubtype.square, withProperties: nil) But you can leverage the PDFKit framework to view PDF documents in your apps: The traditional approach for viewing a PDF is to open it in a web view. Apart from these basic keys, there are other keys that can be used to create the metadata for a PDF. Since the annotated Core Foundation APIs are automatically memory-managed in Swift, you don’t have to be concerned with the manual retaining and releasing of objects here. Closes the current PDF context and ends writing to the file. Let stringRect = CGRect(x: (pageSize.width / 2 - stringSize.width / 2), y: 20, width: stringSize.width, height: stringSize.height) Let stringSize = attributedPDFTitle.size() ![]() let attributedPDFTitle = NSAttributedString(string: pdfTitle, attributes: ) Let's draw the title of the PDF on top of the page. ![]() Let font = UIFont.preferredFont(forTextStyle. let pageSize = UIGraphicsGetPDFContextBounds().size Creates a new page in the current PDF context. UIGraphicsBeginPDFContextToFile(filePath, CGRect.zero, pdfMetadata) Creates a new PDF file at the specified path. KCGPDFContextOwnerPassword: "myPassword123" Used to enable/disable different permissions. Encrypts the document with the value as the owner password. The name of the application creating the PDF. Let filePath = (documentsDirectory as NSString).appendingPathComponent( "foo.pdf") as String let pdfTitle = "Swift-Generated PDF" let pdfMetadata = [ Let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory. It’s important to note that PDFKit doesn’t allow creating a PDF, and as such, Core Graphics needs to be used: There’s a set of auxiliary dictionary keys that are part of the PDF, and these keys are used to specify the metadata for the PDF. The metadata information for a PDF has to be set while generating the PDF context for its creation. Lastly, you’ll find out how to add annotations and write changes to the PDF. Then, you’ll learn how you can display a PDF with PDFKit. First, you’ll see how to work with Core Graphics to create a PDF. In this tutorial, you’ll learn how to use the basics of PDFKit to configure and modify a PDF document. One such way is with the PDFKit framework from Apple, which has been available since iOS 11 and allows for configuring the metadata of a PDF document and customizing the PDF by adding widgets and annotations. In the past, we talked about the different ways to create a PDF on iOS. PDF is one of the most popular file formats in the world, and viewing PDF files is supported on almost all devices and operating systems out of the box. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |