We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

MailMerge Example using PHP code

hi
i want MailMerge Class example of Aspose.words for PHP, I am have to Create a DSN to Connceted to the Database, or any Class has provided that do the conncetion to the database and will return the ResultSet
with regards
sunil satushe

Hi

Thanks for your inquiry. I think that you can find solution here.

Also I will try to create example for you (I am not strong in PHP).

Hope this helps.

Best regards.

Hi

I spent some time and created sample for you.

  1. You should create Java class that would return a ResultSet Object. See the following example.
public class RelationSet {

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();

}

}

As you see I used for testing Access DB.

  1. Compile this, put it in a jar file, and place it in your application root folder.

  2. Here is my PHP code.

<?php 

require\_once("http://localhost:8080/JavaBridge/java/Java.inc"); 

//Load libraries

java\_require("lib\Aspose.Words.jdk15.jar;lib\jaxen-1.1.jar;lib\RelationSet.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");

?>

Also see attached documents and sample database.

Hope this helps.

Best regards.

i get the following Error, as i have created the in.doc file in MS word where i have stated the MergeField as stated in the above example and to get the output of the Result i have created the out.doc in MS word as specific locaction

Warning**: fread() [function.fread]: Length parameter must be greater than 0 in C:\Program Files\xampp\htdocs\JavaBridge\java\Java.inc on line 1083

protocol error: , Invalid document end at col 1. Check the back end log for details.

Hi

Thanks for your request. Could you please attach your in.doc and your code for testing? Also, note that you don’t need to create out.doc.

Best regards.

FileName:: AsposeTest.php

<?php 
require\_once("java/Java.inc");
java\_autoload();
?>
<?php header("Content-type: text/html; charset=UTF-8"); 
java\_require("C:\\Program Files\\Java\\jdk1.6.0\_03\\jre\\lib\\ext\\Aspose.Words.jdk15.jar;
C:\\Program Files\\Java\\\jdk1.6.0\_03\\jre\\lib\\ext\\RelationSet.jar;
C:\\Program Files\\Java\\\jdk1.6.0\_03\\jre\\lib\\ext\\jaxen-1.1.jar");
echo "Loaded the libraries ";
$doc = new Java("com.aspose.words.Document", "C:\\Temp\\in.doc");
echo "Open document in.doc ";
$myResultSet = new Java("java.sql.ResultSet");
echo "Create ResultSet ";
$myQueryResult = new Java("RelationSet"); 
echo "creates an instance of my own class ";
$myResultSet = $myQueryResult->execQuery();
echo "executes a method of it ";
$doc->getMailMerge()->executeWithRegions("Users", $myResultSet);
echo "Execute mailMerge ";
$doc->save("C:\\Temp\\out.doc");
echo "Save document at c:\Temp\out.doc ";
?>

File Name:: in.doc

First Name Last Name
«TableStart:Users»«FirstName» «LastName»«TableEnd:Users»

Hi

Thank you for additional information. I can’t reproduce this. Your code works fine, but one difference – the following line should be a single line.

java\_require("packages\\Aspose.Words.jdk15.jar;packages\\RelationSet.jar;packages\\jaxen-1.1.jar");

Hope this helps.

Best regards.

hi
thank you for reviewing the code, do you got the same error what i got on my side with my code, i think that the java_require( ) will not be having problem of placing into single line or mulitline.its an arguement , plz do update me if you find solution to the error i have faced as soon as earlyer
with regards
sunil

Hi

Please see the following link. Maybe it will be useful for you.


Make sure the port 8080 is free and your servlet server is started on 8080 ONLY.

On my side I configure my apache to work on 8081 port and my servlet server on 8080.

Maybe this helps.

Have you tried simple hello world using Aspose.Words?

<?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");

?>

Best regards.

hi

thank you for your assistance. i want to share good news with you.finally i have done with MailMerge.once again i thank you for your co-operation as well as the assistance to achive it ,hoping that i future you will also provide the same

with regards

sunil satushe

i want an information about the Class DocumentBuilder in aspose.words, as per my information we can set the font of the document as well as the other properties but when i set a font will it be aviable through out the document or should i set it for each words, also i wnat to add an background image to the document how should i do it ? i am doing the whole thing using the php at front end

with regards
sunil

Hi

Thanks for your inquiry. I think that you should set font for each paragraph in the document. You can find information about DocumentBuilder class here.


I hope this could help you.

Best regards.

would please give me an example or snippet of code regarding what we can do with the DocumentBuilder Class of aspose.words, i mean to say the Paragraph ,font to set dor the document,setting Back ground image to Document ,with using Example on PHP

with Regards
sunil satushe

Hi

Here is code example for your.

  1. Background image can be inserted into the document header. See the following code.
<?php

require\_once("http://localhost:8080/JavaBridge/java/Java.inc");

//Load libraries

java\_require("packages\Aspose.Words.jdk15.jar;packages\jaxen-1.1.jar");

$headerFooterType = new Java("com.aspose.words.HeaderFooterType");

$wrapType = new Java("com.aspose.words.WrapType");

//Create document

$doc = new Java("com.aspose.words.Document");

//Create DocumentBuilder

$builder = new Java("com.aspose.words.DocumentBuilder", $doc );

//Move to primary header

$builder->moveToHeaderFooter($headerFooterType->HEADER\_PRIMARY);

//Insert image 

$awSahpe = $builder->insertImage("C:\\Temp\\test.jpg");

//Set wrap type of shape

$awSahpe->setWrapType($wrapType->NONE);

//Set position behind text

$awSahpe->setBehindText(true);

//Move to document end

$builder->moveToDocumentEnd();

//Write some text

$builder->write("Background image test");

//Save document

$doc->save("C:\\Temp\\out.doc");

?>
  1. Change font of document.
<?php

require\_once("http://localhost:8080/JavaBridge/java/Java.inc");

//Load libraries

java\_require("packages\Aspose.Words.jdk15.jar;packages\jaxen-1.1.jar");

//Open document

$doc = new Java("com.aspose.words.Document", "C:\\Temp\\in.doc");

for ( $sectionIndex = 0; $sectionIndex < $doc->getSections()->getCount(); $sectionIndex++)

{

$section = $doc->getSections()->get($sectionIndex);

for ( $paragraphIndex = 0; $paragraphIndex < $section->getBody()->getParagraphs()->getCount(); $paragraphIndex++)

{

$paragraph = $section->getBody()->getParagraphs()->get($paragraphIndex);

//Set font of paragraph

$paragraph->getParagraphFormat()->getStyle()->getFont()->setName("Arial");

} 

}

$doc->save("C:\\Temp\\out.doc");

?>

I hope this could help you.

Best regards.

Hi

Here is code example for your.

  1. Background image can be inserted into the document header. See the following code.
<?php

require\_once("http://localhost:8080/JavaBridge/java/Java.inc");

//Load libraries

java\_require("packages\Aspose.Words.jdk15.jar;packages\jaxen-1.1.jar");

$headerFooterType = new Java("com.aspose.words.HeaderFooterType");

$wrapType = new Java("com.aspose.words.WrapType");

//Create document

$doc = new Java("com.aspose.words.Document");

//Create DocumentBuilder

$builder = new Java("com.aspose.words.DocumentBuilder", $doc );

//Move to primary header

$builder->moveToHeaderFooter($headerFooterType->HEADER\_PRIMARY);

//Insert image 

$awSahpe = $builder->insertImage("C:\\Temp\\test.jpg");

//Set wrap type of shape

$awSahpe->setWrapType($wrapType->NONE);

//Set position behind text

$awSahpe->setBehindText(true);

//Move to document end

$builder->moveToDocumentEnd();

//Write some text

$builder->write("Background image test");

//Save document

$doc->save("C:\\Temp\\out.doc");

?>
  1. Change font of document.
<?php

require\_once("http://localhost:8080/JavaBridge/java/Java.inc");

//Load libraries

java\_require("packages\Aspose.Words.jdk15.jar;packages\jaxen-1.1.jar");

//Open document

$doc = new Java("com.aspose.words.Document", "C:\\Temp\\in.doc");

for ( $sectionIndex = 0; $sectionIndex < $doc->getSections()->getCount(); $sectionIndex++)

{

$section = $doc->getSections()->get($sectionIndex);

for ( $paragraphIndex = 0; $paragraphIndex < $section->getBody()->getParagraphs()->getCount(); $paragraphIndex++)

{

$paragraph = $section->getBody()->getParagraphs()->get($paragraphIndex);

//Set font of paragraph

$paragraph->getParagraphFormat()->getStyle()->getFont()->setName("Arial");

} 

}

$doc->save("C:\\Temp\\out.doc");

?>

I hope this could help you.

Best regards.

Last time i have said , i have done with the MergeField using the MS-ACCESS, but now i am using the MySql Server and driver called as com.mysql.jdbc.driver , i got the folling Fatal Error

Fatal error: Uncaught [[o:Exception]:"java.lang.Exception: Invoke failed: [[o:MailMerge]]->executeWithRegions((o:String)[o:String], (i:ResultSet)null). Cause: java.lang.NullPointerException: resultSet Responsible VM:
i check it serpartly using the Java code to connect with the Mysql server and excexuted the Query its working fine but using the Php its find some problem for the function called as executeWithRegions(Tablename, ResultSet ); of aspose.words pls to tell where i to get wrong

with regards
sunil satushe

Hi

Thanks for your inquiry. As I see from error message your ResultSet is null. Make sure that ResultSet is not null.

I hope this could help you.

Best regards.

hi

i have downloaded the aspose.words for java, i do have the zip file where i have the different folder, such as aspose.words.demo,aspose.words.demo.web . i have followed the step which are specified in Readme.notepad file. but i am not able to run the demo’s i am getting 401 error for web

with regards

sunil

Hi

Thanks for your inquiry. Could you please provide me more details about this error? Can you start Tomcad? I think that you missed something in configuration.

Best regards.

hi

as i have done with the mailmerge functionality using the aspose.words using java, at backend i have used the MS-access, can i use the .xls file instead of the .mdb file for genrate the aspose.words

and one think i want to ask ,is that i am still won’t able to have the mailmerge functionality using the MysqlServer at back-end, i am gettins the same error as nullpointer Exception ((resultset)null).

with regards

sunil satsuhe