我之前发布过 related question所以,但无济于事。然后我改变了我的方法并创建了尺寸完全相同的 PDF 等。所有这些 PDF 也只包含一页。
现在我想将这些单页 PDF 合并为一个多页 PDF。来自 here我想我已经知道创建多页 PDF 的步骤是什么了。
运行下面的代码后,将创建一个具有预期文件名的 PDF,但该 PDF 只有一页,而且完全是空白。
我在这里束手无策......请告诉我我做错了什么!是的,我相信某种循环会在这里起作用,但老实说,循环总是让我变得更好.... :(
任何帮助将不胜感激!
哦,我几乎不会 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)
}
最佳答案
正如上面的评论中提到的,我发布的代码很糟糕。现在我已经解决了 - 多页 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 !
关于ios - 从其他 PDF 创建多页 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36011518/