Free Support Forum - aspose.com

Convert docx to PNG and set size on PNG image


#1

Hi I am converting docx to png image. How do I set the size of png file in pixels and rotate the image?
Also, sometimes the contents gets cut out. How can we fix that?


#2

@hardeep17

Thanks for your inquiry. You can use ImageSaveOptions class as shown below to convert document’s pages into image.

Document doc = new Document(MyDir + "Rendering.doc");

ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Png)
{
    Resolution = 300,
    PageCount = 1
};

doc.Save(MyDir + @"Rendering.SaveToImageResolution.png", options);

Aspose.Words does not provide API to rotate the images. However, you can use Document.RenderToScale method to render a document page into a Graphics object to a specified scale. Please check the following code example.

Document doc = new Document(MyDir + "Rendering.docx");

PageInfo pageInfo = doc.GetPageInfo(0);

// Let's say we want the image at 50% zoom.
const float myScale = 0.50f;

// Let's say we want the image at this resolution.
const float myResolution = 200.0f;

Size pageSize = pageInfo.GetSizeInPixels(myScale, myResolution);
using (Bitmap img = new Bitmap(pageSize.Width, pageSize.Height))
{
    img.SetResolution(myResolution, myResolution);

    using (Graphics gr = Graphics.FromImage(img))
    {
        // You can apply various settings to the Graphics object.
        gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

        // Fill the page background.
        gr.FillRectangle(Brushes.White, 0, 0, pageSize.Width, pageSize.Height);

        // Render the page using the zoom.
        doc.RenderToScale(0, gr, 0, 0, myScale);
    }

    img.Save(MyDir + @"Rendering.RenderToScale.png");
}

Please ZIP and attach your input and problematic output documents along with code example to reproduce this issue at our end. We will investigate the issue and provide you more information on it.