Free Support Forum - aspose.com

C# Code to Convert .msg to .mhtml to .pdf | Fit Table in MHTML to within PDF Page Bounds during Conversion

Hello,

i’m having a problem converting an msg file to pdf file. Specifically i have a matrix that should be resized and fit in the pdf file, but after the conversion i can see only a part of it.

I’m sending you the msg and pdf files 0001cjln.1.pdf (107.6 KB)
0001cjln.1.msg.zip (25.9 KB)

That’s the code that i am using.

    public int AsposeEmailConvertToPdf(string inputFile, string outputFile)
    {
        int ret = 0;
        try
        {
	        Aspose.Email.MailMessage mailMsg = Aspose.Email.MailMessage.Load(inputFile);

	        MemoryStream ms = new MemoryStream();

            mailMsg.Save(ms, Aspose.Email.SaveOptions.DefaultMhtml);

	        Aspose.Words.LoadOptions loadOptions = new Aspose.Words.LoadOptions();

	        loadOptions.LoadFormat = Aspose.Words.LoadFormat.Mhtml;

	        Aspose.Words.Document document = new Aspose.Words.Document(ms, loadOptions);

	        Aspose.Words.Saving.PdfSaveOptions saveOptions = new Aspose.Words.Saving.PdfSaveOptions();

	        document.Save(outputFile, saveOptions);

Thank you in advance!

@panosk,

We have converted your .msg file to .mhtml by using the latest version of Aspose.Email for .NET i.e. 20.1 and attached it here for your reference:

You can see that the problematic Table in this MHTML is wider than the standard Page width of MS Word documents (i.e. 8.5 inches). You can check this by opening this MHtML with MS Word 2019. So, this is an expected behavior.

However, there are a few ways that you can use to workaround this problem:

  1. Increase PDF page Width
  2. Adjust / Decrease widths of Tables so that it fits inside existing PDF Pages
  3. Auto fit Tables to Windows etc

C# Draft Code:

Aspose.Email.MailMessage mailMsg = Aspose.Email.MailMessage.Load("E:\\Temp\\0001cjln.1.msg\\0001cjln.1.msg");
mailMsg.Save("E:\\Temp\\0001cjln.1.msg\\0001cjln.1.mhtml", Aspose.Email.SaveOptions.DefaultMhtml);

Aspose.Words.LoadOptions loadOptions = new Aspose.Words.LoadOptions();
loadOptions.LoadFormat = Aspose.Words.LoadFormat.Mhtml;
Aspose.Words.Document document = new Aspose.Words.Document("E:\\Temp\\0001cjln.1.msg\\0001cjln.1.mhtml", loadOptions);

//foreach (Table tbl in document.GetChildNodes(NodeType.Table, true))
//{
    // set tables width manually e.g.
    //double tblWidth = document.FirstSection.PageSetup.PageWidth - document.FirstSection.PageSetup.LeftMargin - document.FirstSection.PageSetup.RightMargin;
    //tbl.PreferredWidth = PreferredWidth.FromPoints(tblWidth);

    // or AutoFit Tables To Window
    //tbl.AllowAutoFit = true;
    //tbl.AutoFit(AutoFitBehavior.AutoFitToWindow);
//}

// Or increase Page width
document.FirstSection.PageSetup.PaperSize = Aspose.Words.PaperSize.Ledger;
//document.FirstSection.PageSetup.Orientation = Orientation.Landscape;

Aspose.Words.Saving.PdfSaveOptions saveOptions = new Aspose.Words.Saving.PdfSaveOptions();
document.Save("E:\\Temp\\0001cjln.1.msg\\20.2.pdf", saveOptions);

Hope, this helps in achieving what you are looking for.