Convert to PDF
Word To PDF
With the Word to PDF tool, you can convert your Word file into a PDF file. The following examples show how to upload a test .docx file and convert it into a 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 Word to PDF task
CPDFCreateTaskResult result = client.createTask(CPDFConversionEnum.DOCX_TO_PDF);
// Get a task id
String taskId = result.getTaskId();
// Upload files
client.uploadFile(new File("test.docx"), taskId);
// 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 Word to PDF task
$taskInfo = $client->createTask(CPDFConversion::DOCX_TO_PDF);
// Upload files
$fileInfo = $client->addFile('test.docx')->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 Word to PDF task
CPDFCreateTaskResult result = client.CreateTask(CPDFConversionEnum.DOCX_TO_PDF);
// Get a task id
string taskId = result.TaskId;
// Upload files
client.UploadFile(new FileInfo("test.docx"), taskId);
// 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 Word tO PDF task
create_task_result = client.create_task(CPDFConversionEnum.DOCX_TO_PDF)
# Get a task id
task_id = create_task_result.task_id
# Upload files
client.upload_file('test.docx', task_id)
# 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 Word tO PDF task
let taskModel = await client.createTask(url: CPDFConversion.DOCX_TO_PDF)
// Get a task id
let taskId = taskModel?.taskId ?? ""
// Upload files
let path = Bundle.main.path(forResource: "test", ofType: "docx")
let uploadFileModel = await client.uploadFile(filepath: path ?? "", params: [:], taskId: taskId)
// Execute task
let _ = await client.processFiles(taskId: taskId)
// Query TaskInfo
let taskInfoModel = await client.getTaskInfo(taskId: taskId)
}
Result:
File Type | Description |
---|---|
The PDF file after the transfer process is completed. |