2016-03-15 80 views
0

我以前在SO上發佈過related question,但無濟於事。然後我改變了我的方法並創建了所有具有完全相同尺寸的PDF,等等。所有這些PDF也只包含一個頁面。從其他PDF創建多頁PDF

現在我想將這些單頁PDF合併爲一個多頁PDF。從here我想我已經想出了創建多頁PDF的步驟。

運行下面的代碼後,會創建一個帶有預期文件名的PDF,但PDF僅由一個頁面組成,並且它完全是空白的。

我在智慧的盡頭......請告訴我我做錯了什麼!是的,我相信某種循環將在這裏工作,但說實話,循環一直得到我的好.... :(

任何幫助將不勝感激!

哦,我!可以勉強斯威夫特 - 請不要朝我扔任何的OBJ-C)

這裏是我的代碼:

func CreateCombinedPDF() { 

    let pdf01 = String("\(newDetailsLogIdentifier)_PAGE01.pdf") 
    let pdf02 = String("\(newDetailsLogIdentifier)_PAGE02.pdf") 

    //STEPS IN CREATING A COMBINED PDF 

    // 1. CGPDFDocumentCreateWithURL 
    // 2. CGContextBeginPage 
    // 3. CGPDFDocumentGetPage 
    // 4. CGPDFContextCreateWithURL 
    // 5. CGContextDrawPDFPage 
    // 6. CGContextEndPage 
    // 7. CGPDFContextClose 

    var mediaBox:CGRect = CGRectMake(0, 0, 820, 1170) 
    let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] 
    let combinedDocumentFileName = documentsURL.URLByAppendingPathComponent("\(newDetailsLogIdentifier)_COMBINED.pdf") 
    let fullPathCombinedDocument = combinedDocumentFileName.path! 
    let myCombinedDocumentURL = NSURL(fileURLWithPath: fullPathCombinedDocument) 
    let myContextCombinedDocument = CGPDFContextCreateWithURL(myCombinedDocumentURL, &mediaBox, nil) 

    let fileNamePDF01 = documentsURL.URLByAppendingPathComponent(pdf01) 
    let fullPathPDF01 = fileNamePDF01.path! 
    let urlPDF01 = NSURL(fileURLWithPath: fullPathPDF01) 
    let myContextPDF01 = CGPDFContextCreateWithURL(urlPDF01, &mediaBox, nil) 
    CGPDFContextBeginPage(myContextPDF01, nil) 
    //Here's my problem - I think... 
    CGContextDrawPDFPage(myContextPDF01, nil) 
    CGPDFContextEndPage(myContextPDF01) 

    let fileNamePDF02 = documentsURL.URLByAppendingPathComponent(pdf02) 
    let fullPathPDF02 = fileNamePDF02.path! 
    let urlPDF02 = NSURL(fileURLWithPath: fullPathPDF02) 
    let myContextPDF02 = CGPDFContextCreateWithURL(urlPDF02, &mediaBox, nil) 
    CGPDFContextBeginPage(myContextPDF02, nil) 
    //Here's my problem - I think... 
    CGContextDrawPDFPage(myContextPDF02, nil) 
    CGPDFContextEndPage(myContextPDF02) 

    CGPDFContextClose(myContextCombinedDocument) 

} 
+0

嗯,事實證明,上面的代碼完成任何事情,但重寫原始單頁PDF - 空白頁!我真的需要一些幫助! –

回答

1

如上評論中提到,我已經發布的代碼是廢話。現在我已經把它整理出來了 - 創建了多頁PDF。

我還是要對循環我所提到的工作,但現在這對我的作品(沒有環):

func CreateCombinedPDF() { 

    //Set all constants and variables needed 
    var mediaBox:CGRect = CGRectMake(0, 0, 820, 1170) 
    let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] 
    let combinedDocumentFileName = documentsURL.URLByAppendingPathComponent("\(newDetailsLogIdentifier)_COMBINED.pdf") 
    let fullPathCombinedDocument = combinedDocumentFileName.path! 
    let myCombinedDocumentURL = NSURL(fileURLWithPath: fullPathCombinedDocument) 

    let pdf01 = String("\(newDetailsLogIdentifier)_PAGE01.pdf") 
    let fileNamePDF01 = documentsURL.URLByAppendingPathComponent(pdf01) 
    let fullPathPDF01 = fileNamePDF01.path! 
    let urlPDF01 = NSURL(fileURLWithPath: fullPathPDF01) 
    let contextPDF01 = CGPDFDocumentCreateWithURL(urlPDF01) 
    let pdf01Page = CGPDFDocumentGetPage(contextPDF01,1) 

    let pdf02 = String("\(newDetailsLogIdentifier)_PAGE02.pdf") 
    let fileNamePDF02 = documentsURL.URLByAppendingPathComponent(pdf02) 
    let fullPathPDF02 = fileNamePDF02.path! 
    let urlPDF02 = NSURL(fileURLWithPath: fullPathPDF02) 
    let contextPDF02 = CGPDFDocumentCreateWithURL(urlPDF02) 
    let pdf02Page = CGPDFDocumentGetPage(contextPDF02,1) 

    // 1. Create the PDF context that will become the new PDF file 
    let myContextCombinedDocument = CGPDFContextCreateWithURL(myCombinedDocumentURL, &mediaBox, nil) 

    // 2. Insert pages 

    //Draw PAGE01.pdf 
    CGPDFContextBeginPage(myContextCombinedDocument, nil); 
    CGContextDrawPDFPage(myContextCombinedDocument, pdf01Page) 
    CGPDFContextEndPage(myContextCombinedDocument) 

    //Draw PAGE02.pdf 
    CGPDFContextBeginPage(myContextCombinedDocument, nil); 
    CGContextDrawPDFPage(myContextCombinedDocument, pdf02Page) 
    CGPDFContextEndPage(myContextCombinedDocument) 

    // 3. All pages inserted. Now close and save the new document. 
    CGPDFContextClose(myContextCombinedDocument) 
} 

它可能不是優雅,但它肯定是地獄的作品!

對我發現的信息的榮譽here

0

上面的Swift代碼不適合我,所以我轉換到Objective-C爲有興趣的人。

+(void) CreateCombinedPDF { 
    NSString *newDetailsLogIdentifier = @"filename"; 

    // Set all constants and variables needed 
    CGRect mediaBox = CGRectMake(0, 0, 820, 1170); 
    NSFileManager *fm = [NSFileManager defaultManager]; 
    NSURL *documentsURL = [fm URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0]; 
    NSURL *combinedDocumentFileName = [documentsURL URLByAppendingPathComponent:[NSString stringWithFormat:@"%@_COMBINED.pdf", newDetailsLogIdentifier]]; 
    NSString *fullPathCombinedDocument = combinedDocumentFileName.path; 
    NSURL *myCombinedDocumentURL = [NSURL fileURLWithPath:fullPathCombinedDocument]; 

    NSString *pdf01 = [NSString stringWithFormat:@"%@_PAGE01.pdf", newDetailsLogIdentifier]; 
    NSURL *fileNamePdf01 = [documentsURL URLByAppendingPathComponent:pdf01]; 
    NSString *fullPathPdf01 = fileNamePdf01.path; 
    NSURL *urlPDF01 = [NSURL fileURLWithPath:fullPathPdf01]; 
    CFURLRef cfurl = CFBridgingRetain(urlPDF01); 
    CGPDFDocumentRef contextPDF01 = CGPDFDocumentCreateWithURL(cfurl); 
    CGPDFPageRef pdf01Page = CGPDFDocumentGetPage(contextPDF01, 1); 

    NSString *pdf02 = [NSString stringWithFormat:@"%@_PAGE02.pdf", newDetailsLogIdentifier]; 
    NSURL *fileNamePdf02 = [documentsURL URLByAppendingPathComponent:pdf02]; 
    NSString *fullPathPdf02 = fileNamePdf02.path; 
    NSURL *urlPDF02 = [NSURL fileURLWithPath:fullPathPdf02]; 
    CFURLRef cfurl2 = CFBridgingRetain(urlPDF02); 
    CGPDFDocumentRef contextPDF02 = CGPDFDocumentCreateWithURL(cfurl2); 
    CGPDFPageRef pdf02Page = CGPDFDocumentGetPage(contextPDF02, 1); 

    // 1. Create the PDF context that will become the new PDF file 
    CGContextRef myContextCombinedDocument = CGPDFContextCreateWithURL(CFBridgingRetain(myCombinedDocumentURL), &mediaBox, nil); 

    // 2. Insert pages 

    // Draw PAGE01.pdf 
    CGPDFContextBeginPage(myContextCombinedDocument, nil); 
    CGContextDrawPDFPage(myContextCombinedDocument, pdf01Page); 
    CGPDFContextEndPage(myContextCombinedDocument); 

    // Draw PAGE02.pdf 
    CGPDFContextBeginPage(myContextCombinedDocument, nil); 
    CGContextDrawPDFPage(myContextCombinedDocument, pdf02Page); 
    CGPDFContextEndPage(myContextCombinedDocument); 

    // 3. All pages inserted. Now close and save the new document. 
    CGPDFContextClose(myContextCombinedDocument); 
}