Edit DOCX in PHP SDK

Use REST API to edit DOCX in PHP programmatically

When developers modify a DOCX document with this PHP library, what is actually being edited is the Document Object Model (DOM). Thus, almost any changes can be made to the DOCX document represented as DOM. With the provided PHP SDK, developers can easily edit a document: modify text, update tables, add images and so on. Just load a DOCX, make the necessary changes programmatically and export the result to the same or any supported save format.

View code snippet

Edit DOCX in PHP using REST API

Our PHP library gives developers the ability to modify a DOCX document directly by editing its Document Object Model (DOM), which means no additional software needs to be installed.

Edit text in DOCX file using PHP

The most popular case to edit a DOCX file is text editing. With the given software solution, you can add, modify or delete text using PHP within the document.

Edit table in DOCX file using PHP

Another popular DOCX editing option is table editing. Our PHP SDK allows you to work with tables and text in table cells.

PHP developers can add or remove table and table cells, as well as add, edit and remove text within them.

Add an image to DOCX file using PHP

Besides editing text and tables in DOCX, there is another common option: adding images to a document in PHP. PHP developers can also add an image into a DOCX file using DOM.

Edit DOCX programmatically

Try this powerful PHP SDK and evaluate some options in DOCX document editing. Using the following example, load your DOCX document and make some changes: add text, add a table and a table cell with text or insert an image into the DOCX document:

Upload a document you want to modify
Run code
Upload an image you want insert
Select the target format from the list
use Aspose\Words\WordsApi;

$wordsApi = new WordsApi('####-####-####-####-####', '##################');

$requestDocument = "Input.docx";
$requestParagraph = new ParagraphInsert(array(
    "text" => "Morbi enim nunc faucibus a.",
));
$insertParagraphRequest = new InsertParagraphOnlineRequest(
    $requestDocument, $requestParagraph, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
);
$insertParagraph = $wordsApi->insertParagraphOnline($insertParagraphRequest);

$convertRequest = new ConvertDocumentRequest(
    array_key_first($insertParagraph->document()->values()), "docx", NULL, NULL, NULL, NULL
);
$convert = $wordsApi->convertDocument($convertRequest);
use Aspose\Words\WordsApi;

$wordsApi = new WordsApi('####-####-####-####-####', '##################');

$requestDocument = "Input.docx";
$requestTable = new TableInsert(array(
    "columns_count" => 1,
    "rows_count" => 2,
));
$insertTableRequest = new InsertTableOnlineRequest(
    $requestDocument, $requestTable, "", NULL, NULL, NULL, NULL, NULL, NULL
);
$insertTable = $wordsApi->insertTableOnline($insertTableRequest);

$convertRequest = new ConvertDocumentRequest(
    array_key_first($insertTable->document()->values()), "docx", NULL, NULL, NULL, NULL
);
$convert = $wordsApi->convertDocument($convertRequest);
use Aspose\Words\WordsApi;

$wordsApi = new WordsApi('####-####-####-####-####', '##################');

$requestDocument = "Input1.docx";
$requestDrawingObject = new DrawingObjectInsert(array(
    "height" => 0,
    "left" => 0,
    "top" => 0,
    "width" => 0,
    "relative_horizontal_position" => "Margin",
    "relative_vertical_position" => "Margin",
    "wrap_type" => "Inline",
));
$requestImageFile = "Input2.docx";
$insertDrawingObjectRequest = new InsertDrawingObjectOnlineRequest(
    $requestDocument, $requestDrawingObject, $requestImageFile, "sections/0", NULL, 
	   NULL, NULL, NULL, NULL, NULL
);
$insertDrawingObject = $wordsApi->insertDrawingObjectOnline($insertDrawingObjectRequest);

$convertRequest = new ConvertDocumentRequest(
    array_key_first($insertDrawingObject->document()->values()), "docx", NULL, NULL, NULL, NULL
);
$convert = $wordsApi->convertDocument($convertRequest);
Run code
  
Copy PHP code to the clipboard

How to edit DOCX PHP REST API

  1. Install Aspose.Words Cloud for PHP
  2. Add a library reference (import the library) to your PHP project
  3. Load DOCX to edit in PHP
  4. Insert content at the beginning of DOCX
  5. Download the result document from the cloud storage

PHP library to use DOCX editing

Install 'Aspose.Words Cloud SDK for PHP' from Packagist repository. Just run composer require aspose-cloud/aspose-words-cloud to install the SDK, then use require_once('vendor/autoload.php'); to import it into your project.

As an alternative, you can manually clone Aspose.Words Cloud SDK for PHP source code from GitHub. Please follow these Instructions to quickly get the necessary security credentials and access our REST API.

System Requirements

PHP 7.1 or newer

Refer to the Repository Documentation to see more details.

Other supported file formats

You can perform edit operation for other file formats:

5%

Subscribe to Aspose Product Updates

Get monthly newsletters and offers directly delivered to your mailbox.

© Aspose Pty Ltd 2001-2024. All Rights Reserved.