Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed May 08, 2013 8:18 pm

Hello.

I am currently evaluating Spire.Doc pro edition. When converting a word document to an image, I would like to know if it is possible for the white space in the resulting image to be cropped. Here is an example to illustrate what I am after.

Here is the word document that I wish to convert:

WordDocScreenshot.JPG


Here is the resulting Image:

croppedimage.JPG


Is this result possible? Is it possible to have the resulting image trimmed of white space from the original word document?

Input would be appreciated. Thanks!

tonylintunen
 
Posts: 3
Joined: Tue May 07, 2013 6:09 pm

Thu May 09, 2013 7:40 am

Hello,

Thanks for your inquiry.
Yes, your need can be implemented. Please try the code below.
Code: Select all
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace SALESSUPPORT_1448
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile(@"..\..\sss.docx");
            Image[] imgs = doc.SaveToImages(ImageType.Bitmap);
            for (int i = 0; i < imgs.Length; i++)
            {
                imgs[i] = ImageTrim(imgs[i] as Bitmap);

                imgs[i].Save(String.Format("img-{0}.png", i), ImageFormat.Png);
            }

            doc.Close();
        }

        private static Bitmap ImageTrim(Bitmap img)
        {
            //get image data
            BitmapData bd= img.LockBits(new Rectangle(Point.Empty, img.Size),
            ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            int[] rgbValues = new int[img.Height * img.Width];
            Marshal.Copy(bd.Scan0, rgbValues, 0, rgbValues.Length);
            img.UnlockBits(bd);


            #region determine bounds
            int left = bd.Width;
            int top = bd.Height;
            int right = 0;
            int bottom = 0;

            //determine top
            for (int i = 0; i < rgbValues.Length; i++)
            {
                int color = rgbValues[i] & 0xffffff;
                if (color != 0xffffff)
                {
                    int r = i / bd.Width;
                    int c = i % bd.Width;

                    if (left > c)
                    {
                        left = c;
                    }
                    if (right < c)
                    {
                        right = c;
                    }
                    bottom = r;
                    top = r;
                    break;
                }
            }

            //determine bottom
            for (int i = rgbValues.Length - 1; i >= 0; i--)
            {
                int color = rgbValues[i] & 0xffffff;
                if (color != 0xffffff)
                {
                    int r = i / bd.Width;
                    int c = i % bd.Width;

                    if (left > c)
                    {
                        left = c;
                    }
                    if (right < c)
                    {
                        right = c;
                    }
                    bottom = r;
                    break;
                }
            }

            if (bottom > top)
            {
                for (int r = top + 1; r < bottom; r++)
                {
                    //determine left
                    for (int c = 0; c < left; c++)
                    {
                        int color = rgbValues[r * bd.Width + c] & 0xffffff;
                        if (color != 0xffffff)
                        {
                            if (left > c)
                            {
                                left = c;
                                break;
                            }
                        }
                    }

                    //determine right
                    for (int c = bd.Width - 1; c > right; c--)
                    {
                        int color = rgbValues[r * bd.Width + c] & 0xffffff;
                        if (color != 0xffffff)
                        {
                            if (right < c)
                            {
                                right = c;
                                break;
                            }
                        }
                    }
                }
            }

            int width = right - left + 1;
            int height = bottom - top + 1;
            #endregion

            //copy image data
            int[] imgData = new int[width * height];
            for (int r = top; r <= bottom; r++)
            {
                Array.Copy(rgbValues, r * bd.Width + left, imgData, (r - top) * width, width);
            }

            //create new image
            Bitmap newImage = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            BitmapData nbd = newImage.LockBits(new Rectangle(0, 0, width, height),
            ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
            Marshal.Copy(imgData, 0, nbd.Scan0, imgData.Length);
            newImage.UnlockBits(nbd);

            return newImage;
        }
    }
}

There is any issue, please feel free to contact us.

Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Tue May 14, 2013 9:56 am

Hello,

Has the issue been resolved? Could you please update the thread if convenience?

If there are any questions, welcome to get it back to us.

Thanks,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Thu May 16, 2013 6:24 pm

Hello. Thanks for your help.

Everything worked as expected.

tonylintunen
 
Posts: 3
Joined: Tue May 07, 2013 6:09 pm

Return to Spire.Doc