MOSS Forum

Ask Question   UnAnswered
Home » Forum » MOSS       RSS Feeds

help with embedded images

  Asked By: Corey    Date: Apr 07    Category: MOSS    Views: 1863

I m working to convert my docx into html pages.
My problem is the conversion does not embedded images.
I m working with moss 2007.

I have search on the web and found some solutions than i don t understand like this link:


And with Aspose.Word ...

Is there a simple solution like an update to correct this functionality, or an example to create an event handler to embedded images ?



1 Answer Found

Answer #1    Answered By: Alycia Everett     Answered On: Apr 07

Ok, i have found a solution.
I have create an event handler connected to my library.
In this event handler i will extract my images, update them into my image library (i create a folder with my document name and update into my images)
Next i replace the locations of my images  into my word document and i convert  it in html  page.
I have used and modify the code of this link blogs.msdn.com/.../...to-sharepoint-web-pages.aspx

I put all event handler code, i hope it can help  somebody ...

public override void ItemAdded(SPItemEventProperties properties)
StreamWriter w = File.AppendText("logNico.txt");
Log("debut de fonction", w);

using (SPSite docSite = new SPSite(properties.WebUrl))
using (SPWeb docWeb = docSite.OpenWeb())

SPFile docFile = docWeb.GetFile(properties.WebUrl + "/" + properties.ListItem.File);
char[] separateur = new Char[] { '.' };
string[] result_nom = docFile.Name.Split(separateur);
Stream fileStream = docFile.OpenBinaryStream();
PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(docWeb);
PublishingPageCollection pageConversion = pubWeb.GetPublishingPages();
Guid converterID;
Metro.ZipPackage pack = (Metro.ZipPackage)Metro.ZipPackage.Open(fileStream, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);
Log("def des id ...", w);
const string wordDocId = "schemas.openxmlformats.org/.../officeDocument";
const string imageId = "schemas.openxmlformats.org/.../image";
const string WordMlNamespaceUri = "schemas.openxmlformats.org/.../main";
const string RelationShipUri = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
const string linkedImageRelId = "r:link";
const string embeddedImageRelId = "r:embed";
Metro.PackageRelationshipCollection packRels = pack.GetRelationshipsByType(wordDocId);
foreach (Metro.PackageRelationship r in packRels)
Uri uri = Metro.PackUriHelper.CreatePartUri(r.TargetUri);
Metro.ZipPackagePart wordDoc = (Metro.ZipPackagePart)pack.GetPart(uri);
Metro.PackageRelationshipCollection docRels = wordDoc.GetRelationshipsByType(imageId);

Dictionary<string, string> externalFileMapping = new Dictionary<string, string>();
Dictionary<string, string> externalRelationships = new Dictionary<string, string>();

foreach (Metro.PackageRelationship i in docRels)
// only interested in internal relationships
if (i.TargetMode == System.IO.Packaging.TargetMode.Internal)
Uri internalImageUri = Metro.PackUriHelper.ResolvePartUri(uri, i.TargetUri);

// grab the part that contains the internal image
Metro.ZipPackagePart imagePart = (Metro.ZipPackagePart)

// as external file name just take the internal name and
// put if in the same folder as the original file
string imageFileName = internalImageUri.OriginalString.Substring(
internalImageUri.OriginalString.LastIndexOf('/') + 1);

SPFolderCollection myFolder = docWeb.GetFolder(docWeb.Url + "/Publishingimages").SubFolders;

//add folder into my library

SPFileCollection myDest = docWeb.GetFolder(docWeb.Url + "/Publishingimages/" + result_nom[0]).Files;

SPFile imageFile = myDest.Add(imageFileName, imagePart.GetStream(), true);

externalFileMapping.Add(i.Id, imageFile.ServerRelativeUrl);

foreach (KeyValuePair<string,string> externalFileEntry in externalFileMapping)
Metro.PackageRelationship extRel = wordDoc.CreateRelationship(new Uri(externalFileEntry.Value, UriKind.Relative), System.IO.Packaging.TargetMode.External, imageId);
externalRelationships.Add(externalFileEntry.Key, extRel.Id);


XmlDocument xmlDoc = new XmlDocument();
XmlNamespaceManager nsm = new XmlNamespaceManager(xmlDoc.NameTable);

nsm.AddNamespace("w", WordMlNamespaceUri);
nsm.AddNamespace("r", RelationShipUri);
nsm.AddNamespace("a", "schemas.openxmlformats.org/drawingml/2006/main");
nsm.AddNamespace("pic", "schemas.openxmlformats.org/drawingml/2006/picture");
nsm.AddNamespace("wp", "schemas.openxmlformats.org/.../wordprocessingDrawing");
XmlNodeList linkNodes = xmlDoc.SelectNodes("//w:drawing/wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip", nsm);

foreach (XmlNode linkDataNode in linkNodes)
string linkedId = getAttributeValue(linkDataNode, linkedImageRelId);
string embeddedId = getAttributeValue(linkDataNode, embeddedImageRelId);

if (String.IsNullOrEmpty(linkedId) && !String.IsNullOrEmpty(embeddedId))
if (externalRelationships.ContainsKey(embeddedId))
XmlAttribute externalLinkAttr = xmlDoc.CreateAttribute(
linkedImageRelId, RelationShipUri);
externalLinkAttr.Value = externalRelationships[embeddedId];

nsm.AddNamespace("v", "urn:schemas-microsoft-com:vml");

XmlNodeList compatibleLinkNodes = xmlDoc.SelectNodes("//w:pict/v:shape/v:imagedata", nsm);

foreach (XmlNode linkDataNode in compatibleLinkNodes)
XmlAttribute attribute = linkDataNode.Attributes["r:id"];

if (attribute != null && attribute.Value != null)
if (!String.IsNullOrEmpty(attribute.Value) &&
attribute.Value = externalRelationships[attribute.Value];




if (result_nom[1].Equals("docx"))
converterID = new Guid("6dfdc5b4-2a28-4a06-b0c6-ad3901e3a807");
else if (result_nom[1].Equals("docm"))
converterID = new Guid("888d770d-d3e9-4d60-8267-3c05ab059ef5");
converterID = new Guid("");
//document conversion
PublishingPage newPage = pageConversion.Add(result_nom[0] + ".aspx", docFile, converterID, PageConversionPriority.High);


catch (System.Exception)

properties.ErrorMessage = "impossible de convertir le fichier" + properties.ListItem.Title.ToString();
properties.Cancel = true;



Didn't find what you were looking for? Find more on help with embedded images Or get search suggestion and latest updates.