从其他 PDF 创建多页 PDF

Posted

技术标签:

【中文标题】从其他 PDF 创建多页 PDF【英文标题】:Create Multi-Page PDF from other PDFs 【发布时间】:2016-03-15 12:34:30 【问题描述】:

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

现在我想将这些单页 PDF 合并为一个多页 PDF。来自here,我想我已经了解了创建多页 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)


【问题讨论】:

好吧,事实证明,上面的代码除了将原始的单页 PDF 重写为空白页之外什么也没做!我真的需要一些帮助! 可以帮到你-***.com/questions/58483933/… 【参考方案1】:

正如上面的 cmets 所述,我发布的代码是垃圾。现在我已经解决了 - 创建了多页 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的信息!

【讨论】:

【参考方案2】:

上面的 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);

【讨论】:

以上是关于从其他 PDF 创建多页 PDF的主要内容,如果未能解决你的问题,请参考以下文章

iOS - 从 HTML 内容创建多页 PDF [关闭]

从大于单页的动态创建的表 (div) 创建多页 PDF

leadtools20,从多个图像创建多页文件

leadtools20,从多个图像创建多页文件

使用 android.graphics.pdf 创建多页 PDF

ImageMagick:在不使用外部文本文件的情况下将选择性图像转换为多页 PDF?