We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Preserve Quality of Embedded Excel OLE Object during Converting Word to PDF using C# .NET

I use Aspose.Words 19.12. I have a word document that contains an Excel Sheet. When I converting it to PDF the result has very low quality. For example, the attached document after convert to PDF will like Result.png (7.4 KB)

Excel Text.zip (16.2 KB)

.

@freydoonk,

Please also share following resources here for further testing:

  • Aspose.Words generated output PDF file showing the undesired behavior
  • Simplified source code to reproduce the same problem on our end

We will then start further investigation into your issue and provide you more information. Thanks for your cooperation.

I have used this code to convert Word to PDF:

// Open Word Document
var document = new Aspose.Words.Document(@"D:\Excel Text.docx");

// Define Save Option
var saveOption = new Aspose.Words.Saving.PdfSaveOptions();
saveOption.NumeralFormat = Aspose.Words.Saving.NumeralFormat.Context;
saveOption.FontEmbeddingMode = Aspose.Words.Saving.PdfFontEmbeddingMode.EmbedNonstandard;
saveOption.JpegQuality = 100;

// Rerender Emf as PNG
document.GetChildNodes(Aspose.Words.NodeType.Shape, true)
  .Cast<Aspose.Words.Drawing.Shape>()
  .ToList()
  .ForEach(shape =>
  {
    if (shape.HasImage && (shape.ImageData.ImageType == Aspose.Words.Drawing.ImageType.Wmf || shape.ImageData.ImageType == Aspose.Words.Drawing.ImageType.Emf))
    {
      using (var vectorImageStream = new MemoryStream(shape.ImageData.ImageBytes))
        using (var image = Image.FromStream(vectorImageStream))
          using (var resterImageStream = new MemoryStream())
          {
            image.Save(resterImageStream, System.Drawing.Imaging.ImageFormat.Png);
            shape.ImageData.SetImage(resterImageStream);
          }
      
    }
  });


// Save PDF
document.Save(@"D:\Result.pdf", saveOption);

This code made Result1.pdf. If the Rerender Emf as PNG part is removed, the result will look like Result2.pdf

Excel Text.zip (38.1 KB)

@freydoonk,

We tested the scenario and have managed to reproduce the same problem on our end. For the sake of correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-19770. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

@freydoonk,

Regarding WORDSNET-19770, we have completed the work on your issue and most likely will close this issue with “Not a Bug” status. Please check below the analysis details:

A decline in quality occurs at the stage of conversion of EMF to PNG:

using (var vectorImageStream = new MemoryStream(shape.ImageData.ImageBytes))
using (var image = Image.FromStream(vectorImageStream))
using (var resterImageStream = new MemoryStream())
{
    image.Save(resterImageStream, System.Drawing.Imaging.ImageFormat.Png);
    shape.ImageData.SetImage(resterImageStream);
}

You can verify this by adding a line:

image.Save(resterImageStream, System.Drawing.Imaging.ImageFormat.Png);
image.Save("D:\\result.png");
shape.ImageData.SetImage(resterImageStream);

Aspose.Words cannot affect this; it just takes the current PNG and inserts it into the PDF. If we can help you with anything else, please feel free to ask.

I agree, but when I don’t use this code and convert the word document to PDF via Aspose directly the ExcelSheet content is messed up. Whereas when I use the MsWord to converting word to PDF the quality of embedded ExcelSheet is fine and content is not messed up. The Convert Result.png (24.3 KB) has shown this issue.

Excel Text.zip (194.8 KB)

.

@freydoonk,

We tested the scenario and have managed to reproduce the same problem on our end. For the sake of correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-19822. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.