从其他PDF创建多页PDF

时间:2016-03-15 12:34:30

标签: ios swift xcode7 cgpdf

我之前在SO上发布过related question,但无济于事。然后我改变了我的方法并创建了所有具有完全相同尺寸的PDF等。所有这些PDF也只包含一页。

现在我想将这些单页PDF合并为一个多页PDF。从here开始,我想我已经确定了创建多页PDF的步骤。

运行下面的代码后,会创建一个包含预期文件名的PDF,但PDF只包含一个页面,而且它完全是空白的。

我在智慧'在这里结束...请告诉我我做错了什么!是的,我相信某种循环可以在这里起作用,但说实话,LOOPS总是让我变得更好...... :(

任何帮助将不胜感激!

哦,我几乎不能迅速 - 请不要向我扔任何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)

}

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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);
}