Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function in Swift to Append a Pdf file to another Pdf

Tags:

pdf

swift

I created two different pdf files in two different views using following code:

private func toPDF(views: [UIView]) -> NSData? {

    if views.isEmpty {return nil}

    let pdfData = NSMutableData()
    UIGraphicsBeginPDFContextToData(pdfData, CGRect(x: 0, y: 0, width: 1024, height: 1448), nil)
    let context = UIGraphicsGetCurrentContext()

    for view in views {
        UIGraphicsBeginPDFPage()
        view.layer.renderInContext(context!)
    }

    UIGraphicsEndPDFContext()

    return pdfData
}

In the final view I call both files using:

let firstPDF = NSUserDefaults.standardUserDefaults().dataForKey("PDFone")
let secondPDF = NSUserDefaults.standardUserDefaults().dataForKey("PDFtwo")

My question is: Can anyone suggest a function which append the second file to the first one? (Both are in NSData Format)

like image 361
Ali Avatar asked May 10 '16 08:05

Ali


People also ask

How do you overlap a PDF?

In the Power PDF Properties dialog box, click the PDF Settings tab. In the Destination section, select Overlay with existing file in the If File Exists selection box. Check that Query the file name is selected in the Naming Method selection box. Click the Overlay button.

What does append a PDF mean?

The "Append" button will create a new document containing the existing PDF file and will add the new document information to the end of the file. The "Replace" button will overwrite the existing PDF file, and the "Cancel" button will cancel the PDF creation without affecting the existing PDF file.


2 Answers

Swift 4:

func merge(pdfs:Data...) -> Data
{
    let out = NSMutableData()
    UIGraphicsBeginPDFContextToData(out, .zero, nil)

    guard let context = UIGraphicsGetCurrentContext() else {
        return out as Data
    }

    for pdf in pdfs {
        guard let dataProvider = CGDataProvider(data: pdf as CFData), let document = CGPDFDocument(dataProvider) else { continue }

        for pageNumber in 1...document.numberOfPages {
            guard let page = document.page(at: pageNumber) else { continue }
            var mediaBox = page.getBoxRect(.mediaBox)
            context.beginPage(mediaBox: &mediaBox)
            context.drawPDFPage(page)
            context.endPage()
        }
    }

    context.closePDF()
    UIGraphicsEndPDFContext()

    return out as Data
}
like image 65
Andreas Ley Avatar answered Oct 20 '22 17:10

Andreas Ley


This can be done quite easily with PDFKit and its PDFDocument.

I'm using this extension:

    import PDFKit
    
    extension PDFDocument {
    
        func addPages(from document: PDFDocument) {
            let pageCountAddition = document.pageCount
    
            for pageIndex in 0..<pageCountAddition {
                guard let addPage = document.page(at: pageIndex) else {
                    break
                }
    
                self.insert(addPage, at: self.pageCount) // unfortunately this is very very confusing. The index is the page *after* the insertion. Every normal programmer would assume insert at self.pageCount-1
            }
        }
    
    }
like image 31
JuliusBahr Avatar answered Oct 20 '22 17:10

JuliusBahr