C # convert word, pdf, and ppt files into images,

Source: Internet
Author: User

C # convert word, pdf, and ppt files into images,

Office Word documents, pdf documents, and powerpoint slides are commonly used document types, in reality, it is often necessary to convert them into images-converting every page of word, pdf, and ppt documents into a corresponding image, just as printing these documents first, and then scan the image. Therefore, similar tools that convert word, pdf, and ppt into pictures are generally referred to as "electronic scanners" and are very high-end names!

1. Which scenarios need to convert word, pdf, and ppt into images?

In my understanding, there are usually three scenarios that require converting word, pdf, and ppt documents into images.

1. Prevent documents from being plagiarized by others

If you directly provide word, pdf, and ppt documents to others, others can easily Copy the content of the entire document, and then make some modifications to become their own things.

If we convert a document into an image and then provide it to others, plagiarism is not just as easy as copying it.

2. Save paper

In the past, in order to better achieve the 1st point, documents were printed out for others to read, and many documents were not used once. Therefore, a lot of paper, ink, and printer and power consumption were wasted.

Today, when we advocate low-carbon energy-saving, the use of electronic scanners is even more significant.

3. electronic whiteboard Courseware

In systems such as online teaching and remote training, the use of courseware (word, pdf, ppt, and other types of documents) is a basic requirement, the combination of courseware and electronic whiteboard is generally like this: the courseware is converted into an image, each page of the document corresponds to each page of the electronic whiteboard, it is the background image of the whiteboard page. In this way, the teacher can mark and blackboard on the courseware image. Some time ago, we studied word, pdf, and ppt document conversion technology to provide a Demo of extended courseware type for the OMCS whiteboard function, make it easy to support word, pdf, and ppt courseware.

Ii. How to convert?

Q: du Niang can find many articles similar to converting word into images. However, there are not many useful articles. filtering is a time-consuming process. Here, we directly present the filtering results and encapsulate them into a Class that can be reused directly, saving time for people who need the same in the future.

1. solution 1: use the Office COM Component

(This solution does not support PDF documents. Here is a good summary of the method for converting PDF files to images. We recommend 13 solutions for converting PDF files to images)

The requirement of this solution is that Microsoft Office must be installed on your computer. We can operate Office documents through Interop between. NET and Office COM components.

The principle of this solution is as follows: through COM interoperability, you can open Office documents in the memory, and then access every page of the documents, in addition, you can copy any page of content to the clipboard (in the form of an image). In this way, you can save the content on the clipboard as an image.

The principle is very simple, and the implementation code is a little troublesome, as shown below:

COM Solution

The above implementation is very useful for small Word documents. However, if the Word documents are large and contain many pages, the above calls will occupy a large amount of memory.

If this is the case, you can rewrite the above implementation. If you don't get a page of image, save it to the hard disk instead of saving it in the memory.

The same COM method is used to convert a PPT to an image. At the end of this article, we will download the class implemented by COM of word and ppt to an image.

2. solution 2: Use the Aspose component

The advantage of using the Aspose component is that you do not need to install the Office on your machine or complete the functions we want. This advantage is so obvious that it is the most recommended solution. In addition, Aspose supports word, ppt, pdf, and even excel.

In the Demo that demonstrates how to extend the OMCS whiteboard courseware type, we use the Aspose component, which is stable and easy to use. The following code is taken from the Demo.

Public class Word2ImageConverter: IImageConverter {private bool canceled = false; public event CbGeneric <int, int> ProgressChanged; public event CbGeneric ConvertSucceed; public event CbGeneric <string> ConvertFailed; public void Cancel () {if (this. canceled) {return;} this. canceled = true;} public void ConvertToImage (string originFilePath, string imageOutputDirPath) {this. canceled = false; ConvertToImage (originFilePath, imageOutputDirPath, 0, 0, null, 200 );} /// <summary> /// convert a Word document to an image // </summary> /// <param name = "wordInputPath"> Word file path </param>/ // <param name = "imageOutputDirPath"> image output path, if it is null, the default value is the path where the Word is located </param> // <param name = "startPageNum"> the conversion starts from the page of the PDF document. If it is 0, the default value is 1 </param> /// <param name = "endPageNum"> the conversion starts from the page number of the PDF document. If it is 0, the default value is the total number of pages in Word </param> /// <param name = "imageForm At "> sets the image format. If it is null, the default format is PNG </param> /// <param name =" resolution "> sets the image pixels, the bigger the number, the clearer it is. If it is 0, the default value is 128. We recommend that the maximum value not exceed 1024 </param> private void ConvertToImage (string wordInputPath, string imageOutputDirPath, int startPageNum, int endPageNum, imageFormat imageFormat, int resolution) {try {Aspose. words. document doc = new Aspose. words. document (wordInputPath); if (doc = null) {throw new Exception ("Word file is invalid or Wo The rd file is encrypted! ");} If (imageOutputDirPath. Trim (). Length = 0) {imageOutputDirPath = Path. GetDirectoryName (wordInputPath);} if (! Directory. exists (imageOutputDirPath) {Directory. createDirectory (imageOutputDirPath);} if (startPageNum <= 0) {startPageNum = 1;} if (endPageNum> doc. pageCount | endPageNum <= 0) {endPageNum = doc. pageCount;} if (startPageNum> endPageNum) {int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum;} if (imageFormat = null) {imageFormat = ImageFormat. png;} if (resolution <= 0) {resolution = 128;} string imageName = Path. getFileNameWithoutExtension (wordInputPath); ImageSaveOptions imageSaveOptions = new ImageSaveOptions (SaveFormat. png); imageSaveOptions. resolution = resolution; for (int I = startPageNum; I <= endPageNum; I ++) {if (this. canceled) {break;} MemoryStream stream = new MemoryStream (); imageSaveOptions. pageIndex = I-1; string imgPath = Path. combine (imageOutputDirPath, imageName) + "_" + I. toString ("000") + ". "+ imageFormat. toString ();Doc. Save (stream, imageSaveOptions );Image img = Image. fromStream (stream); Bitmap bm = ESBasic. helpers. imageHelper. zoom (img, 0.6f); bm. save (imgPath, imageFormat); img. dispose (); stream. dispose (); bm. dispose (); System. threading. thread. sleep (200); if (this. progressChanged! = Null) {this. ProgressChanged (I-1, endPageNum) ;}} if (this. canceled) {return ;}if (this. ConvertSucceed! = Null) {this. ConvertSucceed () ;}} catch (Exception ex) {if (this. ConvertFailed! = Null) {this. ConvertFailed (ex. Message );}}}}

The code is quite concise. In the source code, we provide Word2ImageConverter, 4102imageconverter, and Ppt2ImageConverter for conversion from Word documents, pdf documents, ppt slides to images.

Note that Aspose does not directly provide an API for converting ppt files to images. However, Aspose provides the function for converting ppt files to pdf files, in the source code, the conversion of ppt files to images passes through pdf, that is, converting the ppt file into a pdf file first, and then converting the pdf file into a graph

Iii. Code download

1. solution 1 code download

Solution 1 uses Office COM for interoperability and supports converting Word documents and ppt documents into images. Download the class source code:

Officedetail. cs

2. solution 2 code download

The source code of solution 2 can be extracted from our demo (the ImageConverters. cs file in the client project ). Our demo is used to simulate the online education system: a teacher and N students enter the same classroom, so they will see the same whiteboard. Teachers can upload courseware, open courseware, Mark On Whiteboard courseware, blackboard writing, etc. When opening the courseware, this Demo uses the above function to convert word, pdf, and ppt into pictures. You can run the demo to see the specific effect.

Whiteboard courseware Demo

When running the Demo for testing, follow the steps below:

(1) Start the OMCS server.

(2) start the first client, select the "teacher" role, and log on to the default classroom.

(3) Start Multiple clients, select the "student" role, and log on to the default classroom.

(4) instructors can upload courseware, open courseware, delete courseware, flip courseware pages, and Mark and write courseware.

Instructor-Side Running interface:

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.