Aspose Word Template Usage Summary

Source: Internet
Author: User
Tags processing text blank page

Aspose Word Template Usage Summary

Namespaces:

    1. Using Aspose.words;
    2. Using Aspose.Words.Saving;
    3. Using System.IO;
    4. 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

  1. String TempPath =server.mappath ("~/docs/temp/template.doc");
  2. String OutputPath =server.mappath ("~/docs/output/template.doc");
  3. Loading templates
  4. var doc = new Document (TempPath);
  5. Provide a data source
  6. string[] FieldNames = new string[] {"UserName", "Gender", "BirthDay", "Address"};
  7. object[] fieldvalues = new object[] {"Zhang San", "Male", "1988-09-02", "Shaanxi Xianyang"};
  8. Merge templates, equivalent to page rendering
  9. Doc. Mailmerge.execute (fieldnames,fieldvalues);
  10. Save the merged document
  11. Doc. Save (OutputPath);
  12. In WebForm, save the document to the stream, using response.? BinaryWrite Output The file
  13. var docstream = new MemoryStream ();
  14. Doc. Save (Docstream,saveoptions.createsaveoptions (Saveformat.doc));
  15. Response.ContentType = "Application/msword";
  16. Response.AddHeader ("Content-disposition", "attachment; Filename=template.doc ");
  17. Response.BinaryWrite (Docstream.toarray ());
  18. Response.End ();
  19. Adopted in MVC, save the document to the stream, using base. File output
  20. var docstream = new MemoryStream ();
  21. Doc. Save (Docstream,saveoptions.createsaveoptions (Saveformat.doc));
  22. 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

    1. Create a DataTable named UserList
    2. DataTable table=new DataTable ("UserList");
    3. Table. Columns.Add ("UserName");
    4. Table. Columns.Add ("Gender");
    5. Table. Columns.Add ("BirthDay");
    6. Table. Columns.Add ("Address");
    7. //----------------------------------------------------------------------------------------------------
    8. Loading templates
    9. var doc = new Document (TempPath);
    10. Provide a data source
    11. var datatable=getdatatable ();
    12. Merge templates, equivalent to page rendering
    13. Doc. Mailmerge.executewithregions (DataTable);
    14. var docstream = new MemoryStream ();
    15. Doc. Save (Docstream,saveoptions.createsaveoptions (Saveformat.doc));
    16. 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

  1. User table structure
  2. DataTable table = new DataTable ("UserList");
  3. Table. Columns.Add (New DataColumn ("Id", typeof (int)));
  4. Table. Columns.Add ("UserName");
  5. Table. Columns.Add ("Gender");
  6. Table. Columns.Add ("BirthDay");
  7. Table. Columns.Add ("Address");
  8. Fractional table structure
  9. DataTable table = new DataTable ("Scorelist");
  10. Table. Columns.Add (New DataColumn ("UserId", typeof (int)));
  11. Table. Columns.Add ("Name");
  12. Table. Columns.Add ("Score");
  13. //----------------------------------------------------------------------------------------------------
  14. Loading templates
  15. var doc = new Document (TempPath);
  16. Provide a data source
  17. DataSet DataSet = new DataSet ();
  18. var usertable=getuserdatatable ();
  19. var userscoretable=getuserscoredatatable ();
  20. DATASET.TABLES.ADD (usertable);
  21. DATASET.TABLES.ADD (userscoretable);
  22. DATASET.RELATIONS.ADD (New DataRelation ("Scorelistforuser", usertable.columns["Id"],?userscoretable.columns[" UserId "]));
  23. Merge templates, equivalent to page rendering
  24. Doc. Mailmerge.executewithregions (DataSet);
  25. var docstream = new MemoryStream ();
  26. Doc. Save (Docstream,saveoptions.createsaveoptions (Saveformat.doc));
  27. 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

    1. Loading templates
    2. var doc = new Document (TempPath);
    3. var doc1 = new Document (TEMPPATH1);
    4. Find a bookmark with the name Positionflag
    5. var bookmark=doc. range.bookmarks["Positionflag"];
    6. Empty the bookmarked text
    7. Bookmark. Text = "";
    8. 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.
    9. var builder = new Documentbuilder (DOC);
    10. Navigate to the specified location for insert operation
    11. Builder. MoveToBookmark ("Positionflag");
    12. In the Positionflag bookmark location, insert the contents of another document.
    13. The Insertdocument method can be found in http://www.aspose.com/docs/display/wordsnet/How+to++Insert+a+Document+into+another+Document
    14. Insertdocument (bookmark. BOOKMARKSTART.PARENTNODE,DOC1);

9. Create a word template and insert a picture using mergefeild

Inserting a picture example

    1. String TempPath =server.mappath ("~/docs/temp/template.doc");
    2. String Logopath =server.mappath ("~/content/logo.jpg");
    3. var doc = new Document (TempPath); Loading templates
    4. Provide a data source
    5. string[] FieldNames = new string[] {"logo", "Gender", "BirthDay", "Address", "logo"};
    6. object[] fieldvalues = new object[] {"Zhang San", "Male", "1988-09-02", "Shaanxi Xianyang", Logopath};
    7. Increase processing of picture size programs
    8. Doc. mailmerge.fieldmergingcallback= new Handlemergefieldinsertdocument ();
    9. Merge templates, equivalent to page rendering
    10. Doc. Mailmerge.execute (fieldnames,fieldvalues);
    11. Adopted in MVC, save the document to the stream, using base. File output
    12. var docstream = new MemoryStream ();
    13. Doc. Save (Docstream,saveoptions.createsaveoptions (Saveformat.doc));
    14. Return base. File (Docstream.toarray (), "Application/msword", "Template.doc");

The effect is as follows:

Program to increase image size processing

  1. Aspose.word provides a handler-like feature that Ifieldmergingcallback allows us to handle dynamically MERGEFIELD
  2. void Ifieldmergingcallback.fieldmerging (FIELDMERGINGARGSE) {}//Processing text
  3. Voidifieldmergingcallback.imagefieldmerging (Imagefieldmergingargs args) {}//Process picture
  4. Here we handle the picture and write a custom class implementation
  5. Classhandlemergefieldinsertdocument:ifieldmergingcallback
  6. {
  7. Text processing here, if written in this piece, does not work
  8. Voidifieldmergingcallback.fieldmerging (Fieldmergingargs e)
  9. {
  10. }
  11. Picture processing in here
  12. Voidifieldmergingcallback.imagefieldmerging (Imagefieldmergingargs args)
  13. {
  14. if (args. Documentfieldname.equals ("Logo"))
  15. {
  16. Use Documentbuilder to manipulate the size of a picture
  17. Documentbuilderbuilder = new Documentbuilder (args. Document);
  18. Builder. Movetomergefield (args. FieldName);
  19. Shapeshape = Builder. Insertimage (args. Fieldvalue.tostring ());
  20. Sets the x, y coordinates and the height width.
  21. Shape. left= 0;
  22. Shape. top= 0;
  23. Shape. Width= 60;
  24. Shape. Height= 80;
  25. }
  26. }
  27. }

The effect is as follows:

One by one . inserting Html into templates

Here's home introduction using HTML format

inserting html samples

  1. String TempPath =server.mappath ("~/docs/temp/template.doc");
  2. String deschtml = "";//here is HTML text, because too long omitted
  3. var doc = new Document (TempPath); Loading templates
  4. Provide a data source
  5. string[] FieldNames = new string[] {"UserName", "Gender", "BirthDay", "Address", "Desc"};
  6. object[] fieldvalues = new object[] {"Zhang San", "Male", "1988-09-02", "Shaanxi Xianyang", deschtml};
  7. Increased processing of HTML programs
  8. Doc. mailmerge.fieldmergingcallback= new handlemergefieldinserthtml ();
  9. Merge templates, equivalent to page rendering
  10. Doc. Mailmerge.execute (fieldnames,fieldvalues);
  11. Adopted in MVC, save the document to the stream, using base. File output
  12. var docstream = new MemoryStream ();
  13. Doc. Save (Docstream,saveoptions.createsaveoptions (Saveformat.doc));
  14. Return base. File (Docstream.toarray (), "Application/msword", "Template.doc");
  15. If you do not add the HTML handler, the default to the output of the text, here we write a custom processing class
  16. Class Handlemergefieldinserthtml:ifieldmergingcallback
  17. {
  18. Text Processing here
  19. Voidifieldmergingcallback.fieldmerging (Fieldmergingargs e)
  20. {
  21. if (E.documentfieldname.equals ("Desc"))
  22. {
  23. Use Documentbuilder to manipulate the size of a picture
  24. Documentbuilderbuilder = new Documentbuilder (e.document);
  25. Builder. Movetomergefield (E.fieldname);
  26. Builder. Inserthtml (E.fieldvalue.tostring ());
  27. }
  28. }
  29. Picture processing in here
  30. Voidifieldmergingcallback.imagefieldmerging (Imagefieldmergingargs args)
  31. {
  32. }
  33. }
  34. The same applies to the ifieldmergingcallback in the cyclic structure
  35. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.