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

Free Support Forum - aspose.com

Converting HTML to DOCx and embedding images

My previous post I encountered an issue with embedding images. however it seems that solution has stopped working.


The attached file is an HTML based file, with some header information and a .DOC extension. it opens up in MS Word without issue. I am able to manually embed the linked images and save as a .DOCX format and everything is good. When i use the supplied code, the images are removed.

I used the inspector on a breakpoint and found that there are 0 fields in the document.
I need to be able to convert this file to .DOC(x) format and embed the images (unlink)

public static void UpdateFieldsWithAspose(String FileNameIn, String FileNameOut)
{
LoadOptions options = new LoadOptions();
options.PreserveIncludePictureField = true;
// Document doc1 = new Document(FileNameIn, options);
// doc1.Save(FileNameIn, SaveFormat.Docx);
// doc1 = null;
Document doc = new Document(FileNameIn, options);
DocumentBuilder builder = new DocumentBuilder(doc);

ArrayList removefields = new ArrayList();
foreach (Field field in doc.Range.Fields)
{
if (field.Type.Equals(FieldType.FieldIncludePicture))
{
FieldIncludePicture includePicture = (FieldIncludePicture)field;
Console.WriteLine(includePicture.SourceFullName);
builder.MoveToField(includePicture, false);
builder.InsertImage(includePicture.SourceFullName.ToLower().Replace(“https:”, “http:”));
removefields.Add(field);
}
}

foreach (Field field in removefields)
{
field.Remove();
}
doc.Save(FileNameOut);
}

I found an issue. If converting HTML to DOC, if one of the embedded IMG tags contains something other than an image, in my case a .pptx file, it causes the object to crash. not sure if there is a work-a-round for this. or an Option I am not aware of.

Hi Jason,


Thanks for your inquiry. In this case we suggest you please ignore the URI to the .pptx. Please use following code example to fix this issue. Hope this helps you.


HtmlLoadOptions
options = new HtmlLoadOptions();

options.PreserveIncludePictureField = true;

// HtmlLoadOptions.WebRequestTimeout

// The number of milliseconds to wait before the web request times out.

//The default value is 100000 milliseconds (100 seconds).

//options.WebRequestTimeout = 1000;

options.ResourceLoadingCallback = new HandleResourceLoading2();

Document doc = new Document(MyDir + "Report.doc", options);

DocumentBuilder builder = new DocumentBuilder(doc);

ArrayList removefields = new ArrayList();

foreach (Field field in doc.Range.Fields)

{

Console.WriteLine(field.Type);

if (field.Type.Equals(FieldType.FieldIncludePicture))

{

FieldIncludePicture includePicture = (FieldIncludePicture)field;

Console.WriteLine(includePicture.SourceFullName);

builder.MoveToField(includePicture, false);

builder.InsertImage(includePicture.SourceFullName);

removefields.Add(field);

}

}

foreach (Field field in removefields)

{

field.Remove();

}

doc.Save(MyDir + "Out v16.11.0.docx");