2017-09-29 49 views
7

Tôi đang cố thêm chú thích đánh dấu vào tài liệu bằng PDFKit trên iOS.Chú thích nổi bật PdfKit

let highlight = PDFAnnotation(bounds: selection.bounds(for: page), 
           forType: PDFAnnotationSubtype.highlight, 
           withProperties: nil) 

highlight.color = color 

page.addAnnotation(highlight) 
page.displaysAnnotations = true 

Khi thêm chúng bằng mã ở trên, chúng xuất hiện dưới dạng hai lớp có hình dạng khác nhau. Khi lưu chúng vào tệp PDF và mở lại, chúng hiển thị chính xác.

Trong this screen capture

enter image description here

Đỉnh và đáy nổi bật đã được thêm vào cùng một cách sử dụng đoạn cung cấp ở đây. Phần trên cùng đã được lưu vào tài liệu pdf và xuất hiện như mong đợi khi mở lại nó, phần dưới cùng vừa được thêm vào.

Có ai biết cách hiển thị chính xác không (ví dụ như phần trên cùng) mà không cần phải lưu và mở lại tệp?

+0

Chúng tôi có cùng vấn đề chính xác –

+0

Tôi đã đăng lỗi với Apple và tôi đề nghị bạn làm tương tự https://openradar.appspot.com/34784917. Bạn có thể đăng nhập một lỗi tại https://bugreport.apple.com –

+0

Tôi cũng đã mở một lỗi thông qua bugreport.apple.com, không có câu trả lời nào cho đến nay. – guco

Trả lời

1

Vì vậy, đây là lỗi đã biết trong 10.13. Có một cách giải quyết bằng cách di chuyển trang lập tức và sau đó trở lại điểm nhấn

Bạn có thể tạo điểm nhấn sử dụng mã này:

let page = self.pdfDocument?.page(at: 10) 
let bounds = CGRect(x: 85.8660965, y: 786.8891167, width: 298.41, height: 12.1485) 
let annotation = PDFAnnotation(bounds: bounds, forType: .highlight, withProperties: nil) 
annotation.color = NSColor.blue 
page?.addAnnotation(annotation) 

Sau đó, bạn cần phải di chuyển khỏi trang và trở lại điểm nhấn

func annotationScrollHack(page: PDFPage) { 
    guard let pdfDocument = self.pdfDocument else { return } 

    //When adding highlights to macOS 10.13 it seems like 2 highlights are added. 
    //If you scroll to a different page and back the "extra" highlight is removed 
    //This function scrolls to the first/last page in the book and then back to the current page 
    //rdar://34784917 
    let bookScrollView = self.pdfView.documentView?.enclosingScrollView 
    let currentVisibleRect = bookScrollView?.contentView.documentVisibleRect 
    if (0 ... 3).contains(pdfDocument.index(for: page)) { 
     if self.pdfView.canGoToLastPage { 
      self.pdfView.goToLastPage(self) 
     } 
    } else { 
     if self.pdfView.canGoToFirstPage { 
      self.pdfView.goToFirstPage(self) 
     } 
    } 

    if let currentVisibleRect = currentVisibleRect { 
     bookScrollView?.contentView.scroll(to: CGPoint(x: currentVisibleRect.origin.x, y: currentVisibleRect.origin.y)) 
    } 
} 

Phản hồi từ Apple:

Không có cách giải quyết nào khác ngoài “hack” mà họ mô tả: di chuyển đi sau đó quay lại là tùy chọn tốt nhất. Thay đổi hệ số thu phóng và hoàn nguyên nó có thể sửa chữa nó theo cùng một cách, nhưng không được bảo đảm.

+0

Điều này đã được sửa trong macOS 10.13.2 –

1

Tôi đã kết thúc với phương pháp hack này sẽ xem xét tất cả các trường hợp theo ý kiến ​​của tôi. Hy vọng rằng sẽ giúp đỡ.

private func hackScroll(to page: PDFPage) { 

    switch (pdfView.canGoToFirstPage(), pdfView.canGoToLastPage()) { 
    case (true, false): 
     pdfView.goToFirstPage(nil) 
     pdfView.go(to: page) 
    case (false, true): 
     pdfView.goToLastPage(nil) 
     pdfView.go(to: page) 
    case (false, false): 
     guard let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return } 
     let file = "temp.pdf" 

     /// Save to temp file 
     let fileURL = dir.appendingPathComponent(file) 
     pdfView.document?.write(to: fileURL) 

     /// Read from temp file 
     guard let document = PDFDocument(url: fileURL) else { return } 
     pdfView.document = document 
     pdfView.autoScales = true 
    default: 
     pdfView.goToFirstPage(nil) 
     pdfView.go(to: page) 
    } 
} 
Các vấn đề liên quan