How to Remove an Object from a DOCX Document in PHP

Cloudmersive
2 min readFeb 5, 2021

--

DOCX is a popular format with businesses worldwide due its easy versatility and design functions. However, manipulating and editing an online DOCX file can be a difficult process if attempted manually. To simplify the modification process and improve the user experience, we offer several document editing APIs that can be used in conjunction. The following API utilizes the editing URL that is created by the Begin Editing function, and will require the path of the object you need to remove.

First, we will need to install the SDK:

composer require cloudmersive/cloudmersive_document_convert_api_client

Next, we will plug in the editing URL and object path, and call the function with the following code:

<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
$apiInstance = new Swagger\Client\Api\EditDocumentApi(


new GuzzleHttp\Client(),
$config
);
$req_config = new \Swagger\Client\Model\DocxRemoveObjectRequest(); // \Swagger\Client\Model\DocxRemoveObjectRequest | Document input request
try {
$result = $apiInstance->editDocumentDocxRemoveObject($req_config);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling EditDocumentApi->editDocumentDocxRemoveObject: ', $e->getMessage(), PHP_EOL;
}
?>

The returned response will indicate if the task was successful and will also provide and updated document URL. If you need to perform further editing functions, then you would continue on your modification path from here. However, if you are finished editing, you can use the Finish Editing API to download your updated result.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet