DevExpress allows you to preview and process PDF, Word, and Excel documents, and devexpressexcel.

Source: Internet
Author: User

DevExpress allows you to preview and process PDF, Word, and Excel documents, and devexpressexcel.

In the general management system module, more and more common document upload and storage operations are designed, such as PDF, Word, Excel and other documents, sometimes the distributed WCF technology is used to display and process data. Therefore, we hope to preview the data directly without downloading the file, which provides us with a lot of convenience. In DevExpress, corresponding controls are provided to display and process these documents. This article describes how to use the DevExpress control to preview and process PDF, Word, and Excel documents.

1. PDF preview and operations

In the earlier DevExpress control, the corresponding PDF document display control has been provided. However, due to its poor support for the PDF format, some documents are exported by Office, the DevExpress PDF Display Control described in this article is based on 14.1. It seems that many documents have been tested and can be opened normally, therefore, it is also intended to be widely used in the system.

To demonstrate how to process these controls, I wrote an example to process documents such as PDF, Word, and Excel.

To display the PDF document, we need to add an XtraPdfViewer in the interface. the PdfViewer control, which is mainly used to display PDF files. It has many attribute methods for processing PDF files. The test interface is designed as follows.

PDF is generally used to open a file, save it as, or preview it. The related operation code is as follows.

/// <Summary> /// test the display Form in PDF /// </summary> public partial class lateral Viewer: Form {// record the Form name readonly string mainFormText; public writable Viewer () {InitializeComponent (); // record the form name and handle document change events to display the new file name mainFormText = this. text; required viewer1.documentchanged + = new DevExpress. xtraPdfViewer. export documentchangedeventhandler (export viewer1_documentchanged);} // <summary> // After the PDF document changes, display the new file name /// </summary> Void upload viewer1_documentchanged (object sender, DevExpress. xtraPdfViewer. required documentchangedeventargs e) {string fileName = Path. getFileName (e. documentFilePath); if (String. isNullOrEmpty (fileName) {Text = mainFormText;} else {Text = fileName + "-" + mainFormText ;}} /// <summary> /// open the PDF file /// </summary> private void btnOpenFile_Click (object sender, EventArgs e) {string filePath = FileDi AlogHelper. OpenPdf (); if (! String. isNullOrEmpty (filePath) {this.pdf Viewer1.LoadDocument (filePath) ;}/// <summary> // save as a PDF file /// </summary> private void btnSaveAs_Click (object sender, eventArgs e) {string dir = System. environment. currentDirectory; string filePath = FileDialogHelper. savePdf ("", dir); if (! String. isNullOrEmpty (filePath) {try {this.pdf Viewer1.SaveDocument (filePath); MessageUtil. showTips ("saved successfully");} catch (Exception ex) {LogTextHelper. error (ex); MessageUtil. showError (ex. message) ;}}/// <summary> // print the PDF file /// </summary> private void btnPreview_Click (object sender, EventArgs e) {this.pdf Viewer1.Print ();}}

From the code above, we can see that it is very convenient to operate PDF files, mainly after loading files on the interface, we can perform related operations on the PDFViewer object.

 

2. Preview and operate Word documents

Similar to a PDF document, we use RichEditControl to display a document. However, unlike the document viewer, this control allows you to modify and save a document, this makes it easy for users to edit documents.

The interface for the test example is as follows.

The related operation code is similar to the PDF operation. The difference is that after the document changes, it cannot easily obtain the path of the corresponding document from the parameter, special processing is required.

/// <Summary> // test example of the WORD Control /// </summary> public partial class WordViewer: Form {// record the Form name readonly string mainFormText; public WordViewer () {InitializeComponent (); // record the form name and handle document change events to display the new file name mainFormText = this. text; this. richEditControl1.DocumentLoaded + = new EventHandler (richeditcontrol+documentloaded);} // <summary> // after the word document changes, display the name of the new file /// </summary> void richEditControl1 _ DocumentLoaded (object sender, EventArgs e) {string fileName = Path. getFileName (this. richEditControl1.Options. documentSaveOptions. currentFileName); if (String. isNullOrEmpty (fileName) {Text = mainFormText;} else {Text = fileName + "-" + mainFormText;} // modify the default font; entrange range = richEditControl1.Document. range; CharacterProperties cp = this. richEditControl1.Document. beginUpdateCharacters (Range); cp. fontName = "" "; // cp. fontSize = 12; this. richEditControl1.Document. endUpdateCharacters (cp);} // <summary> // open the WORD file /// </summary> private void btnOpenFile_Click (object sender, EventArgs e) {string filePath = FileDialogHelper. openWord (); if (! String. isNullOrEmpty (filePath) {richEditControl1.LoadDocument (filePath); //, DocumentFormat. doc); }}/// <summary> // Save the WORD file // </summary> private void btnSaveFile_Click (object sender, EventArgs e) {this. richEditControl1.SaveDocument ();} // <summary> // save it as a WORD file /// </summary> private void btnSaveAs_Click (object sender, EventArgs e) {try {richEditControl1.SaveDocumentAs (); MessageUtil. showTips ("saved successfully");} catch (Exception ex) {LogTextHelper. error (ex); MessageUtil. showError (ex. message) ;}/// <summary> // print the WORD file /// </summary> private void btnPreview_Click (object sender, EventArgs e) {this. richEditControl1.ShowPrintPreview ();}}

After the document is loaded, the page displays the following content:

The document control easily supports the print preview function. The print preview interface is as follows:

However, although RichEditControl can better display Word documents, some fonts are not displayed very well. The format is not the same as that of Microsoft wordstore, and the format is lost.

Therefore, if you have strict format requirements, it is recommended that you do not save the original documents. If the format is not strict, you can create and save documents as a document server.

 

3. Excel document preview and operations

For Excel document preview and operations, the XtraSpreadsheet added by the DevExpress control in the latest version. the SpreadsheetControl control can be used to display and process Excel files. This control is very powerful and can process Excel files in complicated formats. Although I used another FarPoint Spread control group, however, this XtraSpreadsheet control group can be easily integrated in DevExpress.

This DevExpress control can be used to create, save, print, and preview an Excel file. Of course, you can also open an existing Excel file.

After opening the file, the interface effect is as follows.

The operation code of the interface is as follows.

/// <Summary> /// Excel control test example /// </summary> public partial class ExcelViewer: Form {// record the Form name readonly string mainFormText; public ExcelViewer () {InitializeComponent (); // record the form name and handle document change events to display the new file name mainFormText = this. text; this. spreadsheetControl1.DocumentLoaded + = new EventHandler (spreadsheetcontrol+documentloaded);} // <summary> // after the document changes, display the new file name /// </summary> void spreadsheetC Ontrolw.documentloaded (object sender, EventArgs e) {string fileName = Path. getFileName (this. spreadsheetControl1.Document. path); if (String. isNullOrEmpty (fileName) {Text = mainFormText;} else {Text = fileName + "-" + mainFormText ;}} /// <summary> /// open the Excel file /// </summary> private void btnOpenFile_Click (object sender, EventArgs e) {string filePath = FileDialogHelper. openExcel (); if (! String. isNullOrEmpty (filePath) {IWorkbook workbook = spreadsheetControl1.Document; workbook. loadDocument (filePath) ;}/// <summary> // Save the Excel file /// </summary> private void btnSaveFile_Click (object sender, EventArgs e) {spreadsheetControl1.SaveDocument () ;}/// <summary> // save as an Excel file /// </summary> private void btnSaveAs_Click (object sender, EventArgs e) {string dir = System. environment. cu RrentDirectory; string filePath = FileDialogHelper. SaveExcel ("", dir); if (! String. isNullOrEmpty (filePath) {try {IWorkbook workbook = spreadsheetControl1.Document; workbook. saveDocument (filePath); MessageUtil. showTips ("saved successfully");} catch (Exception ex) {LogTextHelper. error (ex); MessageUtil. showError (ex. message) ;}}/// <summary> // print the Excel file /// </summary> private void btnPreview_Click (object sender, EventArgs e) {this. spreadsheetControl1.ShowPrintPreview ();}}

 

Previewing is also very convenient, similar to previewing Word.

 

The above are the display and operation cases of several common documents. With these examples, we can easily integrate them into our attachment management.

For example, the general attachment management module I used in my Winform development framework and hybrid development framework is based on these features, you can preview and manage images, Excel documents, Word documents, and PDF files online, as shown in the following figure.

 

Related Article

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.