Merge PDF Files in Swift

Cloudmersive
2 min readApr 8, 2021

--

Do you have multiple PDF files you need to combine? If so, you can use the following API to insert a list of up to ten input files to be combined into one PDF; the order in which the input files are placed will even be preserved in the merged document. If necessary for your project, you may also perform this action more than once by using your output file from the first instance as the first input for the second, followed by the rest of the documents you wish to merge.

To call the merge function, you simply need to plug your files into the following example code:

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
var semaphore = DispatchSemaphore (value: 0)let parameters = [
[
"key": "inputFile1",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile2",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile3",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile4",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile5",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile6",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile7",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile8",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile9",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile10",
"src": "/path/to/file",
"type": "file"
]] as [[String : Any]]
let boundary = "Boundary-\(UUID().uuidString)"
var body = ""
var error: Error? = nil
for param in parameters {
if param["disabled"] == nil {
let paramName = param["key"]!
body += "--\(boundary)\r\n"
body += "Content-Disposition:form-data; name=\"\(paramName)\""
if param["contentType"] != nil {
body += "\r\nContent-Type: \(param["contentType"] as! String)"
}
let paramType = param["type"] as! String
if paramType == "text" {
let paramValue = param["value"] as! String
body += "\r\n\r\n\(paramValue)\r\n"
} else {
let paramSrc = param["src"] as! String
let fileData = try NSData(contentsOfFile:paramSrc, options:[]) as Data
let fileContent = String(data: fileData, encoding: .utf8)!
body += "; filename=\"\(paramSrc)\"\r\n"
+ "Content-Type: \"content-type header\"\r\n\r\n\(fileContent)\r\n"
}
}
}
body += "--\(boundary)--\r\n";
let postData = body.data(using: .utf8)
var request = URLRequest(url: URL(string: "https://api.cloudmersive.com/convert/merge/pdf/multi")!,timeoutInterval: Double.infinity)
request.addValue("multipart/form-data", forHTTPHeaderField: "Content-Type")
request.addValue("YOUR-API-KEY-HERE", forHTTPHeaderField: "Apikey")
request.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
semaphore.signal()
return
}
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}
task.resume()
semaphore.wait()

You’ll receive an instant result with your newly merged document. Visit the Cloudmersive website to register for a free account and retrieve your personal API key; this will provide access to 800 monthly calls across our library of APIs.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet