EventHandle Word conversion for MOSS XPS

Source: Internet
Author: User

Reprinted please indicate the location: www.cnblogs.com/putishu bodhi tree qq: 43094723

 

Main ideas:

To add an event to be processed in ItemAdded, follow these steps:

 

1. Obtain the current document and the hard drive path stored on the server;

2. Convert the word on the current hard disk path to xps;

3. Upload data to the moss document library through a binary stream.

 

No more. paste the Code directly!

 

 

 

 

Using System;
Using System. Collections. Generic;
Using System. Text;
Using Microsoft. SharePoint;
Using Word = Microsoft. Office. Interop. Word;
Using System. IO;
Using System. Web;
Using System. Configuration;

Namespace sinopec. LeaderPortal
{
Class Convert2XPS: SPItemEventReceiver
{
Public override void ItemAdded (SPItemEventProperties properties)
{
Base. ItemAdded (properties );
Using (SPSite site Site = new SPSite (properties. ListItem. Web. site. ID ))
{
Using (SPWeb = site. OpenWeb (properties. ListItem. web. ID ))
{
SPFile sf = properties. ListItem. File;
String path = string. Empty;
String FileName = String. Empty;
String testpath = string. Empty;

If (sf! = Null)
{

String appPath = GetPath ();
Testpath = sf. ParentFolder. ServerRelativeUrl;
FileName = sf. Name;
String savePath = ByteConvertWord (sf. OpenBinary (), FileName );
String flName = savePath. Substring (0, savePath. LastIndexOf ("."));
String targetFileName = flName + ". xps ";
String targetFileName2 = FileName. Substring (0, FileName. LastIndexOf (".") + ". xps ";
// Here the write is converted to doc/docx into xps
Convert (appPath + savePath, appPath + targetFileName, Word. WdExportFormat. wdExportFormatXPS );
System. IO. FileInfo myfile = new System. IO. FileInfo (appPath + savePath );
Byte [] fileContents = new byte [int. Parse (myfile. Length. ToString ()];
FileStream fs = File. OpenRead (appPath + targetFileName );
Int n = fs. Read (fileContents, 0, int. Parse (myfile. Length. ToString ()));
UploadDocument (targetFileName2, fileContents, @ "http: // win2003_blank/sites/zhenggongbu/wordxps /");

}
// SPListItem listItem = properties. ListItem;

// Louji

// ListItem ["Title"] = "dafaffsskj; afffds ";

// ListItem. Update ();

}
}
}

Public override void ItemAdding (SPItemEventProperties properties)
{
Base. ItemAdding (properties );
}

Public override void ItemUpdated (SPItemEventProperties properties)
{
Base. ItemUpdated (properties );
}

Public override void ItemUpdating (SPItemEventProperties properties)
{
Base. ItemUpdating (properties );
}

Public override void ItemAttachmentAdded (SPItemEventProperties properties)
{
Base. ItemAttachmentAdded (properties );

Using (SPSite site Site = new SPSite (properties. ListItem. Web. site. ID ))
{
Using (SPWeb = site. OpenWeb (properties. ListItem. web. ID ))
{
SPListItem listItem = properties. ListItem;

// Louji

ListItem ["Title"] = "dafaffsskj; afffds ";
ListItem. Update ();
}
}
}

Private bool Convert (string sourcePath, string targetPath, Microsoft. Office. Interop. Word. WdExportFormat exportFormat)
{
Bool result;
Object paramMissing = Type. Missing;
Word. ApplicationClass wordApplication = new Word. ApplicationClass ();
Word. Document wordDocument = null;
Try
{
Object paramSourceDocPath = sourcePath;
String paramExportFilePath = targetPath;

Word. WdExportFormat paramExportFormat = exportFormat;
Bool paramOpenAfterExport = false;
Word. WdExportOptimizeFor paramExportOptimizeFor =
Word. WdExportOptimizeFor. wdExportOptimizeForPrint;
Word. WdExportRange paramExportRange = Word. WdExportRange. wdExportAllDocument;
Int paramStartPage = 0;
Int paramEndPage = 0;
Word. WdExportItem paramExportItem = Word. WdExportItem. wdExportDocumentContent;
Bool paramdid dedocprops = true;
Bool paramkeepsert = true;
Word. WdExportCreateBookmarks paramCreateBookmarks =
Word. WdExportCreateBookmarks. wdExportCreateWordBookmarks;
Bool paramDocStructureTags = true;
Bool paramBitmapMissingFonts = true;
Bool paramUseISO19005_1 = false;

WordDocument = wordApplication. Documents. Open (
Ref paramSourceDocPath, ref paramMissing, ref paramMissing,
Ref paramMissing,
Ref paramMissing,
Ref paramMissing,
Ref paramMissing,
Ref paramMissing );

If (wordDocument! = Null)
WordDocument. ExportAsFixedFormat (paramExportFilePath,
ParamExportFormat, paramOpenAfterExport,
ParamExportOptimizeFor, paramExportRange, paramStartPage,
ParamEndPage, paramExportItem, paramIncludeDocProps,
Paramkeepsert, paramCreateBookmarks, paramDocStructureTags,
ParamBitmapMissingFonts, paramUseISO19005_1,
Ref paramMissing );
Result = true;
}
Finally
{
If (wordDocument! = Null)
{
WordDocument. Close (ref paramMissing, ref paramMissing, ref paramMissing );
WordDocument = null;
}
If (wordApplication! = Null)
{
WordApplication. Quit (ref paramMissing, ref paramMissing, ref paramMissing );
WordApplication = null;
}
GC. Collect ();
GC. WaitForPendingFinalizers ();
GC. Collect ();
GC. WaitForPendingFinalizers ();
}
Return result;
}

/// <Summary>
/// Convert binary data to a word file
/// </Summary>
/// <Param name = "data"> binary data </param>
/// <Param name = "fileName"> word file name </param>
/// <Returns> relative path of word storage </returns>
Public string ByteConvertWord (byte [] data, string fileName)
{
String savePath = @ "\ SystemWord \" + FormatNowTime (2) + @"\";
String appPath = GetPath ();
If (! System. IO. Directory. Exists (appPath + savePath ))
{
Directory. CreateDirectory (appPath + savePath );
}
SavePath + = fileName;
String filePath = appPath + savePath;

FileStream fs;
If (System. IO. File. Exists (filePath ))
{
Fs = new FileStream (filePath, FileMode. Truncate );
}
Else
{
Fs = new FileStream (filePath, FileMode. CreateNew );
}
BinaryWriter br = new BinaryWriter (fs );
Br. Write (data, 0, data. Length );
Br. Close ();
Fs. Close ();
Return savePath;
}

/// <Summary>
/// Convert binary data into a word file
/// </Summary>
/// <Param name = "wordPath"> word file path </param>
/// <Returns> binary </returns>
Private byte [] wordConvertByte (string wordPath)
{
Byte [] bytContent = null;
System. IO. FileStream fs = null;
System. IO. BinaryReader br = null;
Try
{
Fs = new FileStream (wordPath, System. IO. FileMode. Open );
}
Catch
{
}
Br = new BinaryReader (Stream) fs );
BytContent = br. ReadBytes (Int32) fs. Length );

Return bytContent;
}

/// <Summary>
/// Directory of the project
/// </Summary>
/// <Returns> </returns>
Public string GetPath ()
{
// Return Application. StartupPath;

String path = @ System. Configuration. ConfigurationManager. etettings ["xpspath"];
Return path;
}

Public string GetWebAddress ()
{
Return System. Configuration. ConfigurationManager. receivettings ["webaddr"];
}
/// <Summary>
/// Format the current time:
/// 1: yyMMddHHmmss; 2: yyyy-MM \ dd \
/// </Summary>
/// <Returns> </returns>
Public string FormatNowTime (int num)
{
If (num = 1)
{
Return DateTime. Now. ToString ("yyMMddHHmmss ");
}
Else if (num = 2)
{
Return DateTime. Now. ToString ("yyyy-MM") + @ "\" + DateTime. Now. Day;
}
Return "";
}

Public static string UploadDocument (string fileName, byte [] fileContents, string pathFolder)
{
If (fileContents = null)
{
Return "Null Attachment ";
}
Try
{

Int iStartIndex = pathFolder. LastIndexOf ("/");
String sitePath = pathFolder. Remove (iStartIndex );
String folderName = string. Empty;

If (sitePath. LastIndexOf ("/")> 6)
{
Int sec_iStartIndex = sitePath. LastIndexOf ("/");

SitePath = pathFolder. Remove (sitePath. LastIndexOf ("/"));
FolderName = pathFolder. Substring (sec_iStartIndex + 1 );

}

Else
{
FolderName = pathFolder. Substring (iStartIndex + 1 );

}
SPSite site = new SPSite (sitePath );
SPWeb web = site. OpenWeb ();


SPFolder folder = web. GetFolder (folderName );

String fileURL = fileName;

SPFile exFile = folder. Files. Add (fileURL, fileContents );

If (exFile! = Null)
{
ExFile. CheckIn ("File Checked In ");
}

Return "File added successfully! ";


}
Catch (System. Exception ex)
{
Return ex. Source + ":" + ex. Message;
}
}

}
}

 

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.