Free Support Forum - aspose.com

Convertion from MSG and EML to XPS

Hello,

I found an issue when converting MSG and EML files to XPS. Layout of XPS version of a document is changed. Please see attached original and XPS files for example.

Thank you,
Taras

Hello

Thanks for your inquiry. Could you please provide me the code which will allow me to reproduce the problem on my side? And also which version of Aspose.Words and Aspose.Network you are using?

Best regards,

Hi,

Sure.
Component versions:
Aspose.Words for .NET 9.8.0
Aspose.Network for .NET 6.4.0

Here is my code:

/// Default save option is XPS
SaveOptions saveOption = new XpsSaveOptions();

MessageFormat _messageFormat = MessageFormat.Eml;

/// Load Mail document
_message = MailMessage.Load(filePath, _messageFormat);

using (MemoryStream mhtMemoryStream = new MemoryStream())
{
/// Save as MHT
_message.Save(mhtMemoryStream, MailMessageSaveType.MHtmlFromat);

mhtMemoryStream.Position = 0;

var outFileStream = new MemoryStream();

/// Load MHT file
Document document = new Document(mhtMemoryStream);

/// Accept all revisions in the document.
document.AcceptAllRevisions();

/// Convert Document to defined format - ConversionFormat
document.Save(outFileStream, saveOption);

return outFileStream;
}

Taras

Hello

Thank you for additional information. As you can see, the image inside your document is wider than page. So to fix this problem you should try using the code like the following:

// Default save option is XPS

SaveOptions saveOption = new XpsSaveOptions();

MessageFormat _messageFormat = MessageFormat.Eml;

string filePath = "C:\\Temp\\EML_screenshot.eml";

// Load Mail document

MailMessage _message = MailMessage.Load(filePath, _messageFormat);

using (MemoryStream mhtMemoryStream = new MemoryStream())

{

// Save as MHT

_message.Save(mhtMemoryStream, MailMessageSaveType.MHtmlFromat);

mhtMemoryStream.Position = 0;

// Load MHT file

Document document = new Document(mhtMemoryStream);

document.FirstSection.PageSetup.Orientation = Orientation.Landscape;

document.FirstSection.PageSetup.PageWidth = document.FirstSection.PageSetup.PageWidth + 600;

// Accept all revisions in the document.

document.AcceptAllRevisions();

// Convert Document to defined format - ConversionFormat

document.Save("C:\\Temp\\out.xps", saveOption);

}

Best regards,

Thanks for code.

One question.
Is there the way how I can decide when I need to use ether Orientation.Portrait or Orientation.Landscape in runtime?

Thanks,
Taras

Hello

Thanks for your request. There is no direct way to achieve this using Aspose.Words. You should calculate width of all elements on a page and then set page width in order to calculated width. As you understand it is not a trivial task.

Best regards,