WPF reads and displays word

Source: Internet
Author: User

In project development, word Reading and display often appear in customer requirements. In particular, some laws, regulations, notices, and red-headed documents are all published in word.

In WPF, there are no specific controls for displaying WORD, which makes it more difficult to develop and display WORD. In fact, you only need to change your mind and solve the problem.

That's right. It's XPS. XPS is short for XML Paper Specification (XML File Specification). It is an electronic file format and is a document storage and viewing Specification developed by Microsoft .. . NET platform provides support for operating the XPS file format. In WPF, you can use DocumentViewer to browse XPS files. In this case, you only need to convert word to XPS, and then use DocumentViewer to display it. The problem is well solved. The following is a method for converting word to xps. You need to reference the Microsoft. Office. Interop. Word component.

  XpsDocument ConvertWordToXPS(              FileInfo fi=             XpsDocument result =               xpsDocName =             xpsDocName = xpsDocName.Replace(, ).Replace(,              Microsoft.Office.Interop.Word.Application wordApplication =                                 (!                       Document doc =                     doc.ExportAsFixedFormat(xpsDocName, WdExportFormat.wdExportFormatXPS, , WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, , , WdExportItem.wdExportDocumentContent, , , WdExportCreateBookmarks.wdExportCreateHeadingBookmarks, , ,                      result =                                          result =                                    error =                           }

The above method passes a word file path, and then returns an XpsDocument. The method is to store the XPS file in a folder, because XPS is used as an intermediate conversion and does not need to be stored or used. Method To return XpsDocument, you can use the following two lines of code to display the interface:

docViewer.Document =

In this way, a Word document is easy to display!

Download source code:

1. It only shows WORD. Can it display PDF and txt? The answer is yes. Similarly, you only need to Convert PDF and txt to XPS. PDF conversion to XPS, you can refer to this article: Link. Using Adobe Reader and Microsoft XPS to print by default, and then saving it as a method is a bit troublesome. In fact, there are more effective methods. Here we will leave it for everyone to think about and google.

2. The XPS above is only used as an intermediate conversion. Can it be saved so that the next conversion is unnecessary and can be directly read? Of course! There are two ways to save XPS. One is to save it to a specified directory as a file, and the other is to save it to a database. For saving files, I will not introduce them much. How can XPS be saved to the database? In fact, XPS is a binary stream format, which is the same as storing images in the database. Therefore, you only need to read the XPS file as a byte type and save it to the image field in the database. When displayed, read and generate XPS directly from the database to display it on DocumentViewer. But there is a drawback: When the file is converted to XPS, the file will become larger. If it is saved, it will be a waste of space.

Thank you!

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.