ios - 如何将创建的 PDF 保存在文档文件夹中并在 iOS 中合并

标签 ios objective-c pdf-generation nsdocument

已更新 我可以在 iPhone 中创建带有评论的照片的单个 PDF 页面。单击按钮时,我每次都会生成一个 PDF 页面,并且我希望这些 PDF 页面位于单个 PDF 束中。我无法将单个 PDF 文件合并为一堆。

http://mobile.tutsplus.com/tutorials/iphone/generating-pdf-documents/?search_index=3

我已经按照上面的URL代码进行操作了。你能在这里提出一些逻辑吗?提前致谢。

*在代码中编辑 *您可以检查下面的代码吗?

- (IBAction)didClickOpenPDF {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf",myPDFName]];

if([[NSFileManager defaultManager] fileExistsAtPath:pdfPath]) {
    ReaderDocument *document = [ReaderDocument withDocumentFilePath:pdfPath password:nil];
    if (document != nil)
    {
        ReaderViewController *readerViewController = [[ReaderViewController alloc] initWithReaderDocument:document];
        readerViewController.delegate = self;
        readerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        readerViewController.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentModalViewController:readerViewController animated:YES];
    }
 }
}

- (IBAction)didClickMakePDF {

            [self setupPDFDocumentNamed:[NSString stringWithFormat:@"%@",myPDFName] Width:850 Height:1100];


    [self beginPDFPage];
 CGRect textRect = [self addText:question.text
                      withFrame:CGRectMake(kPadding, kPadding, 400, 200) fontSize:48.0f];

 // dynamic image captured by camera,comment text, lines are added here

  [self finishPDF];

}

 - (void)setupPDFDocumentNamed:(NSString*)name Width:(float)width Height:(float)height {
  _pageSize = CGSizeMake(width, height);

 NSString *myPDFName = [NSString stringWithFormat:@"%@.pdf", name];
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:myPDFName];

 UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil);
int count = [gotIndexString integerValue];

for (int pageNumber = 2; pageNumber <= count; pageNumber++)
{
    //Open a pdf page context
    UIGraphicsBeginPDFPageWithInfo(CGRectZero, nil);

    //Get graphics context to draw the page
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    //Flip and scale context to draw the pdf correctly
    CGContextTranslateCTM(currentContext, 0, CGRectZero.size.height);
    CGContextScaleCTM(currentContext, 1.0, -1.0);
    NSURL *newUrl = [NSURL URLWithString:pdfPath];
    NSLog(@" setupPDFDocumentNamed newUrl for loop %@ ",newUrl);

    //Get document access of the pdf from which you want a page
    CGPDFDocumentRef newDocument = CGPDFDocumentCreateWithURL ((__bridge_retained CFURLRef) newUrl);
    NSLog(@" setupPDFDocumentNamed newDocument for loop %@ ",newDocument);

    //Get the page you want
    CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, 1);
    NSLog(@" setupPDFDocumentNamed newPage for loop %@ ",newPage);

    //Drawing the page
    CGContextDrawPDFPage (currentContext, newPage);
    NSLog(@"CGContextRef context %@ ",currentContext);
    //Clean up
    newPage = nil;
    CGPDFDocumentRelease(newDocument);
    newDocument = nil;
    newUrl = nil;


}

}

- (void)beginPDFPage {

UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, _pageSize.width, _pageSize.height), nil);
}

- (void)finishPDF {
UIGraphicsEndPDFContext();
}

最佳答案

我设法通过遵循具有一些复杂逻辑的代码来实现这一目标:)

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];
// File paths
NSString *pdfPath1 = [documentsDirectory stringByAppendingPathComponent:@"temp1.pdf"];
NSString *pdfPath2 = [documentsDirectory stringByAppendingPathComponent:@"temp2.pdf"];
NSString *pdfPathOutput = [documentsDirectory stringByAppendingPathComponent:@"out.pdf"];

// File URLs - bridge casting for ARC
CFURLRef pdfURL1 = (__bridge_retained CFURLRef)[[NSURL alloc] initFileURLWithPath: (NSString *)pdfPath1];//(CFURLRef) NSURL
CFURLRef pdfURL2 = (__bridge_retained CFURLRef)[[NSURL alloc] initFileURLWithPath: (NSString *)pdfPath2];//(CFURLRef)
CFURLRef pdfURLOutput =(__bridge_retained CFURLRef) [[NSURL alloc] initFileURLWithPath:  (NSString *)pdfPathOutput];//(CFURLRef)

// File references
CGPDFDocumentRef pdfRef1 = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL1);
CGPDFDocumentRef pdfRef2 = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL2);

// Number of pages
NSInteger numberOfPages1 = CGPDFDocumentGetNumberOfPages(pdfRef1); 
NSInteger numberOfPages2 = CGPDFDocumentGetNumberOfPages(pdfRef2);

// Create the output context
 CGContextRef writeContext = CGPDFContextCreateWithURL(pdfURLOutput, NULL, NULL);

// Loop variables
 CGPDFPageRef page;
 CGRect mediaBox;

// Read the first PDF and generate the output pages
NSLog(@"GENERATING PAGES FROM PDF 1 (%i)...", numberOfPages1);
for (int i=1; i<=numberOfPages1; i++) {
 page = CGPDFDocumentGetPage(pdfRef1, i);
 mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
 CGContextBeginPage(writeContext, &mediaBox);
 CGContextDrawPDFPage(writeContext, page);
 CGContextEndPage(writeContext);
 }

 // Read the second PDF and generate the output pages
 NSLog(@"GENERATING PAGES FROM PDF 2 (%i)...", numberOfPages2);
 for (int i=1; i<=numberOfPages2; i++) {
 page = CGPDFDocumentGetPage(pdfRef2, i);
 mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
 CGContextBeginPage(writeContext, &mediaBox);
 CGContextDrawPDFPage(writeContext, page);
 CGContextEndPage(writeContext);      
 }
NSLog(@"DONE!");

 // Finalize the output file
 CGPDFContextClose(writeContext);

// Release from memory
CFRelease(pdfURL1);
CFRelease(pdfURL2);
CFRelease(pdfURLOutput);
CGPDFDocumentRelease(pdfRef1);
CGPDFDocumentRelease(pdfRef2);
CGContextRelease(writeContext);

关于ios - 如何将创建的 PDF 保存在文档文件夹中并在 iOS 中合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16646039/

相关文章:

ios - 快速检索实时相机预览的最后一帧

iphone - RequestFailed 方法在 popViewControllerAnimated :YES 后抛出错误

ios - KVO addObserver 到CoreData对象的一对多关系

iphone - UITableVIew reloadSections :withAnimation:withoutHassle

ios - Swift 每次内存地址跳转都会崩溃

ios - 如何创建一个 UITableView 在 objective-c 的一行中加载 2 个自定义单元格?

vb.net - iTextSharp 对不同的页面应用不同的边距

java - 使用 Java 从 PDF 模板动态生成 PDF,无格式问题

ios - 当我使用自定义图标时,为什么谷歌地图 ios 的信息窗口不显示?

asp-classic - 从托管的ASP经典版将HTML转换为PDF?