Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

objective c - How to save created PDF in document folder and merge in iOS

Updated I am able to create single PDF page of photo captured with comment in iPHone. On button click I am generating one single PDF page every time and I want those PDF page in single PDF bunch. I am not able to merge the single PDF files in to bunch.

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

I have followed the above URL code. Could you suggest some logic here. Thanks in advance.

*Edit in code * can you check below code.

- (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();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I managed to achieve this by following code with some complex logic :)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...