Pergunta

I'm making an application to scan multiple page pdf files. I have a PDFView and a PDFThumbnailView that are linked. The first time a scan is completed, I create a new PDFDocument and set it to PDFView. Then whenever another scan is completed I add a PDFPage to [pdfView document].

Now the problem is whenever a page is added, neither the PDFView or PDFThumbnailView update to show the new document with the extra page. That is until I zoom in or out, then they both update to show the document with the new page.

The temporary solution I have now (zoom in and then autoscale) is certainly not the best one. Take for example when you have already zoomed in on the document, and you scan a new page, the view will then autoscale. I tried [pdfView setNeedsDisplay:YES] before but that doesn't seem to work.

This is the method where the scan arrives as NSData:

- (void)scannerDevice:(ICScannerDevice *)scanner didScanToURL:(NSURL *)url data:(NSData *)data {
    //Hide the progress bar
    [progressIndicator stopAnimation:nil];

    //Create a pdf page from the data
    NSImage *image = [[NSImage alloc] initWithData:data];
    PDFPage *page = [[PDFPage alloc] initWithImage:image];

    //If the pdf view has a document
    if ([pdfView document]) {
        //Set the page number and add it to the document
        [page setValue:[NSString stringWithFormat:@"%d", [[pdfView document] pageCount] + 1] forKey:@"label"];
        [[pdfView document] insertPage:page atIndex:[[pdfView document] pageCount]];
    } else {
        //Create a new document and add the page
        [page setValue:@"1" forKey:@"label"];
        PDFDocument *document = [[PDFDocument alloc] init];
        [document insertPage:page atIndex:0];
        [pdfView setDocument:document];
    }

    //Force a redraw for the pdf view so the pages are shown properly
    [pdfView zoomIn:self];
    [pdfView setAutoScales:YES];
}

Does anyone know of a way where I can add a PDFPage and have the PDFView update without messing with the zoom state of the PDFView?

Foi útil?

Solução

You need to manually call:

- (void)layoutDocumentView

I imagine the reason that this is not called manually is to allow coalescing of multiple changes into one update.

This is documented:

The PDFView actually contains several subviews, such as the document view (where the PDF is actually drawn) and a “matte view” (which may appear as a gray area around the PDF content, depending on the scaling). Changes to the PDF content may require changes to these inner views, so you must call this method explicitly if you use PDF Kit utility classes to add or remove a page, rotate a page, or perform other operations affecting visible layout.

This method is called automatically from PDFView methods that affect the visible layout (such as setDocument:, setDisplayBox: or zoomIn:).

Outras dicas

The best solution I got so far to refresh manually the PDFView with annotation was to move the PDFView to another page and come back to the page you need to refresh because:

- (void)layoutDocumentView didn't work for me in my case.

Then:

[self.pdfView goToLastPage:nil];
[self.pdfView goToPage:destinationPage];
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top