Asp. NET control different people edit different editable regions in a Word document

Source: Internet
Author: User

Asp. NET control different people edit a complete example of different editable regions in a Word document

2010-10-15 11:43,238 People read review (0) Favorite Report

Page Source: http://blog.csdn.net/coco99/article/details/5942895

This article demonstrates how to use C # to call word in ASP. Limit the user to edit only the areas in the Word document that they have permission to edit.

1. Purpose of the project

The demo uses different users to log on to the system, open the same file (without having to open it at the same time), can edit the area differently, everyone has their own editing area.

2. Solution Ideas

Implemented using the method provided by the Wordresponse object of the Soaoffice. About Soaoffice Here are just a few simple words:

Soaoffice can be used to edit online, save the document on the server (of course, this is the basic function), but also support handwriting, set broken, stamping (and the relationship between this article is not very elaborate); Here's a little bit more about the Soaoffice support for bookmark assignment in Word.

First understand the concept "data region (DataRegion)" In a soaoffice:

Data region (dataregion)-a bookmark that begins with "Soa_" in the concept of soaoffice. Because the Wordresponse object provides a data range defined by the Definedataregion method, inserting into a Word template is the bookmark that begins with "Soa_", so this data region is just a concept in soaoffice, essentially "Soa_" The beginning of the bookmark. DataRegion has two properties:

Value: Assigns a value to the data region, for example: Word file has a bookmark: Soa_name, then use Wordresponse object to assign value to this bookmark, opendataregion ("Name"). Value = "XXX"; Note You do not need to write "Soa_" at the beginning.

Needsubmit: Set this data area is not editable, submit (submit for this said function has nothing to do with it). Opendataregion ("Name"). Needsubmit=true, then bookmarks Soa_name can be edited.

This needsubmit attribute is the focus of this use, because the Wordresponse object's Formmode = True, only the data area of Needsubmit = True is editable, not set to Needsubmit = True data regions and other body contents are read-only.

The key to implementing different editing areas in a Word document is to use bookmarks in Word to mark the editing area (data region), and then use the Needsubmit property of the data region to control whether the area is editable. So we want to make sure that the server-side. NET component Soaofficex that references the Soaoffice middleware. In this example, SOAOfficeX.dll is placed under the Bin folder.

3. Key Steps

String userName = request.form["user"]; Suppose this is your logged-on user

Soaofficex.wordresponse Soaword = new Soaofficex.wordresponse ();

Soaword.formmode = true; Set Formmode = True

Soaword.disablewindowrightclick = true;

Different editable regions are set depending on the user username logged in

if (UserName = = "Zhangsan")

{

Soaofficex.iwordresdataregion dataregion = soaword.opendataregion ("Zhangsan1");

Dataregion.needsubmit = true;

DataRegion = Soaword.opendataregion ("zhangsan2");

Dataregion.needsubmit = true;

}

if (UserName = = "Lisi")

{

Soaofficex.iwordresdataregion dataregion = soaword.opendataregion ("Lisi1");

Dataregion.needsubmit = true;

DataRegion = Soaword.opendataregion ("Lisi2");

Dataregion.needsubmit = true;

}

4. Sample program Architecture

----default.aspx//Demo login page

----Simple.aspx.cs//Open File

----savedoc.aspx//page where the document is saved

doc///directory where documents are stored

5. Code Writing

The first is to implement the document online editing function page: word1.aspx. In order to realize the online editing function of the document, we need to install the Soaoffice client first, because we need to save the document in the time of saving the edited document content into the database, so we need to disable Word's own "save" function, the use of custom "save" function. The foreground HTML adds the following code:

*******************************************************************************

<!--client code starts--

<script language= "JavaScript" event= "OnInit ()" for= "Soaofficectrl" >

Control triggers before opening a document, used to initialize the interface style

</SCRIPT>

<script language= "JavaScript" event= "ondocumentopened (str, obj)" for= "Soaofficectrl" >

Control triggers immediately after opening a document, adding custom menus, customizing toolbars, prohibiting printing, preventing save, saving, etc.

Bdocopen = true;

Soaofficectrl.appendtoolbutton (1, "Save", 1);

</SCRIPT>

<script language= "JavaScript" event= "ondocumentclosed ()" for= "Soaofficectrl" >

Bdocopen = false;

</SCRIPT>

<script language= "JavaScript" event= "Onusermenuclick (Index, caption)" for= "Soaofficectrl" >

Add your custom menu item event response

</SCRIPT>

<script language= "JavaScript" event= "Oncustomtoolbarclick (Index, caption)" for= "Soaofficectrl" >

Add your custom toolbar button event response

if (index==1) Soaofficectrl.websave ();

</SCRIPT>

<!--object tag, client control reference--

<object id= "Soaofficectrl" codebase= "soaoffice.ocx#version=8,1,0,6" height= "100%" width= "100%"

Data= "" classid= "clsid:83171ba4-bdca-42a8-be44-745abf36eb7e" viewastext>

<div align= "center" style= "color:red;" >

The Soaoffice client control is not installed on this machine, please install the Soaoffice client control in the yellow cue bar above the browser or in the pop-up prompt box. </div>

</OBJECT>

<!--client code ends-

*******************************************************************************

The specific code is as follows:

<%@ page language= "C #" autoeventwireup= "true" codefile= "Simple.aspx.cs" inherits= "_default"%>

<title> Thin Code </title>

<body>

<form id= "FormData" runat= "Server" >

<font face= "Song Body" >

<!--************** Soaoffice client code starts ************************-->

<script language= "JavaScript" event= "OnInit ()" for= "Soaofficectrl" >

Control triggers before opening a document, used to initialize the interface style

</SCRIPT>

<script language= "JavaScript" event= "ondocumentopened (str, obj)" for= "Soaofficectrl" >

Control triggers immediately after opening a document, adding custom menus, customizing toolbars, prohibiting printing, preventing save, saving, etc.

Bdocopen = true;

Soaofficectrl.appendtoolbutton (1, "Save", 1);

</SCRIPT>

<script language= "JavaScript" event= "ondocumentclosed ()" for= "Soaofficectrl" >

Bdocopen = false;

</SCRIPT>

<script language= "JavaScript" event= "Onusermenuclick (Index, caption)" for= "Soaofficectrl" >

Add your custom menu item event response

</SCRIPT>

<script language= "JavaScript" event= "Oncustomtoolbarclick (Index, caption)" for= "Soaofficectrl" >

Add your custom toolbar button event response

if (index==1) Soaofficectrl.websave ();

</SCRIPT>

<!--object tag, client control reference--

<object id= "Soaofficectrl" codebase= "soaoffice.ocx#version=8,1,0,6" height= "100%" width= "100%"

Data= "" classid= "clsid:83171ba4-bdca-42a8-be44-745abf36eb7e" viewastext>

<div align= "center" style= "color:red;" >

The Soaoffice client control is not installed on this machine, please install the Soaoffice client control in the yellow cue bar above the browser or in the pop-up prompt box. </div>

</OBJECT>

<!--************** Soaoffice client code end ************************-->

</FONT>

</form>

</body>

Next is the simple.aspx code Simple.aspx.cs, because to take advantage of the Soaoffice middleware, so we need to add server-side code inside it, mainly to set up the opening of the document and set the editable area. The server-side code is as follows:

*******************************************************************************

--------------Soaoffice server-side programming begins---------------------------//

First, make sure to reference the server-side. NET component of Soaoffice middleware Soaofficex

SOAOfficeX.dll in the Bin folder of this sample code

String userName = request.form["user"];

Soaofficex.wordresponse Soaword = new Soaofficex.wordresponse ();

Soaword.formmode = true;

Soaword.disablewindowrightclick = true;

if (UserName = = "Zhangsan")

{

Soaofficex.iwordresdataregion dataregion = soaword.opendataregion ("Zhangsan1");

Dataregion.needsubmit = true;

DataRegion = Soaword.opendataregion ("zhangsan2");

Dataregion.needsubmit = true;

}

if (UserName = = "Lisi")

{

Soaofficex.iwordresdataregion dataregion = soaword.opendataregion ("Lisi1");

Dataregion.needsubmit = true;

DataRegion = Soaword.opendataregion ("Lisi2");

Dataregion.needsubmit = true;

}

Soaofficex.soaofficectrl Soactrl = new Soaofficex.soaofficectrl ();

Set up the Soaoffice Middleware Service page

Soactrl.serverurl = "soaservice/soaserv.aspx";

Soactrl.savedocurl = "savedoc.aspx";

Soactrl.menubarstyle = SOAOfficeX.soaMenubarStyle.Center1;

Soactrl.assign (Soaword);

Soactrl.toolbars = false;

Soactrl.webopen ("Doc/test.doc", SOAOfficeX.soaWorkMode.docNoRevision, "User name", "Word.Document");

--------------Soaoffice End-of-server programming---------------------------//

*******************************************************************************

Savedoc.aspx the code of the page, the foreground does not need any code, the background code is as follows: ***************************************************************************** **

Soaofficex.savedocobj soaobj = new Soaofficex.savedocobj ();

Soaobj.savetofile (Server.MapPath ("doc//") + Soaobj.filename);

Soaobj.returnok ();

Asp. NET control different people edit different editable regions in a Word document

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.