Sunday 5 August 2012

Set Image Qaulity in Word to PDF Conversion with C#, VB.NET

When we convert Word to PDF, we can easily manage the format of word document text in different ways. But for many images contained word files, we cannot control it very well since the size of the output PDF file is always larger than it of the original word document, which, sometimes, can bring inconvenience. Thus, in order to meet people's need, I would like to share a simple method to set the image quality when we convert word to PDF 

Setting the image quality, to a large extent, is to manage the size of the images in output PDF document. New version Spire.Doc 4.2.8 helps me set the image quality when convert word to PDF in a quick way with C#, VB.NET.


How to set image quality in word to PDF conversion with C#,VB.NET
I am not sure whether other .net word component can realize this function, so please install Spire.Doc first in your system.


Step1. Create a project
1.
     Create a new project in Visual Studio.

2.     Add Spire.Doc Dll as reference. and then, add using at the top of the method. Please see below:

         C# 
         using System;         using System.Collections.Generic;         using System.Linq;         using System.Text;         using Spire.Doc;         namespace image_quality         {             class Program             {                 static void Main(string[] args)                 {                     .                  }                                      }         }
             VB.NET 

                Imports Collections.Generic             Imports System.Linq             Imports System.Text             Imports Spire.Doc             Namespace image_quality                        Class Program                                   Private Shared Sub Main(args As String())                                 .                                   End Sub                        End Class              End Namespace

Step2. Load a Word file from system
C# Code:
Document doc = new Document();
doc.LoadFromFile(@"C:\Program Files\Spire.Doc\image quality.doc", FileFormat.Doc);

VB.NET Code:
Dim doc As New Document()
doc.LoadFromFile(@"C:\Program Files\Spire.Doc\image quality.doc", FileFormat.Doc);
Step3. Set image quality.There are six images in the first page of my doc document,  I set the size of images all to be "20".  If I have only one image, the size of the image is also"20", so you can set it as your own need.

C# Code:
doc.JPEGQuality = 20;

VB.NET Code:
doc.JPEGQuality = 20;

Step4. Save and launch the fileC# Code:
          doc.SaveToFile("Image.pdf", FileFormat.PDF);
          System.Diagnostics.Process.Start("Image.pdf");

VB.NET Code:
          doc.SaveToFile("Image.pdf", FileFormat.PDF);
          System.Diagnostics.Process.Start("Image.pdf");


Preview
                      
                                                         Word Images
                                
                                                               PDF Images

The original word document size is 63KB. If I convert the word file directly to PDF, it is 80.5 KB. After I set the image quality, it changes to 58 KB. That is the clear difference.




 

No comments:

Post a Comment

Popular posts