Aspose.Words is a document processing component that allows to read, modify and write Word® documents without utilizing Microsoft Word®. Aspose.Words is available in .NET and Java versions. There is no special version of Aspose.Words for PHP, but if you want to use Aspose.Words in your PHP project then this article is intended for you.
You can either use Aspose.Words for .NET (via COM) or Aspose.Words for Java (via PHP/Java Bridge) with PHP. This article describes both scenarios. There are pros and cons for either.
|
Scenario
|
When to Use
|
|
Aspose.Words for .NET via COM
|
Pros:
- New features first appear in Aspose.Words for .NET and as a result, it has more features than Aspose.Words for Java.
Cons:
- Available on Microsoft Windows platforms only.
- Cannot call static methods.
- Hard to call overloaded methods (suffixes added to names).
- Hard to use enumerated values (need to lookup and use a constant value).
- Cannot invoke constructors with parameters.
|
|
Aspose.Words for Java via PHP/Java Bridge
|
Pros:
- Available on any platform where Java and PHP is available.
- Easy to call static methods, constructors with parameters, overloaded methods and use enumerated values.
Cons:
- Has less features than Aspose.Words for .NET
|
Prerequisites
Aspose.Words for .NET with PHP via COM
Configure your PHP to work with COM. See http://www.php.net/manual/en/ref.com.php for more information.
Download the latest version of Aspose.Words for .NET from http://www.aspose.com/Community/Files/51/aspose.words/category1188.aspx and install for all users. See Utilize Aspose.Words from COM Clients (ASP, VB, VBScript) for more info.
Aspose.Words for Java with PHP via PHP/Java Bridge
Install and configure the PHP/Java Bridge to make use of Java from PHP. For more details about installing and configuring the PHP/Java Bridge see http://www.php.net/manual/en/ref.java.php.
Also you can download PHP/Java Bridge that doesn’t require java extension from http://php-java-bridge.sourceforge.net/pjb.
Download the latest version of Aspose.Words for Java from http://www.aspose.com/Community/Files/51/aspose.words/category1201.aspx. Extract files and copy the lib folder with jar files to the root folder of your PHP project.
Create a Hello World Application in PHP
Let’s create a “Hello World!” application.
Aspose.Words for .NET
<?php
//Create Document object
$doc = new COM("Aspose.Words.Document");
//Create DocumentBuilder
$builder = new COM("Aspose.Words.DocumentBuilder");
//Set document
$builder->Document = $doc;
//Write “Hello world!” in the document
$builder->Write("Hello world!!!");
//Save created document
$doc->Save("C:\\Temp\\out.doc");
?>
Aspose.Words for Java
<?php
require_once("http://localhost:8080/JavaBridge/java/Java.inc");
java_require("lib\\Aspose.Words.jdk15.jar;lib\\jaxen-1.1.jar");
// Create a Document object
$doc = new Java("com.aspose.words.Document");
// Create a DocumentBuilder
$builder = new Java("com.aspose.words.DocumentBuilder");
// Set the document
$builder->setDocument($doc);
// Write “Hello world!” in word document
$builder->write("Hello world!");
// Save created document
$doc->save("C:\\Temp\\out.doc");
?>
Fill a Document with Data
Aspose.Words provides a reporting generation facility similar to Microsoft Word’s mail merge. Here is a code example how you can perform a simple mail merge using Aspose.Words.
Aspose.Words for .NET
In COM you can use only the MailMerge.ExecuteADO method.
<?php
//Create instance od ComHelper class
$helper = new COM("Aspose.Words.ComHelper");
//Open a template document
$doc = $helper->Open("C:\\Temp\\in.doc");
//Connect Database
$dbfile ='C:\\Temp\\myDb.mdb';
$conn = new COM("ADODB.Connection");
$conn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=".$dbfile);
//Select records fromDB
$rs = $conn->Execute("SELECT * FROM Clients");
//Execute mail merge
$doc->MailMerge->ExecuteADO($rs);
//Save generated document
$doc->Save("C:\\Temp\\out.doc");
//Close RecordSet
$rs->Close();
?>
Aspose.Words for Java – Simple Mail Merge
<?php
require_once("http://localhost:8080/JavaBridge/java/Java.inc");
java_require("lib\\Aspose.Words.jdk15.jar;lib\\jaxen-1.1.jar");
// Open template file
$doc = new Java("com.aspose.words.Document", "C:\\Temp\\in.doc");
// Create names and values arrays
$names = array("FullName", "Company", "City");
$values = array("Alexey Noskov", "Aspose", "Auckland");
// Execute mailmerge
$doc->getMailMerge()->execute($names, $values);
// Save generated report
$doc->save("C:\\Temp\\out.doc");
?>
Aspose.Words for Java - Mail Merge with Regions
First, create a Java class that would return a ResultSet object as shown below. Compile it, put it in a jar file, and place it in your application root folder.
public class MyTest {
public static ResultSet execQuery()
{
try
{
//Create database query
String sqlString = "SELECT * FROM Users";
//Create statement
Statement stmt = createStatement();
//Return ResultSet object
return stmt.executeQuery(sqlString);
}
catch(Exception e)
{
System.out.println(e);
}
return null;
}
/**
* Utility function that creates a connection to the Database.
*/
private static Statement createStatement() throws Exception
{
//Load a DB driver that is used by the demos
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Compose connection string.
String connectionString = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\\Temp\\test.mdb;UID=Admin";
//DSN-less DB connection.
Connection connection = DriverManager.getConnection(connectionString);
//Reutrn Statement
return connection.createStatement();
}
}
Next, write your PHP code.
<?php
require_once("http://localhost:8080/JavaBridge/java/Java.inc");
java_require("lib\Aspose.Words.jdk15.jar;lib\jaxen-1.1.jar;lib\MyTest.jar");
//Open document
$doc = new Java("com.aspose.words.Document", "C:\\Temp\\in.doc");
//Create ResultSet
$myResultSet = new Java("java.sql.ResultSet");
//creates an instance of my own class and executes a method of it: the database query
$myQueryResult = new Java("RelationSet");
$myResultSet = $myQueryResult->execQuery();
//Execute mailMerge
$doc->getMailMerge()->executeWithRegions("Users", $myResultSet);
//Save document
$doc->save("C:\\Temp\\out.doc");
?>
Save or Convert a Document
With Aspose.Words, you can save a document in a variety of formats. For the full list of formats and identifiers see the SaveFormat enumeration.
Aspose.Words for .NET
//Save a document in a variety of formats.
//SaveFormat.Doc = 1, therefore 1 is passed as a parameter to indicate save format.
$doc->Save_2("C:\\Temp\\out.doc", 1);
$doc->Save_2("C:\\Temp\\out.txt", 2);
$doc->Save_2("C:\\Temp\\out.html", 4);
$doc->Save_2("C:\\Temp\\out.rtf", 6);
$doc->Save_2("C:\\Temp\\out.docx", 8);
Note that some methods have overloads and they will be exposed to COM clients with a number suffix added to them except the very first method that stays unchanged. For example, the Document.Save method overloads become Document.Save, Document.Save_2, Document.Save_3, and so on. The order in which method overloads are shown in the API documentation corresponds to the way they are numbered. This is the approach that .NET COM Interop takes to make overloaded methods available to COM clients.
Aspose.Words for Java
//Save a document in a variety of formats.
$saveFormat = new Java("com.aspose.words.SaveFormat");
$doc->save("C:\\Temp\\out.doc", $saveFormat->DOC);
$doc->save("C:\\Temp\\out.html", $saveFormat->HTML);
$doc->save("C:\\Temp\\out.txt", $saveFormat->TEXT);
Invoke Static Methods
This is only possible from PHP when using Aspose.Words for Java (because of .NET COM Interop limitations).
Some methods in the Aspose.Words object model are static. For example, there is the detectFormat method that allows you to detect the original format of a file.
<?php
require_once("http://localhost:8080/JavaBridge/java/Java.inc");
java_require("lib\Aspose.Words.jdk15.jar;lib\jaxen-1.1.jar");
$loadFormat = new Java("com.aspose.words.LoadFormat");
// Note creating a class object, not an instance.
$doc = new JavaClass("com.aspose.words.Document");
// Now we can call static methods on the class object.
$test = $doc->detectFileFormat("C:\\Temp\\out.doc");
if ($test == $loadFormat->DOC)
{
echo("DOC");
}
else if ($test == $loadFormat->HTML)
{
echo("HTML");
}
else
{
echo("Unknown format");
}
?>
Summary
In this article we have shown basics of using Aspose.Words in your PHP project:
- How to choose between Aspose.Words for .NET and ASpose.Words for Java to use with PHP.
- Create objects, call methods, access properties, modify a document with the DocumentBuilder object.
- Generate a report using Aspose.Words mail merge.
- Convert a document into a different format, use enumerated values.
If you follow the principles described in this article, you can use all features of Aspose.Words in a PHP project.