Aspose.Pdf and Aspose.Drawing exceptions converting from msg to html and then pdf

I’m using .net core on Windows with version 25.11 of Aspose.Email and Aspose.Pdf.Drawing libraries (the latest) and I have 2 files where I’m getting exceptions. One exception occurs in Aspose.PDF and the other in Aspose.Drawing. I’m converting from msg to html first and then loading the html into an Aspose.Pdf document. I’ve attached the sample files including the original msg files, along with the generated html, and a text file with the stacktrace. These are pretty simple emails so they should convert without issue.

Stack trace for the first file:

Exception loading html into Aspose.Pdf.Document
Aspose.PDF - Index was outside the bounds of the array.

at Aspose.Pdf.XImageCollection.#=zf5IEHTRPehAAwNmjUA==(#=zxT_okVOd$Stxe28mrpG0GxKVDtk7 #=zl1IxFJmOEpkY5Ucgug==, #=zsOWUR1TDpmZzaeovtYBVIhBHuNhTY9Gt4WHxRSOLqTbC #=zLJ8uqQI=, #=zP93hTBtWJAumuQlMxCjevYtbSsPWe7kvpA== #=zilhl144=, Image& #=z5y1sBB7vG75t, Stream #=zzB2LPOj2RfZWxVT5K45$9XQ=)
at Aspose.Pdf.XImageCollection.#=zNgbuOgFEmAL9(Int32 #=z5ex6NM8=, #=zxT_okVOd$Stxe28mrpG0GxKVDtk7 #=zl1IxFJmOEpkY5Ucgug==, String& #=z3dKvsjX5vBbg, Stream #=zzB2LPOj2RfZWxVT5K45$9XQ=)
at #=zkDb0gCYFdBwDgwcBB8v65TW1Fse0JClTwA==.#=zpO9QUJYw5XHG(Byte[] #=zl56uqB8=, #=zzvgHDKlKl5y4QNKsdq1uNhkKknqS #=znYAt8Nc=, RectangleF #=zmbo8LHA=)
at #=z98cJmZiqJ1TV9OVdQhH94iuSpUjPkwOEHA==.#=zB934aAw=(#=zPSeSTB7fgnUQa6_wzloOI$Lrgpd_5GPDWQ== #=zJq4VM9GddWDPXin2iA==, #=zA73XL_VPNtEv02UQ0BUY9i8iG0uy #=zbIS0$ME=, Single #=zPmDCBR4=, Single #=zs9HkfB4=, #=zLUkj1LLdJWCnT3rQ9tx1Nrfipfo5bAoezg== #=zViS1$z_rGfl6)
at #=zT$T4vLPyyXcuCnMEMgKuimFHahzmKfC9LUgxJKg=.#=zB934aAw=(#=zPSeSTB7fgnUQa6_wzloOI$Lrgpd_5GPDWQ== #=zJq4VM9GddWDPXin2iA==, #=zA73XL_VPNtEv02UQ0BUY9i8iG0uy #=zbIS0$ME=, Single #=zPmDCBR4=, Single #=zs9HkfB4=, #=zLUkj1LLdJWCnT3rQ9tx1Nrfipfo5bAoezg== #=zViS1$z_rGfl6, Boolean #=zOVTqK$iOOwuaCpVVug==, #=zlKg_owb_YE5oZjMii$ylj1kJNpTYLTkBL_ycoxg= #=z9uMJag4=)
at #=zPSeSTB7fgnUQa6_wzloOI$Lrgpd_5GPDWQ==.#=z8YnlCyTLn3WsIe2rtb$peAE=(#=zqI_S15kllOekR6hUXsjMEE9Vq06FLFja6VJM33_jGR7O #=zkyA5fzEqPGX8, Boolean #=zOVTqK$iOOwuaCpVVug==, #=zlKg_owb_YE5oZjMii$ylj1kJNpTYLTkBL_ycoxg= #=z9uMJag4=)
at #=zPSeSTB7fgnUQa6_wzloOI$Lrgpd_5GPDWQ==.#=zlGtQR4DBvkHKTAbvHg==(#=zTBo03oMrcp7ATr7QrA== #=zzFwO7VfrhvBHDktj7A==, Boolean #=zOVTqK$iOOwuaCpVVug==, #=zlKg_owb_YE5oZjMii$ylj1kJNpTYLTkBL_ycoxg= #=z9uMJag4=)
at #=zPSeSTB7fgnUQa6_wzloOI$Lrgpd_5GPDWQ==.#=z6$bIrNc=(#=zSGw_P1cag2N_Jpwq_S8DC7QnAAcwwaUsb$c14y_nduox #=z4u54Eyg=, #=zrPDgd6zUqgMQzBIAxg== #=zzFwO7VfrhvBHDktj7A==, Boolean #=zOVTqK$iOOwuaCpVVug==, #=zlKg_owb_YE5oZjMii$ylj1kJNpTYLTkBL_ycoxg= #=z9uMJag4=)
at #=zqt6KoM6i_jDh0vCGJ8vAnY39oEADnYWX8kd25BU=.Render(#=zA73XL_VPNtEv02UQ0BUY9i8iG0uy #=zbIS0$ME=, CancellationToken #=zxt_$Rto=)
at #=zfN05p60z1At6wvQJ0zmF_M_4S2gV.#=zzs5ojw$LbwAZ(#=ziubIULQpSaS6rnp1ZU9sE_M= #=zJq4VM9GddWDPXin2iA==, #=zy_ju8ikgDaxdZlPUWSsoMUTf2jRu[] #=z_45JoAZGPoNB, #=zA73XL_VPNtEv02UQ0BUY9i8iG0uy #=zbIS0$ME=, CancellationToken #=zpgqUof0=)
at #=zLZbqDYFDf7Db8P$GqSurmULxErYC.Render(#=zA73XL_VPNtEv02UQ0BUY9i8iG0uy #=zbIS0$ME=, CancellationToken #=zpgqUof0=, #=zyKSjQdRkSulgDTX1DUI9_So=[] #=z4BlEemg=, #=zwQ2B5xR3BLfBeJmvR_H0C7Q=[] #=z6rPai0g=)
at #=zLZbqDYFDf7Db8P$GqSurmULxErYC.Render(#=zA73XL_VPNtEv02UQ0BUY9i8iG0uy #=zbIS0$ME=, TimeSpan #=zgKwwFGo=, #=zHrM4$A2BqqJuiffJruaBxoc=[] #=zdaX1kgQ=)
at #=zTjpNV1a9i2S56TuaHxCs3JcfZa4M.Render(#=zA73XL_VPNtEv02UQ0BUY9i8iG0uy #=zbIS0$ME=, #=zBRod2bE= #=zaM0WXqo=, TimeSpan #=zgKwwFGo=)
at #=zTjpNV1a9i2S56TuaHxCs3JcfZa4M.Render(#=zA73XL_VPNtEv02UQ0BUY9i8iG0uy #=zbIS0$ME=, #=zBRod2bE= #=zaM0WXqo=)
at #=zOr1F02AuROLXQbC4uQonRsPCVcqG.#=zz4qG8ae5dqMx(Stream #=zrwNQ00Mxi76O, Document #=z6GVHu3mv2rNQ, HtmlLoadOptions #=zLh26SM7ZXNFP, String #=zEFYNijL5KmlB)
at #=zOr1F02AuROLXQbC4uQonRsPCVcqG.#=zMLwHPh4=(Stream #=zrwNQ00Mxi76O, Document #=z6GVHu3mv2rNQ, HtmlLoadOptions #=zLh26SM7ZXNFP, String #=zEFYNijL5KmlB)
at #=zOr1F02AuROLXQbC4uQonRsPCVcqG.#=zMLwHPh4=(Stream #=zrwNQ00Mxi76O, Document #=z6GVHu3mv2rNQ, HtmlLoadOptions #=zLh26SM7ZXNFP)
at Aspose.Pdf.Document.#=zfO8bulY=(Stream #=zpj$3NoY=, LoadOptions #=zu2IVDcE=, Boolean #=zFkotHjsm4Fn2)
at Aspose.Pdf.Document.#=zfO8bulY=(Stream #=zpj$3NoY=, LoadOptions #=zu2IVDcE=)
at Aspose.Pdf.Document…ctor(String filename, LoadOptions options)

Stack trace for the second file:

Exception loading html into Aspose.Pdf.Document
Aspose.Drawing - Value does not fall within the expected range.

at J2.d(Byte[] d, Int32 v, Byte[] c, Int32 t, Int32 n)
at zz.pziFd(Byte[] d, Int32 v, Int32 c)
at PB.d(Byte[][] d)
at PB.d(Object d, Int32 v)
at r2.v(Int32 d)
at r2.d(qB d, mU v, Int32 c, Int32 t, Int32 n, Boolean B)
at r2.d(Int32 d, Int32 v, Int32 c, mU t)
at PB.IEiFd(mU d)
at gB.SEiFd(mU d, gj v, Me c)
at gB.REiFd(mU d, dE v)
at Yd.IEiFd(mU d)
at Kd.d(IE d, IList`1 v, Qq c)
at Kd.d(mU d, IE v, hd c, Int32 t, Int32 n, Qq B)
at Kd.d(mU d, IE v, Qq c)
at EG.RGiFd(mU d, dE v)
at MF.v(mU d, dE v, Boolean c)
at MF.d(mU d, Boolean v)
at MF.v(mU d)
at LG.d(Stream d, dU2 v, Boolean c)
at LG.v.d()
at vU2.d()
at eU2.d()
at System.Drawing.Bitmap.get_Palette()
at Aspose.Pdf.XImageCollection.#=zzt0DX4AtYBlh$ouMGw==(Image #=z0wtqZYY=)
at Aspose.Pdf.XImageCollection.#=zNgbuOgFEmAL9(Int32 #=z5ex6NM8=, #=zxT_okVOd$Stxe28mrpG0GxKVDtk7 #=zl1IxFJmOEpkY5Ucgug==, String& #=z3dKvsjX5vBbg, Stream #=zzB2LPOj2RfZWxVT5K45$9XQ=)
at Aspose.Pdf.XImageCollection.#=zKIMP9bo=(#=zxT_okVOd$Stxe28mrpG0GxKVDtk7 #=zl1IxFJmOEpkY5Ucgug==, String& #=zSLKla74=)
at Aspose.Pdf.XImageCollection.Add(Stream image)
at #=zkDb0gCYFdBwDgwcBB8v65TW1Fse0JClTwA==.#=zpO9QUJYw5XHG(Byte[] #=zl56uqB8=, #=zzvgHDKlKl5y4QNKsdq1uNhkKknqS #=znYAt8Nc=, RectangleF #=zmbo8LHA=)
at #=zPSeSTB7fgnUQa6_wzloOI$Lrgpd_5GPDWQ==.#=zv904Qa9De8u0(Byte[] #=zl56uqB8=, #=zzvgHDKlKl5y4QNKsdq1uNhkKknqS #=zU_D9Zqo=, RectangleF #=zXA4CexU=)
at #=z98cJmZiqJ1TV9OVdQhH94iuSpUjPkwOEHA==.#=zB934aAw=(#=zPSeSTB7fgnUQa6_wzloOI$Lrgpd_5GPDWQ== #=zJq4VM9GddWDPXin2iA==, #=zA73XL_VPNtEv02UQ0BUY9i8iG0uy #=zbIS0$ME=, Single #=zPmDCBR4=, Single #=zs9HkfB4=, #=zLUkj1LLdJWCnT3rQ9tx1Nrfipfo5bAoezg== #=zViS1$z_rGfl6)
at #=zT$T4vLPyyXcuCnMEMgKuimFHahzmKfC9LUgxJKg=.#=zB934aAw=(#=zPSeSTB7fgnUQa6_wzloOI$Lrgpd_5GPDWQ== #=zJq4VM9GddWDPXin2iA==, #=zA73XL_VPNtEv02UQ0BUY9i8iG0uy #=zbIS0$ME=, Single #=zPmDCBR4=, Single #=zs9HkfB4=, #=zLUkj1LLdJWCnT3rQ9tx1Nrfipfo5bAoezg== #=zViS1$z_rGfl6, Boolean #=zOVTqK$iOOwuaCpVVug==, #=zlKg_owb_YE5oZjMii$ylj1kJNpTYLTkBL_ycoxg= #=z9uMJag4=)
at #=zPSeSTB7fgnUQa6_wzloOI$Lrgpd_5GPDWQ==.#=z8YnlCyTLn3WsIe2rtb$peAE=(#=zqI_S15kllOekR6hUXsjMEE9Vq06FLFja6VJM33_jGR7O #=zkyA5fzEqPGX8, Boolean #=zOVTqK$iOOwuaCpVVug==, #=zlKg_owb_YE5oZjMii$ylj1kJNpTYLTkBL_ycoxg= #=z9uMJag4=)
at #=zPSeSTB7fgnUQa6_wzloOI$Lrgpd_5GPDWQ==.#=zBFpr10o8sW4C(#=zpKaNFSpTpH5ediJ3679frksz4DzypIYIEs7Ph4ACEG_7 #=z4BtCdac=, Boolean #=zOVTqK$iOOwuaCpVVug==, #=zlKg_owb_YE5oZjMii$ylj1kJNpTYLTkBL_ycoxg= #=z9uMJag4=)
at #=zPSeSTB7fgnUQa6_wzloOI$Lrgpd_5GPDWQ==.#=zBFpr10o8sW4C(#=zpKaNFSpTpH5ediJ3679frksz4DzypIYIEs7Ph4ACEG_7 #=z4BtCdac=, Boolean #=zOVTqK$iOOwuaCpVVug==, #=zlKg_owb_YE5oZjMii$ylj1kJNpTYLTkBL_ycoxg= #=z9uMJag4=)
at #=zPSeSTB7fgnUQa6_wzloOI$Lrgpd_5GPDWQ==.#=zlGtQR4DBvkHKTAbvHg==(#=zTBo03oMrcp7ATr7QrA== #=zzFwO7VfrhvBHDktj7A==, Boolean #=zOVTqK$iOOwuaCpVVug==, #=zlKg_owb_YE5oZjMii$ylj1kJNpTYLTkBL_ycoxg= #=z9uMJag4=)
at #=zPSeSTB7fgnUQa6_wzloOI$Lrgpd_5GPDWQ==.#=z6$bIrNc=(#=zSGw_P1cag2N_Jpwq_S8DC7QnAAcwwaUsb$c14y_nduox #=z4u54Eyg=, #=zrPDgd6zUqgMQzBIAxg== #=zzFwO7VfrhvBHDktj7A==, Boolean #=zOVTqK$iOOwuaCpVVug==, #=zlKg_owb_YE5oZjMii$ylj1kJNpTYLTkBL_ycoxg= #=z9uMJag4=)
at #=zqt6KoM6i_jDh0vCGJ8vAnY39oEADnYWX8kd25BU=.Render(#=zA73XL_VPNtEv02UQ0BUY9i8iG0uy #=zbIS0$ME=, CancellationToken #=zxt_$Rto=)
at #=zfN05p60z1At6wvQJ0zmF_M_4S2gV.#=zzs5ojw$LbwAZ(#=ziubIULQpSaS6rnp1ZU9sE_M= #=zJq4VM9GddWDPXin2iA==, #=zy_ju8ikgDaxdZlPUWSsoMUTf2jRu[] #=z_45JoAZGPoNB, #=zA73XL_VPNtEv02UQ0BUY9i8iG0uy #=zbIS0$ME=, CancellationToken #=zpgqUof0=)
at #=zLZbqDYFDf7Db8P$GqSurmULxErYC.Render(#=zA73XL_VPNtEv02UQ0BUY9i8iG0uy #=zbIS0$ME=, CancellationToken #=zpgqUof0=, #=zyKSjQdRkSulgDTX1DUI9_So=[] #=z4BlEemg=, #=zwQ2B5xR3BLfBeJmvR_H0C7Q=[] #=z6rPai0g=)
at #=zLZbqDYFDf7Db8P$GqSurmULxErYC.Render(#=zA73XL_VPNtEv02UQ0BUY9i8iG0uy #=zbIS0$ME=, TimeSpan #=zgKwwFGo=, #=zHrM4$A2BqqJuiffJruaBxoc=[] #=zdaX1kgQ=)
at #=zTjpNV1a9i2S56TuaHxCs3JcfZa4M.Render(#=zA73XL_VPNtEv02UQ0BUY9i8iG0uy #=zbIS0$ME=, #=zBRod2bE= #=zaM0WXqo=, TimeSpan #=zgKwwFGo=)
at #=zTjpNV1a9i2S56TuaHxCs3JcfZa4M.Render(#=zA73XL_VPNtEv02UQ0BUY9i8iG0uy #=zbIS0$ME=, #=zBRod2bE= #=zaM0WXqo=)
at #=zOr1F02AuROLXQbC4uQonRsPCVcqG.#=zz4qG8ae5dqMx(Stream #=zrwNQ00Mxi76O, Document #=z6GVHu3mv2rNQ, HtmlLoadOptions #=zLh26SM7ZXNFP, String #=zEFYNijL5KmlB)
at #=zOr1F02AuROLXQbC4uQonRsPCVcqG.#=zMLwHPh4=(Stream #=zrwNQ00Mxi76O, Document #=z6GVHu3mv2rNQ, HtmlLoadOptions #=zLh26SM7ZXNFP, String #=zEFYNijL5KmlB)
at #=zOr1F02AuROLXQbC4uQonRsPCVcqG.#=zMLwHPh4=(Stream #=zrwNQ00Mxi76O, Document #=z6GVHu3mv2rNQ, HtmlLoadOptions #=zLh26SM7ZXNFP)
at Aspose.Pdf.Document.#=zfO8bulY=(Stream #=zpj$3NoY=, LoadOptions #=zu2IVDcE=, Boolean #=zFkotHjsm4Fn2)
at Aspose.Pdf.Document.#=zfO8bulY=(Stream #=zpj$3NoY=, LoadOptions #=zu2IVDcE=)
at Aspose.Pdf.Document…ctor(String filename, LoadOptions options)

Sample code is simple enough:

            using (Aspose.Email.MailMessage msg1 = MailMessage.Load("test.msg"))
                msg1.Save("test.msg.html", Aspose.Email.SaveOptions.DefaultHtml);

            using var document1 = new Document("test.msg.html", new Aspose.Pdf.HtmlLoadOptions());

files.zip (337.9 KB)

@skowald

What’s happening

  • You load a .msg file with Aspose.Email, save it as HTML, then open that HTML with Aspose.Pdf (new Document(htmlPath, new HtmlLoadOptions()).
  • For two particular messages the HTML contains image data that Aspose.Pdf (and the underlying Aspose.Drawing) cannot parse – the renderer throws “Index was outside the bounds of the array” or “Value does not fall within the expected range.”

These errors are usually caused by:

  1. Unsupported image format (e.g., CMYK BMP, ICO with an invalid palette, corrupted base‑64 data).
  2. Corrupt image stream that Aspose.Drawing tries to read when the HTML <img> tag points to a local file or a data‑uri.
  3. Version mismatch – the older Aspose.Pdf.Drawing assembly is being loaded together with the newer Aspose.Pdf (the drawing library is now merged into Aspose.Pdf).

Quick‑fix checklist

Step Action Why it helps
1. Use the same major version for all Aspose assemblies Ensure Aspose.Email and Aspose.Pdf are from the same release (e.g., both 25.11). Remove any reference to Aspose.Pdf.Drawing – it is obsolete. Prevents type‑mismatch and internal‑API conflicts.
2. Inspect the generated HTML Open the .html file in a browser. Look for <img> tags that reference files in the same folder or use data: URIs. Note the file extensions (e.g., .bmp, .tif, .ico). Identifies the exact image that triggers the failure.
3. Convert unsupported images before loading Replace the problematic image with a supported format (PNG, JPEG, GIF) or remove it. You can do this programmatically:
foreach (var img in Directory.GetFiles(htmlFolder)) { if (Path.GetExtension(img).Equals(".bmp", StringComparison.OrdinalIgnoreCase)) ImageConverter.ConvertToPng(img); }
Aspose.Pdf can always read PNG/JPEG.
4. Disable image loading (if images are not required) csharp var htmlOpts = new HtmlLoadOptions { LoadImages = false }; var doc = new Document(htmlPath, htmlOpts); Skips the failing image parsing altogether.
5. Use Aspose.Email’s direct PDF conversion csharp using (MailMessage msg = MailMessage.Load(msgPath)) { msg.Save(pdfPath, SaveOptions.CreateSaveOptions(SaveFormat.Pdf)); } Bypasses the HTML step and avoids the image‑rendering path that causes the crash.
6. Verify runtime Run the app on .NET 6/7 (or the version you built against) and make sure the Microsoft Visual C++ Redistributable is installed (required by native image handling). Missing native dependencies can also surface as “value out of range”.

Minimal reproducible code (working with the fixes)

// 1. Load the .msg
using (var msg = MailMessage.Load(@"C:\Temp\test.msg"))
{
    // 2. Direct PDF conversion – no HTML step needed
    msg.Save(@"C:\Temp\test.pdf", SaveOptions.CreateSaveOptions(SaveFormat.Pdf));
}

// ---------------------------------------------------
// If you must keep the HTML step (e.g., you need the HTML output)

using (var msg = MailMessage.Load(@"C:\Temp\test.msg"))
{
    // Save HTML (images are written to a folder next to the .html)
    msg.Save(@"C:\Temp\test.html", SaveOptions.DefaultHtml);
}

// Optional: remove/convert unsupported images in the folder
// (example: convert all .bmp to .png)
foreach (var bmp in Directory.GetFiles(@"C:\Temp", "*.bmp"))
{
    using var img = System.Drawing.Image.FromFile(bmp);
    var pngPath = Path.ChangeExtension(bmp, ".png");
    img.Save(pngPath, System.Drawing.Imaging.ImageFormat.Png);
    File.Delete(bmp);
}

// Load HTML into PDF, skipping images if you prefer
var htmlOpts = new HtmlLoadOptions { LoadImages = false };   // set true after fixing images
var pdfDoc = new Document(@"C:\Temp\test.html", htmlOpts);
pdfDoc.Save(@"C:\Temp\test_from_html.pdf");

How to pinpoint the offending image (if you need it)

try
{
    var doc = new Document(htmlPath, new HtmlLoadOptions());
}
catch (Exception ex)
{
    // ex.StackTrace will contain the image index; you can also enable logging:
    Aspose.Pdf.Logging.Logger.StartLogging("pdf_log.txt");
    // re‑run and inspect the log – it lists the image file name that caused the error.
}

The log file (or the inner exception message) usually prints the file name of the image that could not be decoded.


TL;DR

  • Make sure all Aspose assemblies are from the same release and remove Aspose.Pdf.Drawing.
  • Check the HTML for images in unsupported formats; convert them to PNG/JPEG or disable image loading.
  • Prefer the direct MailMessage → PDF conversion offered by Aspose.Email, which avoids the problematic HTML‑to‑PDF path altogether.

If the problem persists after these steps, please attach the specific image file (or its name from the log) so we can verify whether the format is truly unsupported.

Responding to the robot.

  1. I am using the same versions of libraries, the latest. Are you sure I can use Aspose.Pdf instead of Aspose.Pdf.Drawing on Linux with .net core 7.0 and not get errors with System.Drawing being unavailable? I don’t think that works. If I remove Aspose.Pdf.Drawing and add Aspose.Pdf and run on Windows then the file REV004212_Exported_Native_File.msg works but the other file still does not even if I add a resource handler and skip loading all resources. And to be clear, there are no external images files.

  2. No comment.

  3. There are no external files.

  4. LoadImages = false is not an available option. I can disable loading images using a resource handler and I tried that but it still errors.

  5. SaveFormat.Pdf is not an option for SaveOptions.CreateSaveOptions(SaveFormat.Pdf).

  6. No this library cannot be required when running on Linux.

@skowald

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-61405

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.