我正在研究基于文档的应用程序。我将在UIWebView中加载PDF。根据我的要求,我必须在我的触摸/点击点添加图像。我在UIWebview中找到了触摸点(CGPoint)。但是我希望获得以PDF格式加载的PDF的相同接触点。
详细地说,UIWebView以不同的大小(小尺寸)显示PDF文档的内容。因此UIWebView的接触点(CGPoint)将与PDF不同。
由于页面清晰度降低,我没有将UIWebView的内容呈现为PDF。我将直接呈现PDF的内容。
下面的代码将解释我如何呈现PDF。
PDF渲染代码:
NSString *tessdataPath = [[NSBundle mainBundle] pathForResource:@"input" ofType:@"pdf"];
NSURL *url = [NSURL fileURLWithPath:tessdataPath];
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)url);
const size_t numberOfPages = CGPDFDocumentGetNumberOfPages(pdf);
NSMutableData* data = [NSMutableData data];
UIGraphicsBeginPDFContextToData(data, CGRectZero, nil);
for(size_t page = 1; page <= numberOfPages; page++)
{
// Get the current page and page frame
CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdf, page);
const CGRect pageFrame = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);
UIGraphicsBeginPDFPageWithInfo(pageFrame, nil);
// Draw the page (flipped)
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -pageFrame.size.height);
CGContextDrawPDFPage(ctx, pdfPage);
CGContextRestoreGState(ctx);
if(page == 1)
{
for (UIView *subview in [self.pdfView.scrollView subviews]) {
if([subview isKindOfClass:[UIImageView class]])
{
UIImageView *img = (UIImageView*)subview;
[subview removeFromSuperview];
CGPoint point1 = CGPointMake(img.frame.origin.x, img.frame.origin.y);
[self drawCustomPDFContent:img.image point1:point1];
}
}
}
}
UIGraphicsEndPDFContext();
CGPDFDocumentRelease(pdf);
pdf = nil;
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString *filePath = [documentDirectory stringByAppendingPathComponent:@"output.pdf"];
//bool fileCopied = [[NSFileManager defaultManager]copyItemAtURL:url toURL:toUrl error:nil];
[data writeToFile:filePath atomically:YES];
在此,我使用第1页中的图像渲染输入pdf。使用input.pdf成功渲染了图像。但我需要在pdf的确切位置添加图像。有关触点差异,请参见上图。任何人都可以提供解决方案来找到PDF中的确切接触点吗?