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

Free Support Forum - aspose.com

Images are not Rendered after MHT to PDF Conversion using .NET

Hello,

This time I evaluate Aspose.Words and found, that it is unable to convert my MHT file to PDF correctly. Microsoft Word converts this MHT file to PDF with ease.

So, please let me know if you plan to fix this issue? Or this is just evaluation version limitation?

You can download my test MHT file from:
https://download.print-driver.com/ex/mb/aspose/my-test.rar

Mikhael

@mikhaelbolgov

We have converted the shared MHT to PDF using the latest version of Aspose.Words for .NET 21.6. The output PDF looks good. Please check the attached PDF file.
21.6.pdf (412.4 KB)

Could you please share some detail about the issue that you are facing? We will then answer your query accordingly.

PDF file you shared for me looks well.

Please look at PDF file I attached. 3 files an attachment.pdf (130.6 KB)

Here is code snippet I use to create this PDF:

Dim sFile = “C:\In\3 files an attachment.mht”
Dim sOutFile = “C:\Out\3 files an attachment.pdf”

Dim myDoc As New Aspose.Words.Document(sFile)
myDoc.Save(sOutFile, Words.SaveFormat.Pdf)

Please let me know what should I change to fix this issue.

Mikhael

image.png (423.3 KB)

@mikhaelbolgov

We have not found the shared issue at our end. Please use the 30 days temporary license and apply it before importing MHT to Aspose.Words’ DOM. Please also make sure that the images are accessible to your application.

Hello!

I installed 30 day trial license for Aspose.Words and have the same issue with converting “3 files an attachment.mht” file to PDF.

Shall I send you my TaemViewer’s user id and password to let you see what happened and give me an advice?

Mikhael

@mikhaelbolgov

Could you please ZIP and attach your problematic and expected output PDF files here for further testing? We will investigate the issue and provide you more information on it.

  1. You can download my test MHT file in RAR from:
    https://download.print-driver.com/ex/mb/aspose/my-test.rar

Just unrar and open this MHT in Internet Explorer to see it. Here is a screenshot image file I see on my computer:
ie screen.jpg (263.5 KB)

  1. Please look at PDF file I attached to see Aspose.Words output3 files an attachment.pdf (130.6 KB)

@mikhaelbolgov

The output PDF file shared in your post is generated without using license file.

We have tested the scenario using the latest version of Aspose.Words for .NET 21.7 and have not found the shared issue. Please check the attached PDF. 21.7.pdf (386.2 KB)

Sorry, here is PDF file I created using Aspose.Words with 30-day license.
aspose words version info.png (236.7 KB)
3 files an attachment.pdf (94.9 KB)

Mikhael

By the way, I noticed a lot of notifications about cathced exceptions in Visual Studio output window. May be this info can help us to find the issue reason?
exceptions list.png (410.1 KB)

@mikhaelbolgov

Please make sure that your application has access to images of MHTML. You can get the URL of image resources by implementing IResourceLoadingCallback interface.

Aspose.Words.Loading.HtmlLoadOptions loadOptions = new Aspose.Words.Loading.HtmlLoadOptions();
loadOptions.ResourceLoadingCallback = new HtmlLinkedResourceLoadingCallback();

Document doc = new Document(MyDir + "3 files an attachment.mht", loadOptions);
doc.Save(MyDir + "21.7.pdf");

private class HtmlLinkedResourceLoadingCallback : IResourceLoadingCallback
{
    public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args)
    {
        switch (args.ResourceType)
        {
            case ResourceType.Image:
                Console.WriteLine($"External Image found upon loading: {args.OriginalUri}");
                return ResourceLoadingAction.Default;
        }

        return ResourceLoadingAction.Default;
    }
}

If you still face problem, please share your working environment e.g. operating system, .NET version etc. We will investigate this issue further and provide you more information on it.

Thank you for your advice! It let me debug the issue and find the solution.

So, just for your information, my new code now is:

Select Case sSrcFileExt
    Case ".mht", ".mhtml", ".htm", ".html"
        Net.ServicePointManager.SecurityProtocol = Net.SecurityProtocolType.Tls Or Net.SecurityProtocolType.Ssl3 Or CType(3072, Net.SecurityProtocolType)
End Select

Dim opt = New Aspose.Words.Loading.LoadOptions With {.Password = sPassword}
Dim myDoc = New Aspose.Words.Document(sFile, opt)

Mikhael

I have one more question about this MHT file today. Please look at my code below.
It seems, that I cannot add line on top of loaded document, because “nde” is Nothing.

Could you please give me an advice?

Dim sFile = "E:\test files kit\mht\3 files an attachment.mht"
Dim sOutDir = "D:\out\_Aspose test"
Dim sOutFile = IO.Path.Combine(sOutDir, GetFileName(sFile) + ".pdf")
Dim sSrcFileExt = GetFileExt(sFile)?.ToLower()

Select Case sSrcFileExt
    Case ".mht", ".mhtml", ".htm", ".html"
        Net.ServicePointManager.SecurityProtocol = Net.SecurityProtocolType.Tls Or Net.SecurityProtocolType.Ssl3 Or CType(3072, Net.SecurityProtocolType)
End Select

Dim myDoc As New Aspose.Words.Document(sFile)
Dim nde = myDoc.FirstSection.Body.Paragraphs(0).Runs(0) ' it is NOTHING for this MHT file !!

If Not IsNothing(nde) Then
    Dim bldr = New Words.DocumentBuilder(myDoc)
    bldr.MoveTo(nde)
    bldr.Writeln("A whole paragraph.")
End If

myDoc.Save(sOutFile, Words.SaveFormat.Pdf)

@mikhaelbolgov

Please check attached DOM image of your document. You need to move the cursor to correct node and insert the desired content.
DOM.png (49.6 KB)

Please use DocumentBuilder.MoveToDocumentStart method to move the cursor to the start of document. Hope this helps you.

You may insert empty paragraph at the start of document before the Table node and insert text.