从其他PDF创建多页PDF [英] Create Multi-Page PDF from other PDFs
问题描述
I've previously posted a related question on SO, but to no avail. I then changed my approach and created PDFs which all have the exact same dimensions, etc. All these PDFs also consists of one page only.
现在,我想将这些单页PDF合并为一个多页PDF.来自此处我想我已经确定了创建多页PDF的步骤.
Now I would like to combine these single page PDFs into one multi-page PDF. From here I think I've figured what the steps are in creating the multi-page PDF.
运行下面的代码后,将创建具有所需文件名的PDF,但该PDF仅包含一页,并且完全空白.
After running the code below, a PDF with the expected file name is created, but the PDF consists of only one page, and it is entirely blank.
我在这里很机智...请告诉我我做错了!是的,我相信这里会有某种循环,但是老实说,LOOPS总是让我变得更好....:(
I'm at wits' end here... Please tell me what I'm doing wrong! Yes, I believe some sort of loop will work here but, to be honest, LOOPS have always gotten the better of me.... :(
任何帮助将不胜感激!
Any help will be greatly appreciated!
哦,我几乎无法使用Swift-请不要向我扔任何Obj-C! ;)
Oh, and I can barely Swift - please do not throw any Obj-C at me! ;)
这是我的代码:
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.
As mentioned in the comments above, the code I've posted was crap. Now I've got it sorted out - the multi-page PDF gets created.
我仍然必须处理我提到的那个循环,但是现在这对我有用(没有循环):
I still have to work on that loop I've mentioned, but for now this works for me (without a loop):
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)
}
它可能并不优雅,但可以确定地狱般奏效!
It might not be elegant, but it sure as hell works!
对我在此处找到的信息表示敬意!
Kudos to the information I found here!
这篇关于从其他PDF创建多页PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!