选择PDF文件中的文本(iphone)

时间:2021-09-21 10:44:41

I'am working in a PDF reader for iphone and I want to learn how to Select a text in PDF, I was able to parse a PDF Document(using CGPDFContentStreamRef and CGPDFScannerRef), and to search for words in the pdf file, but i can't highlight the word in the PDF Document. So, how can I locate a word in the PDF View and How to select it?

我在iphone的PDF阅读器中工作,我想学习如何选择PDF格式的文本,我能够解析PDF文档(使用CGPDFContentStreamRef和CGPDFScannerRef),并在PDF文件中搜索单词,但是我不能在PDF文档中突出这个单词。那么,如何在PDF视图中找到一个单词并选择它呢?

2 个解决方案

#1


2  

You can try PDFKitten. There is some code in it.

你可以试试PDFKitten。里面有一些代码。

- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    CGPDFDocumentRef doc = CGPDFDocumentRetain(self.document);
    CGPDFPageRef page = CGPDFDocumentGetPage(doc, 1);

    // Transform the CTM compensating for flipped coordinate system
    CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);

    // Draw PDF (scaled to fit)
    CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, layer.bounds, 0, YES));
    CGContextDrawPDFPage(ctx, page);

    for (Selection *s in self.selections)
    {
        CGContextSaveGState(ctx);

        CGContextConcatCTM(ctx, [s transform]);
        CGContextSetFillColorWithColor(ctx, [[UIColor yellowColor] CGColor]);
        CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
        CGContextFillRect(ctx, [s frame]);

        CGContextRestoreGState(ctx);
    }

    CGPDFDocumentRelease(doc); doc = nil;
}

#2


0  

Have you tried FastPDFKit?

你有试过FastPDFKit吗?

https://github.com/mobfarm/FastPdfKit

https://github.com/mobfarm/FastPdfKit

It will highlight the search results, but won't let you select text by touching.

它会突出显示搜索结果,但不会让你通过触摸来选择文本。

#1


2  

You can try PDFKitten. There is some code in it.

你可以试试PDFKitten。里面有一些代码。

- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    CGPDFDocumentRef doc = CGPDFDocumentRetain(self.document);
    CGPDFPageRef page = CGPDFDocumentGetPage(doc, 1);

    // Transform the CTM compensating for flipped coordinate system
    CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);

    // Draw PDF (scaled to fit)
    CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, layer.bounds, 0, YES));
    CGContextDrawPDFPage(ctx, page);

    for (Selection *s in self.selections)
    {
        CGContextSaveGState(ctx);

        CGContextConcatCTM(ctx, [s transform]);
        CGContextSetFillColorWithColor(ctx, [[UIColor yellowColor] CGColor]);
        CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
        CGContextFillRect(ctx, [s frame]);

        CGContextRestoreGState(ctx);
    }

    CGPDFDocumentRelease(doc); doc = nil;
}

#2


0  

Have you tried FastPDFKit?

你有试过FastPDFKit吗?

https://github.com/mobfarm/FastPdfKit

https://github.com/mobfarm/FastPdfKit

It will highlight the search results, but won't let you select text by touching.

它会突出显示搜索结果,但不会让你通过触摸来选择文本。