We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Set resolution of image export with DocIO

Is there a way to set the image resolution when exporting with DocIO?

14 Replies

SR Suganya Rathinam Syncfusion Team January 6, 2016 10:18 AM UTC

Hi Michael,

Thank you for contacting Syncfusion support.

Currently DocIO does not provide any direct API to specify the resolution of the Image in Word to Image conversion. As a work around you can specify the resolution of the image before saving it. Please find the following code snippets for your reference.



WordDocument document = new WordDocument(openFileDialog1.FileName);

Image[] images = document.RenderAsImages(ImageType.Bitmap);

int i = 0;

foreach (Image image in images)

{

   //Specify the resolution of the image

   (image as Bitmap).SetResolution(100, 100);

   //Saves the images as jpeg

   image.Save("WordToImage_" + i + ".jpeg", ImageFormat.Jpeg);

   i++;

}
document.Close();



Please let us know if you have any questions.

Regards,
Suganya



MR Michael Rensing January 6, 2016 02:38 PM UTC

Thanks.
Is there a way to also specify the image size. I am looking to save out a larger image. I need the images size in inches to remain the same as the Word document while also increasing the number of pixels.


SR Suganya Rathinam Syncfusion Team January 7, 2016 07:07 AM UTC

Hi Michael,

In Word to Image conversion using DocIO, the size of the image is considered based on the page size in the Word document. There is no need to explicitly specify the size of the image. If you need to modify the image size then you can modify the size of the pages in the Word document. Please find the following code snippets to modify the size of the page in the Word document using DocIO.


WordDocument document = new WordDocument(FileName);

//Modify the size of the pages in the document

foreach(WSection section in document.Sections)

{

    section.PageSetup.PageSize = new SizeF(620, 712);

}

//The images in the collection will have the width and height of the page in the Word document

Image[] images = document.RenderAsImages(ImageType.Bitmap);

//Close the document

document.Close();

int i = 0;

foreach (Image image in images)

{

    //Saves the images as jpeg

    image.Save("WordToImage_" + i + ".jpeg", ImageFormat.Jpeg);

    i++;
}


When the page size of the input document is modified, the number of images in Word to Image conversion will vary based on the modified page size.

Please let us know if you have any questions.

Regards,
Suganya



MR Michael Rensing January 7, 2016 04:45 PM UTC

Okay
So the actual image size will always be 96 * page size.  IE a letter sized document (8.5" x 11") will be save to an image at 816 x 1056 pixels. If I want a letter sized document to save at 1275 x 1650 pixels (8.5" x 11" @ 150 dpi instead of 96 dpi), I need to increase the page size by 1.5625 and set the resolution to 150. I would also need to increase all dimension in the document by the same amount.  Font sizes, tabs, etc.

The easiest way I see to modify the document would be to modify all the styles used in the document. The document is using custom named styles, not the default ones in word.  How would I go about accessing the custom styles by name to modify them. I was looking at the documentation on working with paragraphs, but don't see how to access a custom named style.  (http://help.syncfusion.com/file-formats/docio/working-with-paragraph#applying-paragraph-formatting)

Thanks for your help
Michael.


SR Suganya Rathinam Syncfusion Team January 8, 2016 09:09 AM UTC

Hi Michael,

DocIO does not provide any public API’s to specify the size and resolution of the image in Word to Image conversion. Please find the following code snippets which we have tried at our end to resize the image.


WordDocument document = new WordDocument(FileName);

Image[] images = document.RenderAsImages(ImageType.Metafile);

//Close the document

document.Close();

int i = 0;

foreach (Image image in images)

{

    Bitmap bitmap = null;

    Metafile metaFile = image as Metafile;

    bitmap = new Bitmap(1275, 1650);

    bitmap.SetResolution(150, 150);

    using (Graphics g = Graphics.FromImage(bitmap))

    {

        g.DrawImage(metaFile, 0, 0, 1275, 1650);

        g.Dispose();

    }

    bitmap.Save("WordToImage_" + i + ".png", ImageFormat.Png);

    i++;
}


Please find the following UG documentation link to access the paragraph with specified custom style name in the document for your reference.

UG documentation link:
http://help.syncfusion.com/file-formats/docio/working-with-word-document#iterating-through-document-elements

If the provided solution does not meet your requirement then kindly provide us your expected output so that we can analyze further and update you with appropriate solution at the earliest.

Regards,
Suganya



MR Michael Rensing January 8, 2016 04:18 PM UTC

Suganya,

This worked well.  I am however now running into an issue with creating a new bitmap. I am getting an error
"ArgumentException was unhandled Parameter is not valid" It will always happen on page 91 of the document.  I have added a try catch to sleep the thread until the new bitmap works. However, the pages that cause the thread to sleep never output to image properly. I have attached the project as well as the word document.

Thanks

Attachment: SyncFusion_Word_To_PNG_cb7a8a1d.zip


MR Michael Rensing January 8, 2016 11:14 PM UTC

I also noticed periods are missing ate numbers and letters in list.


SR Suganya Rathinam Syncfusion Team January 11, 2016 09:38 AM UTC

Hi Michael,

Thank you for your update.

The mentioned Parameter not valid exception while creating new Bitmap is not reproduced at our end. This is an external exception which may cause due to insufficient memory while creating bitmaps in your application. Please find the following MSDN forum link for your reference.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/37684999-62c7-4c41-8167-745a2b486583/systemdrawingbitmap-max-size?forum=netfxbcl

Please dispose the bitmap object after saving the image which will release the memory used by the bitmap object. Please find the following code snippets to dispose the bitmap object.


try

{

    bitmap = new Bitmap(bitmapWidth, bitmapHeight);

    bitmap.SetResolution(resolutionF, resolutionF);

    using (Graphics g = Graphics.FromImage(bitmap))

    {

        g.DrawImage(metafile, 0, 0, (float)bitmapWidth, (float)bitmapHeight);

        g.Dispose();

    }

    bitmap.Save(@"..\..\ssImages\Sheet Specs" + i.ToString("D3") + ".png",     ImageFormat.Png);

    bitmap.Dispose();

    bitmap = null;

    populated = true;

}


Regarding List Number Preservation issue:
We have created a new incident #149456  for the mentioned list number preservation issue in Word to Image conversion using DocIO for better follow up. Kindly follow the incident #149456 to get solution for the reported issue.

Regards,
Suganya



MR Michael Rensing January 12, 2016 11:55 PM UTC

Thanks, That worked great.  Is there a way to force antialiasing of the image?  I am using the same code, on in a simple form application and one in a add-in to another program. The simple form saves the images with antialiasing, the add-in program does not. The add-in is going to be the target use for the docx to png.

Any ideas to get antialiasing in the images?


SR Suganya Rathinam Syncfusion Team January 13, 2016 09:57 AM UTC

Hi Michael,

You have specified that the add-in does not save the images with antialiasing and the Windows Forms does. If the code used to save the image in simple Windows Form sample and add-in varies then please provide us the code snippets you have used. Please find the following code snippet to specify the antialiasing of the image and let us know if it helps.



using (Graphics g = Graphics.FromImage(bitmap))

{

    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

    g.DrawImage(metaFile, 0, 0, 1275, 1650);

    g.Dispose();

}



Also please let us know what add-in you are using so that we can analyze further and update you with the appropriate solution at the earliest.

Regards,
Suganya



MR Michael Rensing January 13, 2016 07:29 PM UTC

Suganya,

Thank you for the reply.  The code
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
didn't work.  Antialising is still not present in the image.  I am working on making an add-in for Autodesk Revit. The add-in has a form for selecting the word document to be processed. I am executing the conversion to png in the form, so the code is exactly the same as what was running in the stand alone windows form application. I am attaching the entire add-in project in case it helps.

Attachment: Manage_Sheet_Specs_e5c3d75b.zip


PL Pradeep Loganathan Syncfusion Team January 14, 2016 08:36 AM UTC

Hi Michael,

Thank you for your reply.

We could not run your Addin in any of our machine. Can you please provide necessary software to run your Addin and RevitAPI.dll, RevitAPIUI.dll , Thereby we can proceed further.

Also can you please try the following code snippet to resolve your antialiasing problem?



using (Graphics g = Graphics.FromImage(bitmap))

{

g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

g.DrawImage(metafile, 0, 0, 1275, 1650);

g.Dispose();

}


If this code does not meet your expected result, go through the following link with more details about image processing techniques.

https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode%28v=vs.110%29.aspx

Please let us know if you have any queries.

Best Regards,
Pradeep L



MR Michael Rensing January 14, 2016 03:23 PM UTC

Pradeep,

The add-in runs inside the program Revit.  It can be downloaded from http://www.autodesk.com/products/revit-family/free-trial and run for 30 day free.  The RevitAPI.dll and RevitAPIUI.dll are included in the install of he software.  The supplied code did not change the output.  I will consult the supplied link and see if any information there helps.

Thanks


DV Divyalakshmi Vasudevan Syncfusion Team January 15, 2016 09:59 AM UTC

Hi Michael,

Thank you for your update.
We have created a new incident #149718 for your last query for the better follow up. Kindly follow that incident to get resolved the reported issue.

Please let us know if you have any other questions.

Regards,
Divyalakshmi V



Loader.
Live Chat Icon For mobile
Up arrow icon