【问题标题】:Load pdf using PDF Kit in view only mode在仅查看模式下使用 PDF Kit 加载 pdf
【发布时间】:2018-05-02 07:28:53
【问题描述】:

我正在使用以下简单代码来加载 pdf 表单,但我不希望它以可编辑的形式加载。我没有找到任何限制它以仅查看模式加载的内容。

NSData *fileData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"PdfFormExample" ofType:@"pdf"]];
PDFDocument *pdfDocument = [[PDFDocument alloc] initWithData:fileData];
PDFView *pdfView = [[PDFView alloc] initWithFrame:self.view.frame];
pdfView.document = pdfDocument
[self.view addSubView:pdfView];

谢谢

【问题讨论】:

  • userInteractionEnabled 可能是一个解决方案
  • @MuhammadZohaibEhsan 这将禁用许多功能,如缩放、单击链接等。我试过了,但我最终遇到了这些问题。
  • 说得好。请注意您的荣誉:)

标签: ios objective-c swift ios11 pdfkit


【解决方案1】:

我认为您想禁用 LongPressGestureRecognizer。像这样的:

    private static void DisableDefaultLongPressGestureRecognizer(PdfKit.PdfView oPdfView)
    {
        oPdfView
            .GestureRecognizers
            .Where(x => x is UILongPressGestureRecognizer)
            .ForEach((oLongPressGestureRecognizer, nIndex) =>
            {
                oLongPressGestureRecognizer.Enabled = false;
            });
    }

【讨论】:

    【解决方案2】:

    我可以通过在呈现文档之前在文档中的其他注释上添加一个空白注释来做到这一点。我为 PDF 文档添加了一个 ma​​keReadOnly() 扩展,它对所有注释执行此操作,以使整个文档为只读。

    这样做的好处是所有其他 PDF 查看功能仍然可以正常工作。这是斯威夫特代码。您可以使用 Objective-C 类别做类似的事情:

    // A blank annotation that does nothing except serve to block user input
    class BlockInputAnnotation: PDFAnnotation {
    
        init(forBounds bounds: CGRect, withProperties properties: [AnyHashable : Any]?) {
            super.init(bounds: bounds, forType: PDFAnnotationSubtype.stamp,  withProperties: properties)
            self.fieldName = "blockInput"
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        override func draw(with box: PDFDisplayBox, in context: CGContext)   {
        }
    }
    
    
    extension PDFDocument {
        func makeReadOnly() {
            for pageNumber in 0..<self.pageCount {
                guard let page = self.page(at: pageNumber) else {
                    continue
                }
                for annotation in page.annotations {
                    annotation.isReadOnly = true // This _should_ be enough, but PDFKit doesn't recognize the isReadOnly attribute
                    // So we add a blank annotation on top of the annotation, and it will capture touch/mouse events
                    let blockAnnotation = BlockInputAnnotation(forBounds: annotation.bounds, withProperties: nil)
                    blockAnnotation.isReadOnly = true
                    page.addAnnotation(blockAnnotation)
                }
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-08
      • 2014-09-05
      相关资源
      最近更新 更多