如何以编程方式将文本粘贴到iOS中的PDF文件
今天我搜索了一段时间,寻找一种在iOS7应用程序中以编程方式将文本粘贴到PDF文件中的方法。不幸的是,没有简单的方法来编辑PDF表单。你可以使用付费图书馆,但那不是我的选择。所以我通过使用CGPDF在指定的坐标处粘贴文本来实现。 我就是这样做的:如何以编程方式将文本粘贴到iOS中的PDF文件,ios,objective-c,pdf,edit,Ios,Objective C,Pdf,Edit,今天我搜索了一段时间,寻找一种在iOS7应用程序中以编程方式将文本粘贴到PDF文件中的方法。不幸的是,没有简单的方法来编辑PDF表单。你可以使用付费图书馆,但那不是我的选择。所以我通过使用CGPDF在指定的坐标处粘贴文本来实现。 我就是这样做的: -(void)editPDF { NSString*fileName=@“new.pdf”; NSArray*阵列路径= NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUser
-(void)editPDF
{
NSString*fileName=@“new.pdf”;
NSArray*阵列路径=
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
是的);
NSString*path=[ArrayPath对象索引:0];
NSString*pdfFileName=[path stringByAppendingPathComponent:fileName];
NSString*templatePath=[[NSBundle mainBundle]pathForResource:@“mytemplate”类型:@“pdf”];
//创建空的pdf文件;
UIGraphicsBeginPDFContextToFile(pdfFileName,CGRectMake(0,0792612),nil);
CFURLRef url=CFURLCreateWithFileSystemPath(NULL,(CFStringRef)templatePath,kCFURLPOSIXPathStyle,0);
//打开模板文件
CGPDFDocumentRef templateDocument=CGPDFDocumentCreateWithURL(url);
CFRelease(url);
//获取模板中的页数
大小\u t计数=CGPDFDocumentGetNumberOfPages(templateDocument);
//对于模板中的每个页面
用于(大小\u t页码=1;页码
- (void)editPDF
{
NSString* fileName = @"new.pdf";
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
NSString *templatePath = [[NSBundle mainBundle] pathForResource:@"mytemplate" ofType:@"pdf"];
//create empty pdf file;
UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectMake(0, 0, 792, 612), nil);
CFURLRef url = CFURLCreateWithFileSystemPath (NULL, (CFStringRef)templatePath, kCFURLPOSIXPathStyle, 0);
//open template file
CGPDFDocumentRef templateDocument = CGPDFDocumentCreateWithURL(url);
CFRelease(url);
//get amount of pages in template
size_t count = CGPDFDocumentGetNumberOfPages(templateDocument);
//for each page in template
for (size_t pageNumber = 1; pageNumber <= count; pageNumber++) {
//get bounds of template page
CGPDFPageRef templatePage = CGPDFDocumentGetPage(templateDocument, pageNumber);
CGRect templatePageBounds = CGPDFPageGetBoxRect(templatePage, kCGPDFCropBox);
//create empty page with corresponding bounds in new document
UIGraphicsBeginPDFPageWithInfo(templatePageBounds, nil);
CGContextRef context = UIGraphicsGetCurrentContext();
//flip context due to different origins
CGContextTranslateCTM(context, 0.0, templatePageBounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
//copy content of template page on the corresponding page in new file
CGContextDrawPDFPage(context, templatePage);
//flip context back
CGContextTranslateCTM(context, 0.0, templatePageBounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
//create dictionary for font
UIFont *font = [UIFont fontWithName: @"Courier" size:12];
NSDictionary *attribdict = [[NSDictionary alloc] initWithObjectsAndKeys: font, NSFontAttributeName, nil];
/* Here you can do any drawings */
[@"Test" drawAtPoint:CGPointMake(200, 300) withAttributes:attribdict];
}
CGPDFDocumentRelease(templateDocument);
UIGraphicsEndPDFContext();
[self showPDFFile];
}
-(void)showPDFFile
{
NSString* fileName = @"new.pdf";
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
NSURL *url = [NSURL fileURLWithPath:pdfFileName];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[webView loadRequest:request];
[self.view addSubview:webView];
}