0

我正在尝试获取 PDF 文档的方向属性。目的是,我想在取决于 PDF 文档方向的位置添加一个按钮小部件。

例如:

     func openPDFDocument() {
            if let documentURL = Bundle.main.url(forResource: "PDF document", withExtension: "pdf"),
               let document = PDFDocument(url: documentURL),
               let page = document.page(at: 0) {
                // Set our document to the view, center it, and set a background color
                pdfView?.document = document
                pdfView?.autoScales = true
                pdfView?.backgroundColor = UIColor.lightGray
                
              //I think I should be able to add a code here like:
              if page.orientation = Horizontal {
                self.insertResetButtonInto(page)
                } else {
//do nothing or do something else
                }     
        }
    }

如果文档处于横向模式,这是我想添加的功能:

  func insertResetButtonInto(_ page: PDFPage) {

        let pageBounds = page.bounds(for: .cropBox)

        let resetButtonBounds = CGRect(x: 90, y: pageBounds.size.height - 300, width: 106, height: 32)
        let resetButton = PDFAnnotation(bounds: resetButtonBounds, forType: PDFAnnotationSubtype(rawValue: PDFAnnotationSubtype.widget.rawValue), withProperties: nil)
        resetButton.widgetFieldType = PDFAnnotationWidgetSubtype(rawValue: PDFAnnotationWidgetSubtype.button.rawValue)
        resetButton.widgetControlType = .pushButtonControl
        resetButton.caption = "Reset"
        page.addAnnotation(resetButton)
        // Create PDFActionResetForm action to clear form fields.
        let resetFormAction = PDFActionResetForm()         
        resetFormAction.fieldsIncludedAreCleared = false
        resetButton.action = resetFormAction
  
    }

我从Apple 的文档网站获得了示例项目。我查看了以前的类似问题,但似乎这是在 Objective C 中。

我将不胜感激在这件事上的帮助。

4

1 回答 1

1

没有直接的 API 可以orientationPDFPage. 但是您可以先从 获取页面大小.mediaBox,然后计算如下所示的方向。

    let pageSize = page.bounds(for: .mediaBox).size
    
    if pageSize.width > pageSize.height {
        //landscape
    } else {
        //portrait
    }
于 2021-09-24T17:04:09.933 回答