我以前在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)
}
嗯,事實證明,上面的代碼完成任何事情,但重寫原始單頁PDF - 空白頁!我真的需要一些幫助! –