In our Unity App for iOS build, when we opened the PDF from the app, it is automatically opening in landspace mode instead of portrait. In the android and windows apps, we are able to open in the portrait mode. We tried to make the changes in the project settings but it did not change.
Any way in which we can acheive this would be helpful for us.
PDFKit
RSS for tagDisplay and manipulate PDF documents in your applications using PDFKit.
Posts under PDFKit tag
42 Posts
Sort by:
Post
Replies
Boosts
Views
Activity
The Problem
Push buttons (created as a PDFAnnotation using PDFKit) do not properly write the associated caption's key-value pair (within the annotation's appearance characteristics dictionary) to a PDF document.
What is Happening
Push button widget annotations can have a caption that is displayed as the button’s label.
In the PDF 1.7 specification (ISO PDF32000-2008, s. 12.5.6.19), a widget annotation can have an ‘appearance characteristics dictionary’ (MK) with properties to construct the appearance of the widget. The caption property (CA) is used to construct a button’s caption/label.
PDFKit uses the PDFAnnotation .caption property to set the value of a push button’s caption as a string.
Observation 1:
In an open PDF document (using PDFView), a push button widget annotation can be created and added to a PDFPage using the following code:
let pushButton = PDFAnnotation(bounds: pushButtonBounds, forType: .widget, withProperties: nil)
pushButton.widgetFieldType = .button
pushButton.widgetControlType = .pushButtonControl
pushButton.caption = "My Button"
page.addAnnotation(pushButton)
The PDFAnnotation .caption property is used to set the caption to the required string. As a result, the push button is correctly displayed on the PDFPage with the correct label being display on the button.
While the PDF document remains open, the appearance characteristics dictionary (an PDFAppearanceCharacteristics object) retains a key-value pair for the caption with the correct value as expected.
On saving/writing to the PDF file, however, the key-value pair for the caption in the appearance characteristics dictionary is not written to the PDF document’s file.
Resulting PDF markup:
6 0 obj
<< /Rect [ 256 299.8977 356 399.8977 ] /Border [ 0 0 0 ] /T (button23) /F
4 /Subtype /Widget /DA (/.AppleSystemUIFont 13 Tf 0 g) /MK 8 0 R /C [ 0 ]
/AP 9 0 R /V /Off /M (D:20250330154918Z00'00') /FT /Btn /Type /Annot /Ff 65536
>>
endobj
9 0 obj
<< /N 10 0 R >>
endobj
8 0 obj
<< /BG [ 0.75 ] >>
endobj
10 0 obj
<< /Filter /FlateDecode /Type /XObject /Subtype /Form /FormType 1 /BBox [0 0 100 100]
/Resources 11 0 R /Length 170 >>
stream
x }ê1 Ç0 Öw~≈ ahÈ KÈ
q1q0\‚`ú Ÿ¿ 8¯Ôm% u0óª‰.Ô{yπ åP°H-}ª‡à y3 ¸ %≠¡‰ %› g¨$•µMVXø‡Hé†Ö ”î“¿˜® BI•L ˆ†b A pü‰Ã @ÓpB∫ †æœs ãÙ:d8Éwÿr»/}” €∂I÷Bõ B;'+gm Ô˝„ mÙ~ L*>•
endstream
endobj
On closing the PDF document, the assigned value for the push button’s caption is not written to the file and is lost.
Observation 2:
On reopening the PDF document, and assigning a new value for the already-created push button’s caption, a key-value pair for the caption is again correctly added to the PDFAnnotation appearance characteristics dictionary.
On saving/writing to the PDF file, this time, the caption key-value pair in the appearance characteristics dictionary is correctly written/saved to the PDF document file.
Resulting PDF markup:
6 0 obj
<< /Border [ 0 0 0 ] /Rect [ 256 299.8977 356 399.8977 ] /T (button23) /F
4 /BS 8 0 R /Subtype /Widget /DA (/.AppleSystemUIFont 13 Tf 0 g) /MK 9 0 R
/C [ 0 ] /AP 10 0 R /V /Off /M (D:20250330154918Z00'00') /FT /Btn /Type /Annot
/Ff 65536 >>
endobj
10 0 obj
<< /N 11 0 R >>
endobj
9 0 obj
<< /BG [ 0.75 ] /CA (My Button) >>
endobj
8 0 obj
<< /W 0 >>
endobj
11 0 obj
<< /Filter /FlateDecode /Type /XObject /Subtype /Form /FormType 1 /BBox [0 0 100 100]
/Resources 12 0 R /Length 163 >>
stream
x uè1 ¬@ Ö˜˛ä7∂√]ì´◊Î≠ ¡A 8à”a∑Vj·ø˜jë™ !ÅÑ|y/=ˆËA1òʺ]pDá|=0¬“Œb ø+Õ gùf2E≤∞Ê≈N` û·Xm©-BãZ†H Ÿ
¿≈ºPÄ= Ø míãp •¡ ÈÓÅ˙>é “kó· Ÿb#—¬ Ûã¶2∂Ñ2fiÎ ;óDÌiÓ?ü>LÁûÊy;}
endstream
endobj
Impact on User Experience:
Push button captions may not be properly saved to the PDF document’s file. This may result in an application redrawing a push button without a caption/label. More so, an application that uses the caption value to “read” a button’s label (e.g., for accessibility purposes) will not be able to do so.
iOS 18 emulator wkwebview loading pdf display blank
At this line of code (SketchTextSelectionManager.swift:449), sometimes there will be crashes based on crashlytics reports.
let selection = pdfPage.selectionForWord(at: location)
This is directly calling into PDFKit's PDFPage#selection method: https://developer.apple.com/documentation/pdfkit/pdfpage/selectionforword(at:)
Attached the full stacktrace:
Crashed: com.apple.root.user-initiated-qos.cooperative
0 CoreGraphics 0x30c968 PageLayout::getWordRange(unsigned long, long) const + 908
1 CoreGraphics 0x30bbc0 PageLayout::getTextRangeIndex(CGPoint, CGPDFSelectionType, SelectionPrecision) const + 2292
2 CoreGraphics 0x44a53c CGPDFSelectionCreateBetweenPointsWithOptions + 384
3 PDFKit 0x8d5f8 -[PDFPage selectionFromPoint:toPoint:type:] + 168
4 PDFKit 0x92040 -[PDFPage _rvItemAtPoint:] + 64
5 PDFKit 0x91f4c -[PDFPage rvItemAtPoint:] + 84
6 PDFKit 0x8caa8 -[PDFPage selectionForWordAtPoint:] + 40
7 MyApp 0x8420e0 closure #1 in SketchTextSelectionManager.startNewTextSelection(pageId:location:) + 449 (SketchTextSelectionManager.swift:449)
8 MyApp 0x841a70 SketchTextSelectionManager.startNewTextSelection(pageId:location:) + 205 (CurrentNoteManager.swift:205)
9 libswift_Concurrency.dylib 0x61104 swift::runJobInEstablishedExecutorContext(swift::Job*) + 252
10 libswift_Concurrency.dylib 0x63a28 (anonymous namespace)::ProcessOutOfLineJob::process(swift::Job*) + 480
11 libswift_Concurrency.dylib 0x611c4 swift::runJobInEstablishedExecutorContext(swift::Job*) + 444
12 libswift_Concurrency.dylib 0x62514 swift_job_runImpl(swift::Job*, swift::SerialExecutorRef) + 144
13 libdispatch.dylib 0x15d8c _dispatch_root_queue_drain + 392
14 libdispatch.dylib 0x16590 _dispatch_worker_thread2 + 156
15 libsystem_pthread.dylib 0x4c40 _pthread_wqthread + 228
16 libsystem_pthread.dylib 0x1488 start_wqthread + 8
```
At this line of code (SketchTextSelectionManager.swift:378), sometimes there will be crashes based on crashlytics reports. In the reports, it seems like this only happens for RTL text range.
let selection = pdfPage.selection(
from: CGPoint(x: fromStart.x + 1, y: fromStart.y - 1),
to: CGPoint(x: toEnd.x - 1, y: toEnd.y + 1)
)
This is directly calling into PDFKit's PDFPage#selection method: https://developer.apple.com/documentation/pdfkit/pdfpage/selection(from:to:)
Attached the full stacktrace:
Crashed: com.apple.root.user-initiated-qos.cooperative
0 CoreGraphics 0x30598c PageLayout::convertRTLTextRangeIndexToStringRangeIndex(long) const + 156
1 CoreGraphics 0x44c3f0 CGPDFSelectionCreateBetweenPointsWithOptions + 224
2 PDFKit 0x91d00 -[PDFPage selectionFromPoint:toPoint:type:] + 168
3 MyApp 0x841044 closure #1 in SketchTextSelectionManager.handleUserTouchMoved(_:) + 378 (SketchTextSelectionManager.swift:378)
4 MyApp 0x840cb0 SketchTextSelectionManager.handleUserTouchMoved(_:) + 205 (CurrentNoteManager.swift:205)
5 libswift_Concurrency.dylib 0x60f5c swift::runJobInEstablishedExecutorContext(swift::Job*) + 252
6 libswift_Concurrency.dylib 0x63a28 (anonymous namespace)::ProcessOutOfLineJob::process(swift::Job*) + 480
7 libswift_Concurrency.dylib 0x6101c swift::runJobInEstablishedExecutorContext(swift::Job*) + 444
8 libswift_Concurrency.dylib 0x62514 swift_job_runImpl(swift::Job*, swift::SerialExecutorRef) + 144
9 libdispatch.dylib 0x15ec0 _dispatch_root_queue_drain + 392
10 libdispatch.dylib 0x166c4 _dispatch_worker_thread2 + 156
11 libsystem_pthread.dylib 0x3644 _pthread_wqthread + 228
12 libsystem_pthread.dylib 0x1474 start_wqthread + 8
By setting the PKCanvasView background color to blue, I can tell that the PKCanvasView for each PDFPage is created normally, but it does not respond to touch. Specifically, whether it is finger or applepencil, all the responses of the page occur from PDFView(such as zoom and scroll), and PKCanvasView can not draw, please how to solve?
class PDFAnnotatableViewController: UIViewController, PDFViewDelegate {
private let pdfView = PDFView()
private var pdfDocument: PDFDocument?
let file: FileItem
private var userSettings: UserSettings
@Binding var selectedPage: Int
@Binding var currentMode: Mode
@Binding var latestPdfChatResponse: LatestPDFChatResponse
@State private var pdfPageCoordinator = PDFPageCoordinator()
@ObservedObject var userMessage: ChatMessage
init(file: FileItem,
userSettings: UserSettings,
drawDataList: Binding<[DrawDataItem]>,
selectedPage: Binding<Int>,
currentMode: Binding<Mode>,
latestPdfChatResponse: Binding<LatestPDFChatResponse>,
userMessage: ChatMessage) {
self.file = file
self.userSettings = userSettings
self._selectedPage = selectedPage
self._currentMode = currentMode
self._latestPdfChatResponse = latestPdfChatResponse
self.userMessage = userMessage
super.init(nibName: nil, bundle: nil)
DispatchQueue.global(qos: .userInitiated).async {
if let document = PDFDocument(url: file.pdfLocalUrl) {
DispatchQueue.main.async {
self.pdfDocument = document
self.pdfView.document = document
self.goToPage(selectedPage: selectedPage.wrappedValue - 1)
}
}
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
setupPDFView()
}
private func setupPDFView() {
pdfView.delegate = self
pdfView.autoScales = true
pdfView.displayMode = .singlePage
pdfView.displayDirection = .vertical
pdfView.backgroundColor = .white
pdfView.usePageViewController(true)
pdfView.displaysPageBreaks = false
pdfView.displaysAsBook = false
pdfView.minScaleFactor = 0.8
pdfView.maxScaleFactor = 3.5
pdfView.pageOverlayViewProvider = pdfPageCoordinator
if let document = pdfDocument {
pdfView.document = document
goToPage(selectedPage: selectedPage)
}
pdfView.frame = view.bounds
pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(pdfView)
NotificationCenter.default.addObserver(
self,
selector: #selector(handlePageChange),
name: .PDFViewPageChanged,
object: pdfView
)
}
// Dealing with page turning
@objc private func handlePageChange(notification: Notification) {
guard let currentPage = pdfView.currentPage, let document = pdfView.document else { return }
let currentPageIndex = document.index(for: currentPage)
if currentPageIndex != selectedPage - 1 {
DispatchQueue.main.async {
self.selectedPage = currentPageIndex + 1
}
}
}
func goToPage(selectedPage: Int) {
guard let document = pdfView.document else { return }
if let page = document.page(at: selectedPage) {
pdfView.go(to: page)
}
}
// Switch function
func togglecurrentMode(currentMode: Mode){
DispatchQueue.main.async {
if self.currentMode == .none{
self.pdfView.usePageViewController(true)
self.pdfView.isUserInteractionEnabled = true
} else if self.currentMode == .annotation {
if let page = self.pdfView.currentPage {
if let canvasView = self.pdfPageCoordinator.getCanvasView(forPage: page) {
canvasView.isUserInteractionEnabled = true
canvasView.tool = PKInkingTool(.pen, color: .red, width: 20)
canvasView.drawingPolicy = .anyInput
canvasView.setNeedsDisplay()
}
}
}
}
}
}
class MyPDFPage: PDFPage {
var drawing: PKDrawing?
func setDrawing(_ drawing: PKDrawing) {
self.drawing = drawing
}
func getDrawing() -> PKDrawing? {
return self.drawing
}
}
class PDFPageCoordinator: NSObject, PDFPageOverlayViewProvider {
var pageToViewMapping = [PDFPage: PKCanvasView]()
func pdfView(_ view: PDFView, overlayViewFor page: PDFPage) -> UIView? {
var resultView: PKCanvasView? = nil
if let overlayView = pageToViewMapping[page] {
resultView = overlayView
} else {
let canvasView = PKCanvasView(frame: view.bounds)
canvasView.drawingPolicy = .anyInput
canvasView.tool = PKInkingTool(.pen, color: .systemYellow, width: 20)
canvasView.backgroundColor = .blue
pageToViewMapping[page] = canvasView
resultView = canvasView
}
if let page = page as? MyPDFPage, let drawing = page.drawing {
resultView?.drawing = drawing
}
return resultView
}
func pdfView(_ pdfView: PDFView, willEndDisplayingOverlayView overlayView: UIView, for page: PDFPage) {
guard let overlayView = overlayView as? PKCanvasView, let page = page as? MyPDFPage else { return }
page.drawing = overlayView.drawing
pageToViewMapping.removeValue(forKey: page)
}
func savePDFDocument(_ pdfDocument: PDFDocument) -> Data {
for i in 0..<pdfDocument.pageCount {
if let page = pdfDocument.page(at: i) as? MyPDFPage, let drawing = page.drawing {
let newAnnotation = PDFAnnotation(bounds: drawing.bounds, forType: .stamp, withProperties: nil)
let codedData = try! NSKeyedArchiver.archivedData(withRootObject: drawing, requiringSecureCoding: true)
newAnnotation.setValue(codedData, forAnnotationKey: PDFAnnotationKey(rawValue: "drawingData"))
page.addAnnotation(newAnnotation)
}
}
let options = [PDFDocumentWriteOption.burnInAnnotationsOption: true]
if let resultData = pdfDocument.dataRepresentation(options: options) {
return resultData
}
return Data()
}
func getCanvasView(forPage page: PDFPage) -> PKCanvasView? {
return pageToViewMapping[page]
}
}
Is there an error in my code? Please tell me how to make PKCanvasView painting normally?
I am creating an iOS app that needs to parse the text from a PDF document. I can read the entire PDF document's text using the string property, but if it's a large PDF document, this could cause delays for users.
From the documentation, I came across the beginFindString function, which seems to asynchronously, with no return?
https://developer.apple.com/documentation/pdfkit/pdfdocument/beginfindstring(_:withoptions:))
Unfortunately I cannot find examples on how to use this function or its intended purpose/functionality, so any guidance would be appreciated.
My goal is to read the PDF document one line at a time, searching for newlines ('\n'), then parsing that line as needed. I'm hoping the beginFindString function will be useful.
Reading text out of PDFs with PDFKit results in some text being returned way out of order when using .string or .attributedString functions. Way out of order means not just wrong sorting of words on a line or wrongly showing up on the next line (as has happened with PDFKit on older iOS releases, e.g. 17.x), but some text (one or more words) may show up near the end of a page of text, while it should show near the beginning.
As Page.characterBounds(at:) is buggy in iOS 18.x returns wrong bounds, devs cannot correct such faulty PDFKit behaviour programmatically.
I believe it is on Apple to fix this iOS 18 bug asap. Thank you for giving it priority as this is killing apps that need PDFKit to get and parse text data out of PDFs.
I have filed Feedback FB16264926.
I’m encountering an issue with the page.selectionForWord(at:) method in my application. This method has stopped working in Mac Designed for iPad mode. The functionality works fine on physical devices such as iPhone and iPad, as well as on Mac, but in this specific mode, the method no longer works, and the application crashes.
It is crucial for me that this functionality works in Mac Designed for iPad mode, as rewriting the entire code for Mac would be too time-consuming, especially considering the size of the application.
Interestingly, a similar method, page.selectionForLine(at:), works perfectly with the same parameters in all environments, including the Mac Designed for iPad mode. This makes the issue even more puzzling.
The issue began after the latest update. The app no longer responds to the page.selectionForWord(at:) method, which causes the application to crash.
I have attached a test app to reproduce the error.
When added a PDFAnnotation to a pdf file which already have type stamp annotation type annotation showing in PDFKit but in QLPreviewController not coming
My app uses PDFKit, but I don't know how to solve this bug at all. Under the same IOS system and device model, some users' devices may experience crashes, while our own devices are functioning normally.
The following is the stack information for crashing:
0 libsystem_platform.dylib__os_unfair_lock_recursive_abort + 36
1 libsystem_platform.dylib__os_unfair_lock_lock_slow + 308
2 CoreGraphics_CGPDFPageCopyRootTaggedNode + 56
3 PDFKit-[PDFPageViewAccessibility accessibilityElements] + 76
4 UIAccessibility-[NSObject(AXPrivCategory) _accessibilityElements] + 56
5 UIAccessibility-[NSObjectAccessibility accessibilityElementCount] + 68
6 UIAccessibility-[NSObject(AXPrivCategory) _accessibilityHasOrderedChildren] + 44
7 UIAccessibility-[NSObject(AXPrivCategory) _accessibilityFrameForSorting] + 216
8 UIAccessibility-[NSObject _accessibilityCompareGeometry:] + 116
9 UIAccessibility-[NSObject(AXPrivCategory) accessibilityCompareGeometry:] + 52
10 CoreFoundation___CFSimpleMergeSort + 100
11 CoreFoundation___CFSimpleMergeSort + 248
12 CoreFoundation_CFSortIndexes + 260
13 CoreFoundation-[NSArray sortedArrayFromRange:options:usingComparator:] + 732
14 CoreFoundation-[NSMutableArray sortedArrayFromRange:options:usingComparator:] + 60
15 CoreFoundation-[NSArray sortedArrayUsingSelector:] + 168
16 UIAccessibility___57-[NSObject(AXPrivCategory) _accessibilityFindDescendant:]_block_invoke + 268
17 UIAccessibility___96-[NSObject(AXPrivCategory) _accessibilityEnumerateAXDescendants:passingTest:byYieldingElements:]_block_invoke + 140
18 UIAccessibility-[NSObject _accessibilityEnumerateAXDescendants:passingTest:byYieldingElements:] + 244
19 UIAccessibility-[NSObject _accessibilityFindFirstAXDescendantPassingTest:byYieldingElements:] + 272
20 UIAccessibility-[NSObject(AXPrivCategory) _accessibilityFindDescendant:] + 100
21 UIAccessibility__axuiElementForNotificationData + 276
22 UIAccessibility__massageAssociatedElementBeforePost + 36
23 UIAccessibility__UIAXBroadcastMainThread + 292
24 libdispatch.dylib__dispatch_call_block_and_release + 32
25 libdispatch.dylib__dispatch_client_callout + 20
26 libdispatch.dylib__dispatch_main_queue_drain + 980
27 libdispatch.dylib__dispatch_main_queue_callback_4CF + 44
28 CoreFoundation___CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16
29 CoreFoundation___CFRunLoopRun + 1996
30 CoreFoundation_CFRunLoopRunSpecific + 572
31 GraphicsServices_GSEventRunModal + 164
32 UIKitCore-[UIApplication _run] + 816
33 UIKitCore_UIApplicationMain + 340
34 SwiftUIclosure #1 (Swift.UnsafeMutablePointer<Swift.UnsafeMutablePointer<Swift.Int8>?>) -> Swift.Never in SwiftUI.(KitRendererCommon in _ACC2C5639A7D76F611E170E831FCA491)(Swift.AnyObject.Type) -> Swift.Never + 168
35 SwiftUI SwiftUI.runApp(A) -> Swift.Never + 100
36 SwiftUI static (extension in SwiftUI):SwiftUI.App.main() -> () + 180
I am using PDFPageOverlayViewProvider from pdfkit. I would like to detect changes in the overlaid view and refresh PDFPageOverlayViewProvider. Currently, it does not refresh even when the overlaid view changes. Is there a way to refresh it?
In XCode15, if you specify mediaBox for PDFDisplayBox, the page will be shrunk to fit the specified range.
However, in XCode16, the page is cropped to fit the specified range, as when cropBox is specified for PDFDisplayBox.
I think it's a bug because the setBounds documentation hasn't been updated, but has anyone had the same problem?
Please let me know if there are any workarounds.
I'm building a MacOS app which reads a lot of PDFs in the background. Some of these PDF's have an embedded font which is not installed on the system. In such cases the app shows a popup asking whether to Download or Skip the font. This seems to be a PDFKit behavior because I see the same behavior when I open the file in Preview (see screenshot)
This behavior is disruptive to the user experience and I'd like to be able to disable font downloads. However I don't see any option in the PDFKit API to do so. Any ideas?
I have a SwiftUI View containing a PDFView from PDFKit (via NSViewRepresentable). When setting a new PDFDocument the view flashes briefly with the background color before displaying the new document.
Curiously the flashing is vastly reduced (but not eliminated) by calling layoutDocumentView() which should already be called from setDocument according to its documentation.
struct SheetView: NSViewRepresentable {
var pdf: PDFDocument?
func makeNSView(context: Context) -> PDFView {
let pdfView = PDFView()
pdfView.displaysPageBreaks = false
pdfView.displayMode = .singlePage
pdfView.pageShadowsEnabled = false
pdfView.autoScales = true
return pdfView
}
func updateNSView(_ pdfView: PDFView, context: Context) {
if pdf != pdfView.document {
pdfView.document = pdf
pdfView.layoutDocumentView() // reduces flashing but does not eliminate it
}
}
}
Hi, I'm working with UIDocument to store a PDF file. This is code I use for saving a file.
document.close(completionHandler: { (success) in
if success {
document.save(to: url,
for: .forOverwriting,
completionHandler: { (success) in
if success {
print("Saved file successfully")
}
})
}
})
This code works well with a small file size. But if it's a large file like 100MB, it takes around 2-5 minutes to save the file. Is there a way to save file with the changes only? Or create an auto save function triggered whenever the user edit the file?
I need to apply security measures to a PDF. My goal is to prevent it from being leaked, and even if it is leaked, I want to ensure that the PDF cannot be viewed. Is it possible to use pdfkit and apply DRM to achieve this?⬤
PDFKit PDFPage.characterBounds(at: Int) is returning incorrect coordinates with iOS 18 beta 4 and later / Xcode 16 beta 4.
It worked fine in iOS 17 and earlier (after showing the same issue during the early iOS 17 beta cycle)
It breaks critical functionality that my app relies on.
I have filed feedback (FB14843671).
So far no changes in the latest betas. iOS release date is approaching fast!
Anybody having the same issue? Any workaround available?
Dear all,
I have an app in which I'm trying to create a pdf from the data stored. Everything is working fine except the fact that the text is not following the page margins, going out of the page of being truncated without being written on the line below.
Has anybody experienced the same in the past?
Please assist, I'm getting out of my mind with this in the last two days...
Here the code of the function which is creating the PDF.
let pdfMetaData = [
kCGPDFContextCreator: "Your App Name",
kCGPDFContextAuthor: "Your Name",
kCGPDFContextTitle: "Allenamento"
]
let pageWidth = 8.5 * 72.0 // Standard letter size in points (8.5 x 11 inches)
let pageHeight = 11 * 72.0
let pageSize = CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight)
let margin: CGFloat = 20.0
let pdfData = NSMutableData()
guard let consumer = CGDataConsumer(data: pdfData as CFMutableData) else {
print("Errore nella creazione del consumer")
return
}
var mediaBox = pageSize
guard let pdfContext = CGContext(consumer: consumer, mediaBox: &mediaBox, pdfMetaData as CFDictionary) else {
print("Errore nella creazione del contesto PDF")
return
}
pdfContext.beginPDFPage(nil)
// Parte superiore della pagina
...
// Disegno del rettangolo "MATERIALE DA ALLENAMENTO"
...
// Disegno del rettangolo "ESERCITAZIONI ALLENAMENTO"
let exercisesRect = CGRect(x: margin, y: cellYPosition - 45, width: pageWidth - 2 * margin, height: 25)
pdfContext.setFillColor(NSColor.lightGray.cgColor)
pdfContext.fill(exercisesRect)
pdfContext.setStrokeColor(NSColor.black.cgColor)
pdfContext.stroke(exercisesRect)
let exercisesText = "ESERCITAZIONI ALLENAMENTO"
let exercisesAttributedString = NSAttributedString(string: exercisesText, attributes: boldAttributes)
let exercisesTextPosition = CGPoint(x: exercisesRect.midX - exercisesAttributedString.size().width / 2, y: exercisesRect.midY - 5)
drawText(attributedString: exercisesAttributedString, position: exercisesTextPosition, context: pdfContext)
// Iniziamo a disegnare le informazioni degli esercizi
let sortedExercises = training.trainingExercises.sorted { $0.order < $1.order }
var currentY = exercisesRect.minY - 30
for trainingExercise in sortedExercises {
// Gestione del wrap text e posizionamento su più pagine
let remainingHeight = currentY - margin
if remainingHeight < 100 {
pdfContext.endPDFPage()
pdfContext.beginPDFPage(nil)
currentY = pageHeight - margin
}
// Disegna la durata
let durationText = "\(trainingExercise.duration) min"
let durationAttributedString = NSAttributedString(string: durationText, attributes: boldAttributes)
drawText(attributedString: durationAttributedString, position: CGPoint(x: margin, y: currentY), context: pdfContext)
currentY -= 20
// Disegna la descrizione con wrap text
let descriptionText = trainingExercise.exercise.exerciseDescription
let descriptionAttributedString = NSAttributedString(string: descriptionText, attributes: regularAttributes)
let maxTextWidth = pageWidth - 2 * margin
let descriptionBoundingBox = descriptionAttributedString.boundingRect(with: CGSize(width: maxTextWidth, height: .greatestFiniteMagnitude), options: [.usesLineFragmentOrigin, .usesFontLeading])
if currentY - descriptionBoundingBox.height < margin {
pdfContext.endPDFPage()
pdfContext.beginPDFPage(nil)
currentY = pageHeight - margin
}
// Disegno della descrizione con supporto per il wrap text
let descriptionFramesetter = CTFramesetterCreateWithAttributedString(descriptionAttributedString)
let descriptionPath = CGPath(rect: CGRect(x: margin, y: currentY - descriptionBoundingBox.height, width: maxTextWidth, height: descriptionBoundingBox.height), transform: nil)
let descriptionFrame = CTFramesetterCreateFrame(descriptionFramesetter, CFRangeMake(0, 0), descriptionPath, nil)
CTFrameDraw(descriptionFrame, pdfContext)
currentY -= descriptionBoundingBox.height + 10
// Disegna l'immagine sotto la descrizione
if let imagePath = trainingExercise.exercise.imagePath, let image = NSImage(contentsOfFile: imagePath) {
let imageCG = image.cgImage(forProposedRect: nil, context: nil, hints: nil)
let imageAspect = CGFloat(imageCG!.width) / CGFloat(imageCG!.height)
let targetWidth = min(maxTextWidth, CGFloat(400))
let targetHeight = targetWidth / imageAspect
if currentY - targetHeight < margin {
pdfContext.endPDFPage()
pdfContext.beginPDFPage(nil)
currentY = pageHeight - margin
}
let imageTargetRect = CGRect(x: margin, y: currentY - targetHeight, width: targetWidth, height: targetHeight)
pdfContext.draw(imageCG!, in: imageTargetRect)
currentY -= targetHeight + 10
}
// Disegna una linea sottile come separatore
pdfContext.setStrokeColor(NSColor.lightGray.cgColor)
pdfContext.setLineWidth(1.0)
let lineYPosition = currentY - 5
pdfContext.move(to: CGPoint(x: margin, y: lineYPosition))
pdfContext.addLine(to: CGPoint(x: pageWidth - margin, y: lineYPosition))
pdfContext.strokePath()
currentY -= 20 // Spazio tra esercizi
}
pdfContext.endPDFPage()
pdfContext.closePDF()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let fileName = "Allenamento_\(dateFormatter.string(from: training.date)).pdf"
if let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let fileURL = documentsURL.appendingPathComponent(fileName)
do {
...
}
} catch {
print("Errore durante il salvataggio del PDF: \(error)")
}
}
}
private func drawText(attributedString: NSAttributedString, position: CGPoint, context: CGContext) {
let line = CTLineCreateWithAttributedString(attributedString)
context.textPosition = position
CTLineDraw(line, context)
}
I’m using PDFPageOverlayViewProvider with pdfview. I want to control the visibility of the overlay view using a button. However, the view updates only when it disappears and reappears. I would like the changes to be reflected immediately. How can I achieve this?
struct PDFKitView: View {
let bookId: UUID
let bookTitle: String
@State private var currentPage = "1"
@State private var isSideTab = false
@State private var selectedNote: [SelectedNote] = []
var body: some View {
let pdfDocument = openPDF(at: bookId.uuidString)
ZStack(alignment: .trailing) {
HStack(spacing: 0) {
PDFKitRepresentableView(
bookId: bookId,
selectedNote: $selectedNote,
currentPage: $currentPage,
pdfDocument: pdfDocument
)
if isSideTab {
SideView()
.transition(.move(edge: .trailing))
.zIndex(1)
.frame(maxWidth: 260)
}
}
.padding(.top, 73)
}
.onAppear {
getAllNote(bookId: bookId)
}
.customNavigationBar(back: true) {
Text("\(currentPage)/\(pdfDocument.pageCount)")
.pretendard(.CaptionRegular)
.foregroundStyle(Color.Text.primary)
} TrailingView: {
Button(action: {
withAnimation {
isSideTab.toggle()
}
}) {
Image(systemName: SFSymbol.squareStack3dDownForwardFill.icon)
.sfPro(.IconMedium)
.foregroundStyle(Color.Text.primary)
}
}
}
@ViewBuilder
func SideView() -> some View {
VStack(spacing: 16) {
HStack(spacing: 4) {
Image(systemName: SFSymbol.squareStack3dDownForwardFill.icon)
.sfPro(.IconSmall)
.foregroundStyle(Color.Text.primary)
Text(Literals.sideTabTitle)
.pretendard(.P2Bold)
.foregroundStyle(Color.Text.primary)
Spacer()
}
.padding(16)
.background {
Color.Background.white
}
ScrollView {
ForEach($selectedNote, id: \.noteId) { note in
NoteCellView(note: note)
}
}
.background {
Color.Fill.white
}
}
.background {
Color.Background.blueGray
}
}
@ViewBuilder
func NoteCellView(note: Binding<SelectedNote>) -> some View {
HStack(alignment: .top, spacing: 16) {
Image(.writingNote)
.resizable()
.scaledToFit()
.frame(width: 42, height: 60)
VStack(alignment: .leading, spacing: 8) {
Text(note.wrappedValue.noteId == bookId.uuidString ? "345" : "123")
.foregroundStyle(Color.Text.secondary)
.padding(.horizontal, 8)
.background {
Rectangle()
.strokeBorder(Color.Layout.secondary)
}
Text(bookTitle)
.lineLimit(2)
Toggle("", isOn: note.selected)
.labelsHidden()
.tint(Color.Fill.activePrimary)
}
}
.padding(EdgeInsets(top: 20, leading: 16, bottom: 16, trailing: 16))
}
}
struct PDFKitRepresentableView: UIViewRepresentable {
let bookId: UUID
@Binding var selectedNote: [SelectedNote]
@Binding var currentPage: String
let pdfDocument: PDFDocument
let pdfView = PDFView()
let toolPicker = PKToolPicker()
func makeUIView(context: Context) -> PDFView {
pdfView.displayMode = .singlePageContinuous
pdfView.usePageViewController(false)
pdfView.displayDirection = .vertical
pdfView.pageOverlayViewProvider = context.coordinator
pdfView.autoScales = true
pdfDocument.delegate = context.coordinator
pdfView.document = pdfDocument
return pdfView
}
func updateUIView(_ uiView: PDFView, context: Context) {
if
let localNote = selectedNote.first(where: {$0.noteId == bookId.uuidString}),
!localNote.selected
{
toolPicker.setVisible(false, forFirstResponder: uiView)
} else {
toolPicker.setVisible(true, forFirstResponder: uiView)
}
uiView.becomeFirstResponder()
}
func makeCoordinator() -> CanvasProvider {
return CanvasProvider(parent: self)
}
}
final class CanvasProvider: NSObject, PDFPageOverlayViewProvider, PDFDocumentDelegate {
var localNotes = [PDFPage: PKCanvasView]()
var passNotes = [PDFPage: Image]()
let parent: PDFKitRepresentableView
init(parent: PDFKitRepresentableView) {
self.parent = parent
super.init()
getDrawingDatas(
bookId: parent.bookId.uuidString,
selectedNote: parent.selectedNote,
document: parent.pdfDocument
)
}
func pdfView(_ view: PDFView, overlayViewFor page: PDFPage) -> UIView? {
var coverView: PKCanvasView? = PKCanvasView()
if
let view = localNotes[page],
parent.selectedNote.first(where: { $0.noteId == parent.bookId.uuidString })?.selected ?? false
{
view.backgroundColor = .clear
view.isOpaque = true
view.drawingPolicy = .anyInput
view.delegate = self
parent.toolPicker.addObserver(view)
coverView = view
(page as? CanvasPDFPage)?.canvasView = view
} else {
coverView = nil
}
for subView in view.documentView?.subviews ?? [] {
if subView.theClassName == "PDFPageView" {
subView.isUserInteractionEnabled = true
}
}
return coverView
}
func pdfView(_ pdfView: PDFView, willDisplayOverlayView overlayView: UIView, for page: PDFPage) { }
func pdfView(_ pdfView: PDFView, willEndDisplayingOverlayView overlayView: UIView, for page: PDFPage) { }
}