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.

Mon Feb 13, 2023 3:57 pm

Hi,
we set a resize of the images before the transformation from Word to PDF with a fixed width of 600. In some cases, however, when the PDF is generated, the images do not maintain this resize and are enormous. Example attached: page 4 and page 6 should have two images respectively 600x451 and 600x400 but as you can see on page 4 the photo is huge and not really 600x451. If you export the photos from the PDF but in reality the photos are the correct size, so it would look like a rendering error in generating the PDF.

Below is the code used


Code: Select all
Paragraph p_image = new Paragraph(document);
                        p_image.Format.HorizontalAlignment = HorizontalAlignment.Center;
                       

                        Image imageArt = Image.FromFile(physicalPath + @"numeri\" + num.num_id + "\\" + numa.numa_id + "\\" + numa.numa_immagine);

                        //calculate the ratio
                        double dbl = (double)imageArt.Width / (double)imageArt.Height;
                        int newWidth = imageArt.Width;
                        double newHeight = imageArt.Height;

                        if (newWidth > 600)
                        {
                            newWidth = 600;
                            double diff = (double)(imageArt.Width - 600) / dbl;
                            newHeight = (double)(imageArt.Height - diff);
                        }

                        Bitmap objBitmap = ResizeImage(imageArt, newWidth, Convert.ToInt32(newHeight));

                        using (var memoryStream = new MemoryStream())
                        {
                            objBitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);

                            p_image.AppendPicture(memoryStream.ToArray());
                        }

                        tempSection.Paragraphs.Add(p_image);

awm
 
Posts: 9
Joined: Tue Oct 06, 2015 12:07 am

Tue Feb 14, 2023 12:44 pm

Hello,

Thanks for your inquiry.
According to your message, I simulated a image and created a console project with NetFrameWork4.8 and the latest version of Spire.Doc 11.1.4 to do a preliminary test, I didn’t reproduce your issue, I put my test code below and attached the input image file for your reference.

In addition, the issue relative to another issue (post50762.html#p50762) you mentioned, you can respond to both questions together

Code: Select all
  static void Main(string[] args)
        {
Document document = new Document();
            Image image = Image.FromFile(@"../../data/cat.jpg");

         

            Image newImage = ResetSize(image, new Size(600, 451));

            Section newSection = document.AddSection();

            Paragraph paragraph = newSection.AddParagraph();

            paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center;

            DocPicture picture = paragraph.AppendPicture(image);

            document.SaveToFile(@"../../output/result.docx", FileFormat.Docx);

            document.SaveToFile(@"../../output/result.pdf", FileFormat.PDF);
   }
public static Image ResetSize(Image image, Size size)
        {
            Bitmap bitmap = new Bitmap(size.Width, size.Height);
            Graphics g = Graphics.FromImage(bitmap);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
            g.DrawImage(image, 0, 0, bitmap.Width, bitmap.Height);
            g.Dispose();
            return bitmap;
        }


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Wed Feb 15, 2023 11:03 pm

Hi,
the problem has been fixed with your example code. Thank you

awm
 
Posts: 9
Joined: Tue Oct 06, 2015 12:07 am

Thu Feb 16, 2023 7:04 am

Hello,

Thanks for your feedback.
I'm glad to hear that your this issue has been solved. Have you a nice day! :D

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.Doc