Translate

Sunday, September 10, 2017

Great article on the difference between IEnumerable and IQueryable in C#

I was playing around with those two lately because it causes me a terrible confusion, this article helps me to understand the difference between the two in a very abstract way .

http://haroldrv.com/2015/02/what-is-the-difference-between-ienumerable-and-iqueryable/



Monday, March 20, 2017

Convert word document to PDF file using Microsoft.Office.Interop.Word

You can use Microsoft word Api to convert word files (.doc, .docx) to a pdf files on the fly. if you are writing an automation tool or a system that manipulate word and pdf files you can utilize this API.

Microsoft.Office.Interop.Word assembly which can be found under the windows GAC folder is very easy to use and its performance are acceptable in some scenarios, so you need to study your case carefully before deciding using the component, because its uses unmanaged resources on the OS. and it have thread safety issues.


using System;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop;

public class WordToPdfConverter {

public void Convert(string sourceWordfile, string destinationPdffile)
        {
            Application word = new Application();
            if (string.IsNullOrEmpty(sourceWordfile))
            {
                throw new ArgumentNullException("sourceWordfile", "Source word file path must be passed.");
            }
            if (string.IsNullOrEmpty(destinationPdffile))
            {
                throw new ArgumentNullException("destinationPdffile", "Desitination Pdf file must be passed.");
            }
            try
            {
                word.Documents.Open(sourceWordfile);
                word.Application.Visible = false;
                word.WindowState = WdWindowState.wdWindowStateMinimize;
                Document document = word.ActiveDocument;
                document.SaveAs(destinationPdffile, WdSaveFormat.wdFormatPDF);
            }
            catch (Exception ex)
            {

                return
                    string.Format("An error occured during the conversion process, the message : {0} and the stack trace : {1}",
                        ex.Message, ex.StackTrace);
            }
            finally
            {
                word.Documents.Close();
                word.Quit(WdSaveOptions.wdDoNotSaveChanges);
            }
          
        }

}