我之前在 SO 上发布了一个相关问题,但无济于事。然后我改变了我的方法并创建了所有尺寸完全相同的 PDF,等等。所有这些 PDF 也只包含一页。
现在我想将这些单页 PDF 合并为一个多页 PDF。从这里我想我已经知道创建多页 PDF 的步骤是什么。
运行下面的代码后,会创建一个具有预期文件名的 PDF,但该 PDF 仅包含一页,并且完全是空白的。
我在这里束手无策......请告诉我我做错了什么!是的,我相信某种循环在这里会起作用,但老实说,LOOPS 总是让我变得更好...... :(
任何帮助将不胜感激!
哦,我几乎不能 Swift - 请不要向我扔任何 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)
}