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.

Tue May 21, 2019 3:29 am

Good day!

I was raising an issue for the quality of the image converted from spire. I have A document created from .doc file. When I converting it to using word to PDF the resolution is okay but then when the file is converting from doc to Image the resolution was kind of having a light color and blurred


Please check the file ConvertedtoDocuments


Thank you!

tcballelos
 
Posts: 36
Joined: Wed Nov 28, 2018 5:35 am

Tue May 21, 2019 7:16 am

Hi,

Thanks for your inquiry.
After converting your word to images, you could reset the image resolution to improve the quality of the images. Please refer to the code below. If there is any question, please feel free to write back.
Code: Select all
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("sample.docx", FileFormat.Docx);
            Image[] images = doc.SaveToImages(Spire.Doc.Documents.ImageType.Metafile);
            for (int i = 0; i < images.Length; i++)
            {
                Metafile mf = images[i] as Metafile;
                Image newimage = ResetResolution(mf, 200);
                string outputfile = String.Format("image-{0}.png", i);
                newimage.Save(outputfile,System.Drawing.Imaging.ImageFormat.Png);
            }
        }
        //set the image resolution by the ResetResolution() method
        public static Image ResetResolution(Metafile mf, float resolution)
        {
            int width = (int)(mf.Width * resolution / mf.HorizontalResolution);
            int height = (int)(mf.Height * resolution / mf.VerticalResolution);
            Bitmap bmp = new Bitmap(width, height);
            bmp.SetResolution(resolution, resolution);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.DrawImage(mf, Point.Empty);
            }
            return bmp;
        }

Sincerely,
Nancy
E-iceblue support team
User avatar

nancy.yang
 
Posts: 184
Joined: Wed Apr 03, 2019 2:33 am

Wed May 22, 2019 2:48 am

Good day!

My scenario is that I am creating the Image file first in another Method. Should I put the reset resolution before it converts to image?

This one is converting the document file to image this is the first step:

internal void InserAttachments(Spire.Doc.Document doc, int id, int modulepageId = 0)
{
....//some code
for (int x = 0; x < document.PageCount; x++)
{
Image imageDocument = document.SaveToImages(x, Spire.Doc.Documents.ImageType.Metafile);
imageDocument.Save(downloadPath + "_" + repFIleName + "_" + x + ".jpg", System.Drawing.Imaging.ImageFormat.Bmp);
}


}

Second step was Iwill put the save image file to a new page and I am setting it to default resolution, I draw a line and put a page number below the page also I wonder if this could take effect on the resolution and quality of the image of the file:

internal void CreatNewPageforAttachment(Spire.Doc.Document doc, int id)
{


Section s = doc.AddSection();
Paragraph p = s.AddParagraph();
//Insert Image and Set Its Size
DocPicture Pic = p.AppendPicture(Image.FromFile(list[x]));
Pic.Width = 550;
Pic.Height = 662;
var pageCount = doccount + 1;

s.PageSetup.Margins.Left = 25.9f;
s.PageSetup.Margins.Right = 60.9f;
//s.HeadersFooters.Header.ChildObjects.Clear();
//s.Document.LastSection.HeadersFooters.Header.ChildObjects.Clear();



s.PageSetup.DifferentFirstPageHeaderFooter = true;
s.PageSetup.DifferentOddAndEvenPagesHeaderFooter = false;
//// doc.Sections[x+1].HeadersFooters.Header.ChildObjects.Clear();
s.HeadersFooters.FirstPageHeader.ChildObjects.Clear();
s.HeadersFooters.LinkToPrevious = false;



Table table2 = s.AddTable(true);
table2.ResetCells(1, 4);
//table2.TableFormat.Borders.Color = Color.White;
table2.TableFormat.Borders.Left.Color = Color.White;
table2.TableFormat.Borders.Right.Color = Color.White;
table2.TableFormat.Borders.Bottom.Color = Color.White;
table2.TableFormat.Borders.Vertical.Color = Color.White;
Paragraph p1 = table2[0, 0].AddParagraph();
table2.Rows[0].Cells[0].Width = 140;


Spire.Doc.Fields.TextRange t2 = p1.AppendText("");
p1.Format.HorizontalAlignment = HorizontalAlignment.Left;


Paragraph p2 = table2[0, 1].AddParagraph();
Spire.Doc.Fields.TextRange t3 = p2.AppendText("");
p2.Format.HorizontalAlignment = HorizontalAlignment.Center;

Paragraph p3 = table2[0, 2].AddParagraph();
Spire.Doc.Fields.TextRange t4 = p3.AppendText("");
p3.Format.HorizontalAlignment = HorizontalAlignment.Center;

Paragraph p4 = table2[0, 3].AddParagraph();
Spire.Doc.Fields.TextRange t5 = p4.AppendText("Page " +(pageCount + x) + " of " + totalpage);
p4.Format.HorizontalAlignment = HorizontalAlignment.Center;

table2.TableFormat.Positioning.HorizPositionAbs = HorizontalPosition.Outside;
table2.TableFormat.Positioning.VertRelationTo = VerticalRelation.Margin;
table2.TableFormat.Positioning.VertPosition = 730;
table2.TableFormat.Positioning.HorizPosition = 16;
}

}

tcballelos
 
Posts: 36
Joined: Wed Nov 28, 2018 5:35 am

Wed May 22, 2019 9:43 am

Hi,

Thanks for your feedback.
Please reset the image resolution after converting word to images. See code snippet below. And the code in your second step doesn't take effect on the resolution of the image. If there is any question or misunderstanding, please feel free to write back.
Code: Select all
internal void InserAttachments(Spire.Doc.Document doc, int id, int modulepageId = 0)
{
     //....some code
     for (int x = 0; x < document.PageCount; x++)
    {
         //save to image
         Image imageDocument = document.SaveToImages(x, Spire.Doc.Documents.ImageType.Metafile);
         Metafile mf = imageDocument as Metafile;
        //reset image resolution
        Image newimage = ResetResolution(mf, 200);
        newimage.Save(downloadPath + "_" + repFIleName + "_" + x + ".jpg", System.Drawing.Imaging.ImageFormat.Bmp);
    }
}
....

Sincerely,
Nancy
E-iceblue support team
User avatar

nancy.yang
 
Posts: 184
Joined: Wed Apr 03, 2019 2:33 am

Thu May 23, 2019 6:44 am

Good day!

I will try the code you provided


Thank you!

tcballelos
 
Posts: 36
Joined: Wed Nov 28, 2018 5:35 am

Thu May 23, 2019 7:58 am

Hi,

Thanks for your feedback.
I am looking forward to your test feedback. If there is any question, please feel free to write back.

Sincerely,
Nancy
E-iceblue support team
User avatar

nancy.yang
 
Posts: 184
Joined: Wed Apr 03, 2019 2:33 am

Return to Spire.Doc