Загрузка PDF с использованием PDF Kit в режиме только для просмотра - PullRequest
0 голосов
/ 02 мая 2018

Я использую следующий простой код для загрузки 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];

Спасибо

Ответы [ 2 ]

0 голосов
/ 22 декабря 2018

Мне удалось сделать это, добавив пустую аннотацию поверх других аннотаций в документе перед его представлением. Я добавил расширение makeReadOnly () к документу PDF, которое делает это для всех аннотаций, чтобы сделать весь документ только для чтения.

Преимущество этого заключается в том, что все остальные функции просмотра PDF по-прежнему работают нормально. Вот код Swift. Вы можете сделать нечто подобное с категорией 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)
            }
        }

    }
}
0 голосов
/ 29 августа 2018

Я думаю, что вы хотите отключить LongPressGestureRecognizer. Примерно так:

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