Skip to content
On this page

PDF Rotate ‚Äč

With the PDF rotation tool, you can rotate your PDF file as you want. The following examples show how to upload a test PDF file and rotate PDF pages 1~ 2 through 90 degrees using Java, PHP, C#, Python, and Swift languages. Then, output the rotated PDF file.

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

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

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

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

// 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 rotate task
$taskInfo = $client->createTask(CPDFDocumentEditor::ROTATION);

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

// 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 rotate task
CPDFCreateTaskResult result = client.CreateTask(CPDFDocumentEditorEnum.ROTATION);

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

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

// 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 rotate task
create_task_result = client.create_task(CPDFDocumentEditorEnum.ROTATION)

# Get a task id
task_id = create_task_result.task_id

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

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

# 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 rotate task
    let taskModel = await client.createTask(url: CPDFDocumentEditor.ROTATION)
    
    // 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 ?? "", password: "", params: [
        CPDFFileUploadParameterKey.pageOptions.string():["1"],
        CPDFFileUploadParameterKey.rotation.string() : "90"
    ], taskId: taskId)
    
    // Execute task
    let _ = await client.processFiles(taskId: taskId)
    
    // Query TaskInfo
    let taskInfoModel = await client.getTaskInfo(taskId: taskId)
}

Needed Parameters:

  • pageOptions: Select and rotate page pages, and the page number starts from 1 (The page number entered must be greater than 0 and cannot exceed the maximum page number of the document). Here is an example of rotating certain ranges of pages: 1,2,5-10. You can see all the rotated pages here: Pages 1, 2, 5, 6, 7, 8, 9, 10.

  • rotation: The rotation angle (0, 90, 180, 270) rotates clockwise.

Result:

File TypeDescription
.pdfThe PDF file after rotating.