I made an app to scribble with PencilKit on a PDF file

Greetings

Hello. My name is HAAAAAN. This time, as the title suggests, I made an app that allows you to scribble on a PDF file. I would like to write it with the meaning of memorization.

Since I am a beginner, I know that there may be incorrect expressions and understandings, but thank you for your understanding.

In addition, this app is just for scribbling, so There is no save function. (Future tasks)

Environment

MacOS Xcode swift iPad Pro(iOS:13.6)

Purpose

I'll stay still

① Display the PDF file on the screen ② You can scribble on the displayed PDF file

I'll do it soon

① When the scribble is finished, save the PDF file with the scribble ② When ① is completed, convert the image file to a PDF file -As an image, it feels like taking a screenshot of the screen and converting the image to PDF. (I don't know if it can be done)

Preparation

--Open the storyboard and add one View to the original view controller. --Set PDFView to Class name of CustomClass of the added View and make an Outlet connection. --Add the PDF file you want to display to the project file.

code

qiita.swift


import UIKit
import PDFKit
import PencilKit


class ViewController: UIViewController{
    @IBOutlet weak var pdfView: PDFView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        if let sourceURL = Bundle.main.url(forResource: "Test", withExtension: "pdf") {
            if let source = PDFDocument(url: sourceURL) {
                pdfView.document = source //PDF URL
                pdfView.autoScales = true //PDF fits on screen
                pdfView.displayMode = .singlePage //1 page display
            }
        }

        let canvas = PKCanvasView(frame: view.frame) //Drawing area settings
        view.addSubview(canvas) //Add drawing area above PDF
        canvas.tool = PKInkingTool(.pen, color: .black, width: 15) //Pen settings
        canvas.isOpaque = false //Make the background transparent(Okay)

        if let window = UIApplication.shared.windows.first {
            if let toolPicker = PKToolPicker.shared(for: window) {
                toolPicker.addObserver(canvas) 
                toolPicker.setVisible(true, forFirstResponder: canvas)
                canvas.becomeFirstResponder()
                
            }
        }
    }
}

Clogged up

・ When implementing View As a configuration, put PDFView on the original view controller and put it on it I was thinking of putting a View for drawing, and I was able to implement it exactly as it was. In an article somewhere while wandering the Web, the screen goes black when drawing with a pen There was a thing that I encountered a problem, and I had the exact same symptom on the way.

Probably some processing is moving by the trigger at the time of gesture, I think When I changed the background color, the screen that should be black became the color set as the background color. I decided to make the View itself transparent.

At first, I set the alpha value for the background color of the View for drawing, but I stopped it By setting the isOpaque value of View to false, the problem at the time of gesture has disappeared.

References

- [Swift] Display PDF with PDFKit - Introducing PencilKit with just 3 lines of code to support Apple Pencil - [Swift] How to use isOpaque to make windows transparent

This time I just combined the above articles! (Don!!!)

Later talk

PDF Annotation is one of the features of PDFKit without installing PencilKit There is a function called (annotation function), which means that it can be drawn directly on the PDF ... It seems that you can save the trouble of capturing from an Image file to a PDF file, so I wonder if I can implement it using that.

Recommended Posts

I made an app to scribble with PencilKit on a PDF file
I want to push an app made with Rails 6 to GitHub
I made a rock-paper-scissors app with kotlin
I made a calculator app on Android
I made a rock-paper-scissors app with android
I made a viewer app that displays a PDF
I made a portfolio with Ruby On Rails
Try running an app made with Quarkus on Heroku
I want to monitor a specific file with WatchService
I tried OCR processing a PDF file with Java
I made a chat app.
[Rails] I tried to create a mini app with FullCalendar
I made a plugin to execute jextract with Gradle task
I tried OCR processing a PDF file with Java part2
I made a shopify app @java
I made a GUI with Swing
I made a matching app (Android app)
[Android] I made a pedometer app.
[Rails] I made a simple calendar mini app with customized specifications.
I want to add a browsing function with ruby on rails
I made a tool to output the difference of CSV file
I want to make an ios.android app
I made an eco server with scala
I made a risky die with Ruby
I want to display a PDF in Chinese (Korean) with thin reports
I want to ForEach an array with a Lambda expression in Java
How to make an app with a plugin mechanism [C # and Java]
I made a site that summarizes information on carbohydrate restriction with Vue.js
I want to create a Servlet war file with OpenJDK on CentOS7. Without mvn. With no internet connection.
I want to download a file on the Internet using Ruby and save it locally (with caution)
I will expose the fucking app that I made hard to get a job as an engineer from inexperienced.
Read a string in a PDF file with Java
Downgrade an existing app created with rails 5.2.4 to 5.1.6
04. I made a front end with SpringBoot + Thymeleaf
I made a mosaic art with Pokemon images
I made an app for myself! (Reading management app)
I want to be able to read a file using refile with administrate [rails6]
I made a gender selection column with enum
Publish the app made with ruby on rails
I want to play a GIF image on the Andorid app (Java, Kotlin)
I made an Android app for MiRm service
I tried to create a LINE clone app
I made a Docker container to run Maven
Run an application made with Go on Heroku
Rails6 I want to make an array of values with a check box
How to create an Excel form using a template file with Spring MVC
I made a LINE bot with Rails + heroku
I tried to break a block with java (1)
I tried deploying a page made with antique Middleman on Azure Static Web Apps
I made a lock pattern using the volume key with the Android app. Fragment edition
I made a function to register images with API in Spring Framework. Part 1 (API edition)
I made an iPhone Theremin with Vision framework + AudioKit
A simple CRUD app made with Nuxt / Laravel (Docker)
I tried to implement file upload with Spring MVC
How to push an app developed with Rails to Github
I made a method to ask for Premium Friday
[Ruby] I made a crawler with anemone and nokogiri.
I made a drawing chat "8bit paint chat" on WebAssembly
I want to dark mode with the SWT app
Create a user with an empty password on CentOS7
I made a library for displaying tutorials on Android.