Compress
With the PDF Compression tool, you can compress your PDFs. The following examples show how to upload a PDF file and run the PDF Compression feature using Java, PHP, C#, Python, and Swift programming languages. Then, output a compressed PDF file.
Java
// Create a client
CPDFClient client = new CPDFClient(publicKey,secretKey);
// Create a task
// Create an example of a PDF Compression task
CPDFCreateTaskResult result = client.createTask(CPDFDocumentEditorEnum.COMPRESS);
// Get a task id
String taskId = result.getTaskId();
// File handling parameter settings
CPDFCompressParameter fileParameter = new CPDFCompressParameter();
fileParameter.setQuality("50");
// Upload files
client.uploadFile(new File("test.pdf"), taskId, fileParameter);
// 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 Compression task
$taskInfo = $client->createTask(CPDFDocumentEditor::COMPRESS);
// File handling parameter settings
$file = $client->addFile('test.pdf')
->setQuality('50');
//Upload files
$fileInfo = $file->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 Compression task
CPDFCreateTaskResult result = client.CreateTask(CPDFDocumentEditorEnum.COMPRESS);
// Get a task id
string taskId = result.TaskId;
// File handling parameter settings
CPDFCompressParameter fileParameter = new CPDFCompressParameter();
fileParameter.Quality = "50";
// Upload files
client.UploadFile(new FileInfo("test.pdf"), taskId, fileParameter);
// 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 Compression task
create_task_result = client.create_task(CPDFDocumentEditorEnum.COMPRESS)
# taskId
task_id = create_task_result.task_id
# File handling parameter settings
file_parameter = CPDFCompressParameter()
file-parameter.quality = "50"
# Upload file
upload_file_result = client.upload_file('test.pdf', task_id, file_parameter)
# Execute task
client.execute_task(task_id)
# get task processing information
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 Compression task
let taskModel = await client.createTask(url: CPDFDocumentEditor.COMPRESS)
// Get a task id
let taskId = taskModel?.taskId ?? ""
// Upload file
let path = Bundle.main.path(forResource: "test", ofType: "pdf")
let uploadFileModel = await client.uploadFile(filepath: path ?? "", password: "", params: [CPDFFileUploadParameterKey.quality.string():"50"], taskId: taskId)
// Execute task
let _ = await self.client.processFiles(taskId: taskId)
// Query TaskInfo
let taskInfoModel = await client.getTaskInfo(taskId: taskId)
}
Needed Parameters:
quality
: Compressed document quality in the range 0-100, e.g. 50.
Result:
File Type | Description |
---|---|
The PDF file after compressing. |