SautinSoft blog

Articles about converting HTML, PDF, Word, RTF, Excel, images using C# and VB.Net

The simplest way to convert DOCX to a set of Images in .Net C# app

without comments

Hi Developers,

In this topic you will find a simplest solution to convert DOCX to Images(PNG, JPEG, TIFF, any) in C#.
To start please download these components:

  1. Download UseOffice .Net
  2. Download PDF Focus .Net
using System;
using System.IO;
using System.Collections;
 
namespace Sample
{
    class Program
    {
 
        static void Main(string[] args)
        {
            Console.WriteLine("Convert DOCX file to set of PNG images");
 
            string pathToDocx = @"D:\simple text.docx";
            // We need this pdf as bridge between docx and images.
            // We remove this pdf file after conversion
            string pathToPdf = Path.ChangeExtension(pathToDocx, ".pdf");
            string pathToImages = Path.GetDirectoryName(pathToDocx);
            string imagePageName = "Page";
            int result = -1;
 
            // 1. DOCX to PDF
            SautinSoft.UseOffice u = new SautinSoft.UseOffice();
            //Prepare UseOffice .Net, loads MS Word in memory
            if (u.InitWord() == 0)
            {
                result = u.ConvertFile(pathToDocx, pathToPdf, SautinSoft.UseOffice.eDirection.DOCX_to_PDF);
                //Release MS Word from memory
                u.CloseWord();
            }
            if (result != 0)
            {
                Console.WriteLine("Converting failed on stage DOCX to PDF!");
                Console.ReadLine();
                return;
            }
 
            // 2. PDF to Images
            SautinSoft.PdfFocus p = new SautinSoft.PdfFocus();
            p.OpenPdf(pathToPdf);
            if (p.PageCount > 0)
            {
                p.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
                p.ImageOptions.Dpi = 200;
                p.ImageOptions.ColorDepth = SautinSoft.PdfFocus.CImageOptions.eColorDepth.Rgb24bpp;
                p.ToImage(pathToImages, imagePageName);
                Console.WriteLine("{0} page(s) converted successfully!");
                System.Diagnostics.Process.Start(pathToImages);
            }
            else
            {
                Console.WriteLine("Converting failed on stage PDF to Images!");
                Console.ReadLine();
            }
 
        }
    }
}

Download code: DocxToImages.cs.

I hope this will be helpful to you, anyway if you need a C# code sample to convert between various documents, email me at: support@sautinsoft.com.

Best wishes,
Max (C# developer)
Share on Facebook

Written by Max Sautin

March 12th, 2014 at 2:54 pm

How to create HTML email with embedded images from RTF and send it using Outlook in .Net C#

without comments

In this article you will find how to create a simple .Net/C# application which converts RTF document to HTML email with embedded images and sends it using MS Outlook programmatically.

Let’s make a C# code satisfying of these conditions:

  1. Acceptable for any .Net application: ASP.NET, Windows Forms, WPF, Console, Web Service, SilverLight etc.
  2. Works at 32-bit and 64-bit Windows machines.
  3. Compatible with .NET 2.0, 3.5 , 4.0, 4.5 frameworks and even higher if such will appear.
To make converting of RTF to HTML email with images at server-side we’ll use RTF-to-HTML DLL .Net library. It’s .Net component which will provides API to convert RTF to HTML with all images which we’ll get in a list after converting. We need these images extracted from RTF to further adding them into email as attachments.
RTF to HTML DLL .Net provides API to convert RTF to HTML email in C#

RTF to HTML DLL .Net provides API to convert RTF to HTML email in C#

To get the ball rolling, download the RTF-to-HTML DLL .Net component from here: http://www.sautinsoft.com/components/rtftohtml_dll_net.zip.
Further, please unpack the archive. We’ll need the file “SautinSoft.RtfToHtml.dll” from the package.
Create a new C# Console Application named RtfToHtmlEmail. Next add references to the “SautinSoft.RtfToHtml.dll”, “Microsoft Outlook Library” and “Microsoft.CSharp” as shown on pictures below:
Reference to Microsoft.Outlook library

Reference to Microsoft.Outlook library

Microsoft.Outlook library

Microsoft.Outlook library

This is C# code of our application:
using System;
using System.IO;
using System.Collections;
using Outlook = Microsoft.Office.Interop.Outlook;
 
namespace RtfToHtmlEmail
{
    class Program
    {
        static void Main(string[] args)
        {
            // This application converts RTF file to HTML email with embedded images
            // and send the email using Outlook
 
            // Here we'll specify settings variables
            string pathToRtf = @"c:\Book.rtf";
            // We need this folder to store image attachments temporary.
            // Images will be deleted after sending the email.
            string pathToStoreTempAttachments = @"c:\temp";
            string from = "bob@bobsite.com";
            string to = "john@johnsite.com";
            string subject = "Testing message from Bob to John using Outlook";            
 
            // 1. Convert RTF to HTML and place all images to list
            string rtf = File.ReadAllText(pathToRtf);
            SautinSoft.RtfToHtml r = new SautinSoft.RtfToHtml();
            r.ImageStyle.IncludeImageInHtml = false;
            ArrayList imageList = new ArrayList();
 
            // 2. After launching this method we'll get our RTF document in HTML format
            // and list of all images.
            string html = r.ConvertString(rtf, imageList);
 
            // 3. Create an instance of MS Outlook            
            //Type outlookApp = Type.GetTypeFromProgID("Outlook.Application");
            //object objOutlookApp = Activator.CreateInstance(outlookApp);
            Outlook.Application application = new Outlook.Application();
 
            // 4. Create a new MailItem and set the To, Subject, and Body properties.
            Outlook.MailItem newMail = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);
            newMail.To = to;
            newMail.Subject = subject;            
            newMail.HTMLBody = html;
            newMail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
 
            // 5. Retrieve the account that has the specific SMTP address.
            // Use this account to send the e-mail.            
            Outlook.Account account = GetAccountForEmailAddress(application, from);
 
            // 6. Attach images to the email using cid
            // And send the email
            foreach (SautinSoft.RtfToHtml.SautinImage si in imageList)
            {
                string imageCid = si.Cid.Replace("cid:", "");
                string pathToImage = Path.Combine(pathToStoreTempAttachments, imageCid);
                si.Img.Save(Path.Combine(pathToImage));
                Outlook.Attachment attachment = newMail.Attachments.Add(pathToImage, Outlook.OlAttachmentType.olEmbeddeditem, null, imageCid);
                attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid);
                File.Delete(pathToImage);
            }
 
            newMail.SendUsingAccount = account;
            newMail.Send();
        }
        public static Outlook.Account GetAccountForEmailAddress(Outlook.Application application, string smtpAddress)
        {
 
            // Loop over the Accounts collection of the current Outlook session.
            Outlook.Accounts accounts = application.Session.Accounts;
            foreach (Outlook.Account account in accounts)
            {
                // When the e-mail address matches, return the account.
                if (account.SmtpAddress == smtpAddress)
                {
                    return account;
                }
            }
            throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress));
        }
 
    }
}
Dowload: RtfToHtmlEmailOutlook.cs.
I hope this article can be helpful to you. It is worth pointing out that, you may send HTML email without MS Oulook using standard .Net class SmtpClient.
 
Max Sautin (C# developer).
If you have any questions regarding this article email me, info@sautinsoft.com.
Share on Facebook

Written by Max Sautin

March 7th, 2014 at 8:45 am

How to convert RTF to HTML email with embedded images and send it via SmtpClient in .Net C#

without comments

In this article you will find how to create a simple .Net C# application which converts RTF document to HTML email with attached images and sends it via SmtpClient class.

Let’s make a C# code satisfying of these conditions:

  1. Acceptable for any .Net application: ASP.NET, Windows Forms, WPF, Console, Web Service, SilverLight etc.
  2. Works at 32-bit and 64-bit Windows machines.
  3. Compatible with .NET 2.0, 3.5 , 4.0, 4.5 frameworks and even higher if such will appear.
To make converting of RTF to HTML email with images at server-side we’ll use RTF-to-HTML DLL .Net library. It’s .Net component which will provides API to convert RTF to HTML with all images which we’ll get in a list after converting. We need these images extracted from RTF to further adding them into email as attachments.
RTF to HTML DLL .Net provides API to convert RTF to HTML email in C#

RTF to HTML DLL .Net provides API to convert RTF to HTML email in C#

To get the ball rolling, download the RTF-to-HTML DLL .Net component from here: http://www.sautinsoft.com/components/rtftohtml_dll_net.zip.
To check this out first hand, unpack the archive and create a new C# Console Application named SmtpRtfToHtml. Next add reference to the “SautinSoft.RtfToHtml.dll” as shown on picture below:

How to add a reference to SautinSoft.RtfToHtml.dll

How to add a reference to SautinSoft.RtfToHtml.dll


This is C# code of our application:
using System;
using System;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Drawing;
using System.Collections;
 
namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            string pathToRtf = @"c:\Document.rtf";
 
            // 1. Convert RTF to HTML and place all images to list
            string rtf = File.ReadAllText(pathToRtf);
            SautinSoft.RtfToHtml r = new SautinSoft.RtfToHtml();
            r.ImageStyle.IncludeImageInHtml = false;
            ArrayList imageList = new ArrayList();
 
            // 2. After launching this method we'll get our RTF document in HTML format
            // and list of all images.
            string html = r.ConvertString(rtf, imageList);
 
            // 3. Create HTML email
            string from = "bob@bobsite.com";
            string to = "john@johnsite.com";
            string subject = "This is a testing email from Bob to John using SmtpClient";
 
            MailMessage emailMessage = new MailMessage();
            emailMessage.From = new MailAddress(from);
            emailMessage.To.Add(to);
            emailMessage.Subject = subject.Replace("\r\n", "");
 
            // 4. Attach images to email
            System.Net.Mail.AlternateView altView = AlternateView.CreateAlternateViewFromString(html, null, "text/html");
 
            foreach (SautinSoft.RtfToHtml.SautinImage simg in imageList)
            {
                if (simg.Img != null)
                {
                    LinkedResource lr = null;
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    simg.Img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    if (ms != null && ms.Position > 0)
                        ms.Position = 0;
                    lr = new LinkedResource(ms);
                    lr.ContentId = simg.Cid.Replace("cid:", "");
                    altView.LinkedResources.Add(lr);
                }
            }
            emailMessage.AlternateViews.Add(altView);
 
            // 5. Send the message using email account
            string userName = "bobuser";
            string userPassword = "bobpassword";
 
            SmtpClient client = new SmtpClient();
            client.Port = 25;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
 
            //client.UseDefaultCredentials = false;
 
            // Some smtp servers doesn't require credentials, therefore
            // you may set: client.UseDefaultCredentials = false;
            // and remove the line: client.Credentials = new NetworkCredential(userName, userPassword);
 
            client.Credentials = new NetworkCredential(userName, userPassword);
            client.Host = "smtpout.bobsite.com";
            client.Send(emailMessage);
 
        }
    }
}

Download: RtfToHtmlEmailSmtpClient.cs.
 
I hope this article will be helpful to you. On a related note, here you may find how to send HTML email with attachments in C# using MS Outlook.
 

Max Sautin (C# developer).
If you have any questions regarding this article email me, info@sautinsoft.com.

Share on Facebook

Written by Max Sautin

March 6th, 2014 at 3:10 pm

How to convert a password protected PDF document

without comments

Clearly, a PDF format is a very common and popular in nowadays. This is due the fact that PDF can contain a text, images, filled forms and multimedia elements. That seems to be the case, that PDF format is widely used in polygraphy and also for distributing documents containing a vector graphics!

Thankfully, there are special programs and components to edit PDF-files, but their selection is much smaller than the programs to create and view PDF documents. A one of ways to edit a PDF document is simply convert it to a common editable MS-Word format.  документов. One of the tools allowing to convert PDF to Word is freeware First PDF.

This program is based on the SautinSoft’s component – PDF Focus .Net. The component allows to integrate feature of converting of PDF documents to Word, Text and Images using a few code lines.

1

The one of PDF Focus .Net benefits is that it can process password protected PDF documents. What is a password protected PDF file? If for any reason you want to transfer a file intended only to a narrow circle of people through a link in Internet, you can set a password for opening, reading and editing the PDF document. Thus, a person who will use the PDF will need to enter an appropriate password.

In conjuction with PDF Focus .Net any software will be able to convert password protected PDF documents. Furthermore, it totally simplifies the development of .Net applications and reduces the tens of thousands of lines of code that are necessary for a developer (e.g. to convert PDF to Word in c#) to several lines:

using System;
using System.IO;
 
namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            string pathToPdf = @"C:\PassPDF.pdf";
            string pathToWord = @"C:\Result.doc";
 
            //Convert PDF file to Word file
            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
 
            f.OpenPdf(pathToPdf);
 
            if (f.PageCount > 0)
            {
                int result = f.ToWord(pathToWord);
                f.Password = "qwerty";
                //Show Word document
                if (result==0)
                {
                    System.Diagnostics.Process.Start(pathToWord);
                }
            }
        }
    }
}

To see this functionality firsthand, you may see ASP.Net project which convert PDF to Word online: http://sautinsoft.net/pdf-to-word-images-convert.aspx

2

You may get to know about PDF Focus .Net and its capabilities here:
http://www.sautinsoft.com/products/pdf-focus/index.php

Share on Facebook

Written by Dmitry 'SautinSoft'

September 2nd, 2013 at 5:13 am

Posted in PDF Focus .Net

Tagged with , ,

How to convert HTML to Image using VB.NET

without comments

Article from our customer

Hi
Our law firm serves many customers around the world! For the convenience of our customers, we launched the online service that allows to view court decisions, regulations and orders!

On our site created a comfortable private office, which realized the possibility of online viewing of all documents in HTML. If desired, our client can convert the document from the format Html formats Word, PDF or Images!

Implementation of the function to convert from HTML to Images we have entrusted the company “SautinSoft” and .Net component – PDF Vision.

Below listed implementation of converting from HTML to Images:

        'Convert HTML file/url to Image file
        Dim v As New SautinSoft.PdfVision()
        'Convert image file to image file
        Dim imagePath As String = "C:\Web\Images\"
        Dim ret As Integer = v.ConvertHtmlFileToImageFile("http://www.sautinsoft.net",imagePath)
        ' 0 - converting successfully
        ' 1 - can't open input file, check the input path
        ' 2 - can't create output file, check the output path
        ' 3 - converting failed
        If ret = 0 Then
            'Show produced pdf in Acrobat Reader
            System.Diagnostics.Process.Start(imagePath)
        End If

Get other details you may at SautinSoft company website: http://www.sautinsoft.com

Share on Facebook

Written by Dmitry 'SautinSoft'

August 22nd, 2013 at 12:23 am

Posted in PDF Vision .Net