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

Free Support Forum - aspose.com

Bug? Top margin added in Docx - Html - Docx- when image placed in word header

Hi support,

I have a word document (docx) with a header and an image placed in the header. When i convert the document to html and back to docx it creates a top margin which it shouldn’t.

The before (fejltest.docx) and after file (convertedfiletest.docx) is added to this post.

Here is the code,

Document documentFromHtml;
Document documentWord;

var license = new Aspose.Words.License();
license.SetLicense("Aspose.Words.lic");

var fileBytes = File.ReadAllBytes("fejltest.docx");

// create a word document
using (var inStream = new MemoryStream(fileBytes))
{
    documentWord = new Document(inStream, new LoadOptions(LoadFormat.Docx, null, null));
}
// Convert to html
var htmlDocument = HttpUtility.HtmlDecode(documentWord.ToString(SaveFormat.Html));

// Save to docx again
using (var memstream = new MemoryStream(Encoding.Unicode.GetBytes(htmlDocument)))
{
    var loadOptions = new LoadOptions { LoadFormat = LoadFormat.Html };
    documentFromHtml = new Document(memstream, loadOptions);
}

documentFromHtml.Save("c:\temp\convertedfiletest.docx", SaveFormat.Docx);

Best Regards,

Jakob Petersen
Team Lead
Edora A/S

Hi Jakob,

Thanks for your inquiry. Please try using the following code to achieve what you’re looking for:

Document docx = new Document(MyDir + "fejltest.docx");
HtmlSaveOptions so = new HtmlSaveOptions(SaveFormat.Html);
so.ImagesFolder = MyDir;
MemoryStream htmlStream = new MemoryStream();
docx.Save(htmlStream, so);
Document outDocx = new Document(htmlStream);
outDocx.Save(MyDir + "out.docx");

I hope, this helps.

Best regards,

Hi Aspose,

I have tried your example by giving a directory by using Server.MapPath(TempFilesFolder) for a temporary files folder (HtmlSaveOptions.ImageFolder) inside my website,

When Your conversion is finished the html conversion looks like this,

It creates a physical path in Img tag which should be a realative or absolute url. With the document.ToString(SaveOptions.Html) it produces an embedded base64 image which works because it is independant of any url. As I see, I cant force the HtmlSaveOptions to be embedded and if i give a relative or absolute url to the ImagesFolder property on the HtmlSaveOptions, then i get an error on conversion (document.Save). It is not standard html to place a physical path inside an tag.

Best Regards,

Jakob Petersen
Team Lead
Edora A/S

Hi Jakob,

Thanks for your inquiry. Please set HtmlSaveOptions.ExportImagesAsBase64 property to true. I hope, this helps.

Best regards,