SautinSoft blog

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

Delphi.Net – How to convert PDF to WORD

without comments

Hi Everyone!

My name is Brian and in this short article, I’ll show how to convert PDF to Word in Delphi .Net within PDF Focus .Net library.

Firstly, you need to download the latest distribution of SautinSoft’s PDF Focus .Net (20 Mb):

The download link: http://www.sautinsoft.com/thankyou.php?download=4

More information about the component: http://www.sautinsoft.com/products/pdf-focus/index.php

Secondly, you must have installed the development environment Delphi. In this example, I’m using Delphi .Net 2009 for Microsoft .NET.

I will build a simple windows application with one button. Clicking by this button will launch the PDF to Word conversion.

Let’s get started …

Run the development Delphi environment. Create a new project.

You will see this form:

1

Next add a reference to the PDF Focus.Net library. In the package (pdf_focus_net.zip), which you have downloaded from here, browse the folder “/ Bin/Net 2.0” and choose the file SautinSoft.PdfFocus.dll.

2

Click on the Reference right mouse button. Then, on the .Net Assemblies, click Browse and navigate to the directory with the SautinSoft.PdfFocus.dll.

3

Then click OK. You will see that the Reference to SautinSoft.PdfFocus library has been added.

Now add a standard button from the Tool Palette. Let’s name the button – «Convert».

4

Ok, the most difficult part of the project is done. J It remains to add a few lines of code and we get a comprehensive PDF converter.

Double-click on the Convert button and get into the source code of our project.

In the directory Uses you need to add some libraries (ShellApi, SautinSoft):

5

In the directory Var declare additional variable (f):

6

Finally, it is necessary to prescribe the actions that will take place after we press the Convert button:

7

I will do some explaining.

The library PDF Focus .Net has many options for converting PDF to WORD, PDF to IMAGES, PDF to HTML, PDF to EXCEL. We’ve used only the one direction: PDF to WORD. However, no one is stopping you to experiment with a variety of additional parameters, which are very well described in the documentation: http://www.sautinsoft.net/help/pdf-to-word-tiff-images-text-rtf-csharp-vb-net /index.html

f.OpenPdf(…) – specify the path to the PDF file which you want to convert.

f.ToWord(…) – specify the path to a Word file, which will appear as a result of the conversion.

If the conversion is successful, then the Doc file will open automatically in a standard text editor (such as MS Word).

So… to compile the project and launch our project:

8

After clicking the button, the PDF file – simple_text.pdf, converted to the Doc file – result.doc

9

You can save the entire project to the disk and experiment with other PDF files.

10

If you have any questions about the transformation of PDF document to Doc, Images or any formats, please email: mailto:support@sautinsoft.com.

Our other components to convert different document formats can be found here: www.sautinsoft.com.

Good luck and thank you for your attention!

Brian

Share on Facebook

Written by Brian

August 7th, 2014 at 12:46 pm

Posted in PDF Focus .Net

Tagged with , , , ,

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 Brian

September 2nd, 2013 at 5:13 am

Posted in PDF Focus .Net

Tagged with , ,