Skip to content
On this page

PDF Merge ‚Äč

With the PDF merge tool, you can merge your PDF file as you want. The following examples show how to upload two test PDF files and merge the pages 1~2 of each PDF file, into one PDF file using Java, PHP, C#, Python, and Swift languages.

Java
// Create a client
CPDFClient client = new CPDFClient(publicKey,secretKey);

// Create a task
// Create an example of a PDF merge task
CPDFCreateTaskResult result = client.createTask(CPDFDocumentEditorEnum.MERGE);

// Get a task id
String taskId = result.getTaskId();

// File handling parameter settings
CPDFPageMergeParameter fileParameter = new CPDFPageMergeParameter();
fileParameter.setPageOptions(Arrays.asList("1","2"));

// Upload files
client.uploadFile(new File("test.pdf"), taskId, fileParameter);

File fileSecond = new File("sample/test.pdf");
// File handling parameter settings
CPDFPageMergeParameter fileParameterSecond = new CPDFPageMergeParameter();
fileParameterSecond.setPageOptions(Arrays.asList("1","2"));

// upload File
client.uploadFile(fileSecond,taskId,fileParameterSecond);

// Execute task
client.executeTask(taskId);

// Query TaskInfo
CPDFTaskInfoResult taskInfo = client.getTaskInfo(taskId);
PHP
// Create a client
$client = new CPDFClient('public_key', 'secret_key');

// Create a task
// Create an example of a PDF merge task
$taskInfo = $client->createTask(CPDFDocumentEditor::MERGE);

// File handling parameter settings
$fileOne = $client->addFile('test.pdf')
    ->setPageOptions(['1', '2']);

// Upload files
$fileInfoOne = $fileOne->uploadFile($taskInfo['taskId']);

// File handling parameter settings
$fileTwo = $client->addFile('test.pdf')
    ->setPageOptions(['1', '2']);

// Upload files
$fileInfoTwo = $fileTwo->uploadFile($taskInfo['taskId']);

// Execute task
$client->executeTask($taskInfo['taskId']);

// Query TaskInfo
$taskInfo = $client->getTaskInfo($taskInfo['taskId']);
C#
// Create a client
CPDFClient client = new CPDFClient(publicKey,secretKey);

// Create a task
// Create an example of a PDF merge task
CPDFCreateTaskResult result = client.CreateTask(CPDFDocumentEditorEnum.MERGE);

// Get a task id
string taskId = result.TaskId;

// File handling parameter settings
CPDFPageMergeParameter fileParameter = new CPDFPageMergeParameter();
fileParameter.PageOptions = new List<string>() { "1", "2" };

// Upload files
client.UploadFile(new FileInfo("test.pdf"), taskId, fileParameter);

// File handling parameter settings
CPDFPageMergeParameter fileParameterSecond = new CPDFPageMergeParameter();
fileParameterSecond.PageOptions = new List<string>() { "1", "2" };

// Upload files
client.UploadFile(new FileInfo("test.pdf"), taskId, fileParameterSecond);

// Execute task
client.ExecuteTask(taskId);

// Query TaskInfo
CPDFTaskInfoResult taskInfo = client.GetTaskInfo(taskId);
Python
# Create a client
client = CPDFClient(public_key, secret_key)

# Create a task
# Create an example of a PDF merge task
create_task_result = client.create_task(CPDFDocumentEditorEnum.MERGE)

# Get a task id
task_id = create_task_result.task_id

# File handling parameter settings
file_parameter = CPDFPageMergeParameter()
file_parameter.page_options = ["1", "2"]

# Upload files
client.upload_file('test.pdf', task_id, file_parameter)

# File handling parameter settings
file_parameter_second = CPDFPageMergeParameter()
file_parameter_second.page_options = ["1", "2"]

# Upload files
client.upload_file('test.pdf', task_id, file_parameter_second)

# Execute task
client.execute_task(task_id)

# Query TaskInfo
task_info = client.get_task_info(task_id)
Swift
// Create a client
let client: CPDFClient = CPDFClient(publicKey: public_key, secretKey: secret_key)

Task { @MainActor in
    // Create a task
    // Create an example of a PDF merge task
    let taskModel = await client.createTask(url: CPDFDocumentEditor.MERGE, language: .english)
    
    // Get a task id
    let taskId = taskModel?.taskId ?? ""

    // Upload files
    let path = Bundle.main.path(forResource: "test", ofType: "pdf")
    let uploadFileModel = await client.uploadFile(filepath: path ?? "", language: .english, params: [CPDFFileUploadParameterKey.pageOptions.string():["1,2"]], taskId: taskId)
    // Upload files
    let uploadFileModel2 = await client.uploadFile(filepath: path ?? "",language: .english ,params: [CPDFFileUploadParameterKey.pageOptions.string():["1,2"]], taskId: taskId)
    
    // Execute task
    let _ = await client.processFiles(taskId: taskId, language: .english)
    
    // Query TaskInfo
    let taskInfoModel = await client.getTaskInfo(taskId: taskId, language: .english)
}

Needed Parameters:

  • pageOptions: Merge the page range of the document, the page number starts from 1, for example: 1,2,4,6,9-11 (if you do not enter all pages by default, the page number entered cannot exceed the maximum page number of the document)

Result:

File TypeDescription
.pdfThe PDF file after merging.