// Namespace:
Using Microsoft. Office. InterOP. word;
// Define the packaging operation class
Public class wordcommand: idisposable
{
Public object missing = system. type. missing;
Public object true = true;
Public object false = false;
Public document wdoc = NULL;
Public Application wapp = NULL;
Private bool fromtemplate;
Private string filename;
Private delegate void outstreamhander ();
Private event outstreamhander;
Public wordcommand (string sfilefullname, bool bfromtemplate)
{
Filename = sfilefullname;
Fromtemplate = bfromtemplate;
}
Public void open ()
{
If (! File. exists (filename ))
{
Throw new filenotfoundexception ("the file does not exist! ");
}
Else if (wdoc! = NULL)
{
Throw new exception ("the document has been opened! ");
}
Else
{
Wapp = new applicationclass ();
Wapp. Options. checkspellingasyoutype = false;
Wapp. Options. checkgrammarasyoutype = false;
Object ofilename = filename;
If (fromtemplate)
{
Wdoc = wapp. Documents. Add (ref ofilename, ref missing );
}
Else
{
Wdoc = wapp. Documents. Open (ref ofilename, ref missing, ref false, ref missing,
Ref missing, ref missing, ref true, ref missing, ref missing,
Ref missing, ref missing );
}
Wapp. selection. range. Text = string. empty;
}
}
Public String getcontent ()
{
Return wdoc. content. text;
}
Public String getcontent (string serverpath, string virtualpath)
{
List <Microsoft. Office. InterOP. Word. Range> ranges = new list <Microsoft. Office. InterOP. Word. Range> ();
List <string> files = new list <string> ();
Foreach (Microsoft. Office. InterOP. Word. inlineshape s in wdoc. inlineshapes)
{
If (S. type = Microsoft. Office. InterOP. Word. wdinlineshapetype. wdinlineshapepicture
| S. type = Microsoft. Office. InterOP. Word. wdinlineshapetype. wdinlineshapeembeddedoleobject)
{
// Obtain image data
Byte [] imgdata = (byte []) S. range. enhmetafilebits;
String file = string. Concat (guid. newguid (). tostring (), ". GIF ");
Files. Add (string. Join (",", new string [] {file, S. Height. tostring (), S. Width. tostring ()}));
// Construct the image
Memorystream mstream = new memorystream (imgdata );
Bitmap BMP = new Bitmap (mstream );
// Save to disk
BMP. Save (string. Concat (serverpath, "//", file ));
Mstream. Dispose ();
BMP. Dispose ();
Ranges. Add (S. Range );
S. Delete ();
}
}
For (INT I = 0; I <ranges. Count; I ++)
{
Microsoft. Office. InterOP. Word. Range r = ranges [I];
String [] parameters = files [I]. Split (',');
// Replace the image
R. insertbefore (" 0) This. style. zoom = zoom + '%'; return false;/"/> ");
}
Return wdoc. content. text;
}
Public void savetostream (Stream)
{
String sfilename = path. gettempfilename ();
If (fromtemplate)
{
Saveas (sfilename );
String stmpname = path. gettempfilename ();
Saveas (stmpname );
Using (filestream FS = file. openread (sfilename ))
{
Int ilength = (INT) fs. length;
Byte [] bys = new byte [ilength];
FS. Read (bys, 0, ilength );
Stream. Write (bys, 0, ilength );
}
File. Delete (sfilename );
Filename = stmpname;
Fromtemplate = true;
Outstreamhander = new outstreamhander (wordcommand_outstreamhander );
}
Else
{
// Save it to the temporary directory
Saveas (sfilename );
Saveas (filename );
Using (filestream FS = file. openread (sfilename ))
{
Int ilength = (INT) fs. length;
Byte [] bys = new byte [ilength];
FS. Read (bys, 0, ilength );
Stream. Write (bys, 0, ilength );
}
File. Delete (sfilename );
}
}
Private void wordcommand_outstreamhander ()
{
File. Delete (filename );
}
Public void dispose ()
{
If (wdoc! = NULL)
{
System. runtime. interopservices. Marshal. releasecomobject (wdoc );
Wdoc = NULL;
}
If (wapp! = NULL)
{
System. runtime. interopservices. Marshal. releasecomobject (wapp );
Wapp = NULL;
}
}
}
// Data entity definition
Private class wordinlineshape
{
Private float width;
Public float width
{
Get {return width ;}
Set {width = value ;}
}
Private float height;
Public float height
{
Get {return height ;}
Set {Height = value ;}
}
Private string file;
Public String File
{
Get {return file ;}
Set {file = value ;}
}
Private string ID;
Public String ID
{
Get {return ID ;}
Set {id = value ;}
}
Public wordinlineshape (string ID, float width, float height, string file)
{
This. width = width;
This. Height = height;
This. File = file;
This. ID = ID;
}
}
// Core operation code:
// Obtain the document path
String template = resourcehelper. paperexporttemplateurl;
String address = server. mappath (Template );
Wordcommand comm = new wordcommand (address, false );
Try
{
Comm. open ();
Comm. wdoc. content. Text = bizlogic. prepareexportpapercontents (paperid );
// Reverse processing image
Hashtable inlineshapes = hashtable. synchronized (New hashtable ());
String Pattern = @ " ";
RegEx r = new RegEx (pattern, regexoptions. ignorecase | regexoptions. Compiled );
Match m = NULL;
Int idnumber = 1;
// Get all the matching images
For (M = R. Match (Comm. wdoc. content. Text); M. success; M = M. nextmatch ())
{
If (! String. isnullorempty (M. Value ))
{
// Obtain the image address
String srcpattern = @ " ";
String heightpattern = @ " ";
String widthpattern = @ " ";
RegEx xreg = new RegEx (srcpattern, regexoptions. ignorecase | regexoptions. Compiled );
String srcurl = xreg. Match (M. Value). Groups [1]. value;
Xreg = new RegEx (heightpattern, regexoptions. ignorecase | regexoptions. Compiled );
String Height = xreg. Match (M. Value). Groups [1]. value;
Xreg = new RegEx (widthpattern, regexoptions. ignorecase | regexoptions. Compiled );
String width = xreg. Match (M. Value). Groups [1]. value;
// Convert the image address
String file = resourcehelper. mapfile (srcurl );
String id = string. Concat (" ");
Comm. wdoc. content. Text = comm. wdoc. content. Text. Replace (M. Value, id. tostring ());
Float width = float. parse (width );
Float Height = float. parse (height );
// Add to set
Wordinlineshape inlineshape = new wordinlineshape (ID, width, height, file );
Inlineshapes [ID] = inlineshape;
// Auto-Increment
Idnumber + = 1;
}
}
For (M = R. Match (Comm. wdoc. content. Text); M. success; M = M. nextmatch ())
{
If (! String. isnullorempty (M. Value ))
{
Wordinlineshape inlineshape = (wordinlineshape) inlineshapes [M. value];
// Generate an OLE object
String filename = inlineshape. file;
Object linktofile = false;
Object savewithdocument = true;
// Insert an operation to the anchor
Int Index = comm. wdoc. content. Text. indexof (M. value );
Object start = index;
Object end = index;
Microsoft. Office. InterOP. Word. Range = comm. wdoc. Range (ref start, ref end );
Object anchor = range;
// Insert an image
Comm. wdoc. application. activedocument. inlineshapes. addpicture (filename, ref linktofile, ref savewithdocument, ref anchor );
Comm. wdoc. application. activedocument. inlineshapes [1]. width = inlineshape. width;
Comm. wdoc. application. activedocument. inlineshapes [1]. Height = inlineshape. height;
}
}
Comm. savetostream (response. outputstream );
String filename = @ "paper.doc ";
Response. contenttype = "application/vnd. MS-word ";
Response. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
Response. charset = "gb2312 ";
Response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (filename ));
Response. End ();
}
Catch (exception expt)
{
Showerror (expt. Message );
}
Finally
{
Comm. Close ();
}