@aspose.shrayasr,
Thanks for your inquiry. Aspose.Words does not provide API to trim white space from output image. However, you can achieve this using .NET API. Please check the following code example. Hope this helps you.
Document doc = new Document(MyDir + "in.docx");
ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.Png);
imageOptions.PageIndex = 0;
imageOptions.PageCount = 1;
Color savePaperColor = imageOptions.PaperColor;
imageOptions.PaperColor = Color.Transparent;
MemoryStream stream = new MemoryStream();
doc.Save(stream, imageOptions);
Bitmap croppedImage;
// Load the image into a new bitmap.
using (Bitmap renderedImage = new Bitmap(stream))
{
// Extract the actual content of the image by cropping transparent space around
// the rendered shape.
Rectangle cropRectangle = FindBoundingBoxAroundNode(renderedImage);
croppedImage = new Bitmap(cropRectangle.Width, cropRectangle.Height);
croppedImage.SetResolution(100, 100);
// Create the final image with the proper background color.
using (Graphics g = Graphics.FromImage(croppedImage))
{
g.Clear(savePaperColor);
g.DrawImage(renderedImage, new Rectangle(0, 0, croppedImage.Width, croppedImage.Height), cropRectangle.X, cropRectangle.Y, cropRectangle.Width, cropRectangle.Height, GraphicsUnit.Pixel);
}
}
croppedImage.Save(MyDir + "output.png", System.Drawing.Imaging.ImageFormat.Png);
public static Rectangle FindBoundingBoxAroundNode(Bitmap originalBitmap)
{
Point min = new Point(int.MaxValue, int.MaxValue);
Point max = new Point(int.MinValue, int.MinValue);
for (int x = 0; x < originalBitmap.Width; ++x)
{
for (int y = 0; y < originalBitmap.Height; ++y)
{
// Note that you can speed up this part of the algorithm by using LockBits and unsafe code instead of GetPixel.
Color pixelColor = originalBitmap.GetPixel(x, y);
// For each pixel that is not transparent calculate the bounding box around it.
if (pixelColor.ToArgb() != Color.Empty.ToArgb())
{
min.X = Math.Min(x, min.X);
min.Y = Math.Min(y, min.Y);
max.X = Math.Max(x, max.X);
max.Y = Math.Max(y, max.Y);
}
}
}
// Add one pixel to the width and height to avoid clipping.
return new Rectangle(min.X, min.Y, (max.X - min.X) + 1, (max.Y - min.Y) + 1);
}