Attachments (Excel - Word - PDF - Image )

Hello,

The problem we have is that when you attach certain files ,
such as Word or Excel lose size .

Attach files to Excel spreadsheet contains a specific image that can be of different sizes,
in some cases print correctly in the final report, but in some cases when the image is of a small size it widens losing the original size.

  1. Resultado_Actual.docx ( as currently printed. )
  2. Code ( Script used to attach and assign properties to attachments)
  3. 001.xlsx (Excel files attached to our reports that are generated with the wrong size in the final report)

How can we solve this problem

Regards.

Hi Rodrigo,
Looks like some part of the code is missing. Can you please share a standalone console application to reproduce the issue and a document to show your expected output? Sorry for the inconvenience.
Best Regards,

Hello, Attached as requested.

Hi Rodrigo,
Sorry, the document in your application goes through several methods and it was not very helpful in understanding the issue.
To be more precise, can you please share a document to show your expected output? This will help us understand your requirement.
Best Regards,

Hello,
The process is as follows :
We need to print certain number of files that a single final document. So what we do is upload documents (Attach ) , these files can be Word , Excel or PDF .
In some cases users up excel containing images which when processed in some cases prints correctly but in some cases printed with some distortion in the final document .
We attach the following files:

  1. Resultado_Actual.docx : shows how out as distorted in size and width.
  2. Resultado_esperado.docx : as we need to display.
  3. Attachments to excel . ( Example1 , example2 , example3)

Hi Rodrigo,
Sorry, It is difficult to reproduce the issue using your application as document goes through several methods and it is not very clear which section is being used as current section.
I have extracted following part of code from your application which converts Excel files into images and then inserts those images in Word document and I get attached output at my end which looks correct.

string[] excelFiles = new string[] { "ejemplo1.xlsx", "ejemplo2.xlsx", "ejemplo3.xlsx" };
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Font.Name = "Arial Narrow";
builder.Font.Size = 9.5;
foreach (string excelFile in excelFiles)
{
    Workbook book = new Workbook(excelFile);
    Worksheet sheet = book.Worksheets[0];
    if (sheet.PageSetup.Orientation.ToString() == "Portrait")
    {
        Section section = builder.CurrentSection;
        section.PageSetup.Orientation = Aspose.Words.Orientation.Portrait;
    }
    else
    {
        Section section = builder.CurrentSection;
        section.PageSetup.Orientation = Aspose.Words.Orientation.Landscape;
    }
    Aspose.Words.PageSetup pageSetup = builder.PageSetup;
    Double AltoTabla = pageSetup.PageHeight - pageSetup.TopMargin - pageSetup.BottomMargin + 50;
    Double TamanoPagina = pageSetup.PageWidth - pageSetup.LeftMargin - pageSetup.RightMargin + 150;
    ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
    imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
    imgOptions.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    imgOptions.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    imgOptions.OnlyArea = true;
    imgOptions.IsCellAutoFit = true;
    imgOptions.Quality = 100;
    imgOptions.VerticalResolution = 300;
    imgOptions.HorizontalResolution = 300;
    SheetRender sr = new SheetRender(sheet, imgOptions);
    System.Drawing.Bitmap bitmap = sr.ToImage(0);
    System.Drawing.Size s = bitmap.Size;
    // bitmap.Save(excelFile + ".png");
    if (s.Width < TamanoPagina) { TamanoPagina = s.Width; }
    if (s.Height < AltoTabla) { AltoTabla = s.Height; }
    builder.InsertOleObject("Adjunto", true, true, bitmap.GetThumbnailImage(Convert.ToInt32(TamanoPagina), Convert.ToInt32(AltoTabla), null, IntPtr.Zero));
    builder.InsertBreak(Aspose.Words.BreakType.SectionBreakNewPage);
}
doc.Save("Result.docx");

Can you please share a simple code example to reproduce the issue without using much methods?
Best Regards,

Hello Muhammad ,

As you mentioned, it seems, there is nothing wrong with the code that performs the conversion exceli in image files.

Then for you to understand , we provide the following examples to help us find the problem

1. excels files containing the images to be printed in the final document.

2. resultado_Actual.docx Word file you can view the way it is printing.
You can validate that converts it to a size that does not correspond to the original image.
It is distorted in the final document.

3. Resultado_esperado.docx file is the way we hope we can print.

Already in a previous post we’ve shared the code we use and it is the segment that you mentioned which is responsible for the conversion,
which we believe is failing somewhere that is not printing the right way

Regards!

Hi Rodrigo,
Thanks for sharing these samples and details. We are further investigating it and will update you soon.
Best Regards,

Hi Rodrigo,
In fact the following line is affecting your images because actual width and height of the images is lot more than the specified in this line.

builder.InsertOleObject("Adjunto", true, true, bitmap.GetThumbnailImage(Convert.ToInt32(TamanoPagina), Convert.ToInt32(AltoTabla), null, IntPtr.Zero));

You can replace this line with the following code to see your desired result e.g.

Aspose.Words.Drawing.Shape shape = builder.InsertOleObject("Adjunto", true, true, bitmap);
shape.Width = shape.Width * 1.5;
shape.Height = shape.Height * 1.5;

Also, it would be better to use Landscape orientation as the images are too big to fit in Portrait orientation.

Aspose.Words.PageSetup pageSetup = builder.PageSetup;
builder.PageSetup.PaperSize = PaperSize.Legal;
pageSetup.Orientation = Orientation.Landscape;

Best Regards,

This issue has been solved .

Thank you so much