Aspose Word Template Usage Summary
Namespaces:
- Using Aspose.words;
- Using Aspose.Words.Saving;
- Using System.IO;
- Using System.Data;
Add DLL :
Link: Http://pan.baidu.com/s/1pJG899T Password: bv3k
1. Create A Word template, bind data using mergefeild
Create a new Word document named Template.doc
Note: This is not the "" "and" ", but must be in the menu" Insert → document parts → domain "to find mergefield and enter the appropriate domain name
2. using arrays to provide data sources
- String TempPath =server.mappath ("~/docs/temp/template.doc");
- String OutputPath =server.mappath ("~/docs/output/template.doc");
- Loading templates
- var doc = new Document (TempPath);
- Provide a data source
- string[] FieldNames = new string[] {"UserName", "Gender", "BirthDay", "Address"};
- object[] fieldvalues = new object[] {"Zhang San", "Male", "1988-09-02", "Shaanxi Xianyang"};
- Merge templates, equivalent to page rendering
- Doc. Mailmerge.execute (fieldnames,fieldvalues);
- Save the merged document
- Doc. Save (OutputPath);
- In WebForm, save the document to the stream, using response.? BinaryWrite Output The file
- var docstream = new MemoryStream ();
- Doc. Save (Docstream,saveoptions.createsaveoptions (Saveformat.doc));
- Response.ContentType = "Application/msword";
- Response.AddHeader ("Content-disposition", "attachment; Filename=template.doc ");
- Response.BinaryWrite (Docstream.toarray ());
- Response.End ();
- Adopted in MVC, save the document to the stream, using base. File output
- var docstream = new MemoryStream ();
- Doc. Save (Docstream,saveoptions.createsaveoptions (Saveformat.doc));
- Return base. File (Docstream.toarray (), "Application/msword", "Template.doc");
3. Create a template for the cyclic data, where the loop data is similar to the page's for structure, not rigidly tied to the form table
? Tablestart:userlist?
Name:? UserName?
? Tableend:userlist?
4. using a DataTable to provide a data source
- Create a DataTable named UserList
- DataTable table=new DataTable ("UserList");
- Table. Columns.Add ("UserName");
- Table. Columns.Add ("Gender");
- Table. Columns.Add ("BirthDay");
- Table. Columns.Add ("Address");
- //----------------------------------------------------------------------------------------------------
- Loading templates
- var doc = new Document (TempPath);
- Provide a data source
- var datatable=getdatatable ();
- Merge templates, equivalent to page rendering
- Doc. Mailmerge.executewithregions (DataTable);
- var docstream = new MemoryStream ();
- Doc. Save (Docstream,saveoptions.createsaveoptions (Saveformat.doc));
- Return base. File (Docstream.toarray (), "Application/msword", "Template.doc");
5. Binding with sub-cyclic data templates
6. using a dataset to provide a data source
- User table structure
- DataTable table = new DataTable ("UserList");
- Table. Columns.Add (New DataColumn ("Id", typeof (int)));
- Table. Columns.Add ("UserName");
- Table. Columns.Add ("Gender");
- Table. Columns.Add ("BirthDay");
- Table. Columns.Add ("Address");
- Fractional table structure
- DataTable table = new DataTable ("Scorelist");
- Table. Columns.Add (New DataColumn ("UserId", typeof (int)));
- Table. Columns.Add ("Name");
- Table. Columns.Add ("Score");
- //----------------------------------------------------------------------------------------------------
- Loading templates
- var doc = new Document (TempPath);
- Provide a data source
- DataSet DataSet = new DataSet ();
- var usertable=getuserdatatable ();
- var userscoretable=getuserscoredatatable ();
- DATASET.TABLES.ADD (usertable);
- DATASET.TABLES.ADD (userscoretable);
- DATASET.RELATIONS.ADD (New DataRelation ("Scorelistforuser", usertable.columns["Id"],?userscoretable.columns[" UserId "]));
- Merge templates, equivalent to page rendering
- Doc. Mailmerge.executewithregions (DataSet);
- var docstream = new MemoryStream ();
- Doc. Save (Docstream,saveoptions.createsaveoptions (Saveformat.doc));
- Return base. File (Docstream.toarray (), "Application/msword", "Template.doc");
7. use bookmarks on template, insert marker position
Select the text in the document, and in the menu, insert → bookmark, specify the name of the bookmark, sort by selected as location, and add a new bookmark. The selected text is the bookmark's Text property and is here for easy viewing. You can also insert a bookmark directly and specify a location, just not obvious.
8. Insert the contents of another document at the bookmark location
- Loading templates
- var doc = new Document (TempPath);
- var doc1 = new Document (TEMPPATH1);
- Find a bookmark with the name Positionflag
- var bookmark=doc. range.bookmarks["Positionflag"];
- Empty the bookmarked text
- Bookmark. Text = "";
- Use the Documentbuilder object to insert some document objects, such as inserting a bookmark, inserting a text box, inserting a check box, inserting a paragraph, inserting a blank page, appending or another Word file's contents, and so on.
- var builder = new Documentbuilder (DOC);
- Navigate to the specified location for insert operation
- Builder. MoveToBookmark ("Positionflag");
- In the Positionflag bookmark location, insert the contents of another document.
- The Insertdocument method can be found in http://www.aspose.com/docs/display/wordsnet/How+to++Insert+a+Document+into+another+Document
- Insertdocument (bookmark. BOOKMARKSTART.PARENTNODE,DOC1);
9. Create a word template and insert a picture using mergefeild
Inserting a picture example
- String TempPath =server.mappath ("~/docs/temp/template.doc");
- String Logopath =server.mappath ("~/content/logo.jpg");
- var doc = new Document (TempPath); Loading templates
- Provide a data source
- string[] FieldNames = new string[] {"logo", "Gender", "BirthDay", "Address", "logo"};
- object[] fieldvalues = new object[] {"Zhang San", "Male", "1988-09-02", "Shaanxi Xianyang", Logopath};
- Increase processing of picture size programs
- Doc. mailmerge.fieldmergingcallback= new Handlemergefieldinsertdocument ();
- Merge templates, equivalent to page rendering
- Doc. Mailmerge.execute (fieldnames,fieldvalues);
- Adopted in MVC, save the document to the stream, using base. File output
- var docstream = new MemoryStream ();
- Doc. Save (Docstream,saveoptions.createsaveoptions (Saveformat.doc));
- Return base. File (Docstream.toarray (), "Application/msword", "Template.doc");
The effect is as follows:
Program to increase image size processing
- Aspose.word provides a handler-like feature that Ifieldmergingcallback allows us to handle dynamically MERGEFIELD
- void Ifieldmergingcallback.fieldmerging (FIELDMERGINGARGSE) {}//Processing text
- Voidifieldmergingcallback.imagefieldmerging (Imagefieldmergingargs args) {}//Process picture
- Here we handle the picture and write a custom class implementation
- Classhandlemergefieldinsertdocument:ifieldmergingcallback
- {
- Text processing here, if written in this piece, does not work
- Voidifieldmergingcallback.fieldmerging (Fieldmergingargs e)
- {
- }
- Picture processing in here
- Voidifieldmergingcallback.imagefieldmerging (Imagefieldmergingargs args)
- {
- if (args. Documentfieldname.equals ("Logo"))
- {
- Use Documentbuilder to manipulate the size of a picture
- Documentbuilderbuilder = new Documentbuilder (args. Document);
- Builder. Movetomergefield (args. FieldName);
- Shapeshape = Builder. Insertimage (args. Fieldvalue.tostring ());
- Sets the x, y coordinates and the height width.
- Shape. left= 0;
- Shape. top= 0;
- Shape. Width= 60;
- Shape. Height= 80;
- }
- }
- }
The effect is as follows:
One by one . inserting Html into templates
Here's home introduction using HTML format
inserting html samples
- String TempPath =server.mappath ("~/docs/temp/template.doc");
- String deschtml = "";//here is HTML text, because too long omitted
- var doc = new Document (TempPath); Loading templates
- Provide a data source
- string[] FieldNames = new string[] {"UserName", "Gender", "BirthDay", "Address", "Desc"};
- object[] fieldvalues = new object[] {"Zhang San", "Male", "1988-09-02", "Shaanxi Xianyang", deschtml};
- Increased processing of HTML programs
- Doc. mailmerge.fieldmergingcallback= new handlemergefieldinserthtml ();
- Merge templates, equivalent to page rendering
- Doc. Mailmerge.execute (fieldnames,fieldvalues);
- Adopted in MVC, save the document to the stream, using base. File output
- var docstream = new MemoryStream ();
- Doc. Save (Docstream,saveoptions.createsaveoptions (Saveformat.doc));
- Return base. File (Docstream.toarray (), "Application/msword", "Template.doc");
- If you do not add the HTML handler, the default to the output of the text, here we write a custom processing class
- Class Handlemergefieldinserthtml:ifieldmergingcallback
- {
- Text Processing here
- Voidifieldmergingcallback.fieldmerging (Fieldmergingargs e)
- {
- if (E.documentfieldname.equals ("Desc"))
- {
- Use Documentbuilder to manipulate the size of a picture
- Documentbuilderbuilder = new Documentbuilder (e.document);
- Builder. Movetomergefield (E.fieldname);
- Builder. Inserthtml (E.fieldvalue.tostring ());
- }
- }
- Picture processing in here
- Voidifieldmergingcallback.imagefieldmerging (Imagefieldmergingargs args)
- {
- }
- }
- The same applies to the ifieldmergingcallback in the cyclic structure
- Summary: Use bookmarks with flag bits to flexibly handle a variety of requirements with custom ifieldmergingcallback, and continue to try to load different templates based on conditions
Aspose Word Template Usage Summary