This project has moved and is read-only. For the latest updates, please go here.

Error when there is image in html

Apr 25, 2013 at 3:56 PM
Hi,

I am using HtmlConverter in my code and it is working fine when there is no image tag in the html that i am trying to convert to WordML, but when i have an image then the word doc shows the following pop ups

"We're sorry. We can't open Test.docx because we found a problem with its contents"

and the details contains

__"Unspecified error
Location: Part: /word.header1.xml, Line 1, Column: 0"__

followed by another popup
"Word found unreadable content in "Test.docx". Do you want to recover the contents of this document? If you trust the source of this document, click Yes"

The HTML is something like this, it is generated by a Rich text editor

__This is test string   <p> </p><p> </p><p> </p><img src="/api/ImageApi/GetImageById?id=47">__

When creating the HtmlConverter object i am setting ImageProcessing attr to ImageProcessing.ManualProvisioning and
ProvisionImage += converter_ProvisionImage;

in converter_ProvisionImage() function I make a web service call and set the
e.Data to the byte array that the service call returns.

The code is something like this

static void converter_ProvisionImage(object sender, ProvisionImageEventArgs e)
    {
        HttpWebRequest request = null;
        if (e.ImageUrl.IsAbsoluteUri)
            request = (HttpWebRequest)WebRequest.Create(e.ImageUrl.OriginalString);
        else
            request = (HttpWebRequest)WebRequest.Create(BaseURL + e.ImageUrl.OriginalString);

        request.Method = "GET";
        request.ContentLength = 0;
        request.ContentType = "application/octet-stream";
        request.UseDefaultCredentials = true;

        using (var response = (HttpWebResponse)request.GetResponse())
        {
            var responseValue = string.Empty;

            if (response.StatusCode != HttpStatusCode.OK)
            {
                string message = String.Format("POST failed. Received HTTP {0}", response.StatusCode);
                throw new ApplicationException(message);
            }

            // grab the response  
            using (Stream reader = response.GetResponseStream())
            {
                byte[] b = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    int count = 0;
                    do
                    {
                        byte[] buf = new byte[1024];
                        count = reader.Read(buf, 0, 1024);
                        ms.Write(buf, 0, count);
                    } while (reader.CanRead && count > 0);
                    b = ms.ToArray();
                }
                e.Data = b;
                e.ImageExtension = ImagePartType.Png;
            }
        }
    }
Am I missing something in this function ?

Please help me out here as it is a critical functionality in my project.
Coordinator
Apr 25, 2013 at 4:19 PM
I'm curious, why don't you use :
converter.BaseImageUrl = BaseURL;
converter.ImageProcessing = ImageProcessing.AutomaticDownloaded;

It will handle all your code for you.
Your code looks correct, are your image correct or your web service failed and returned a plain error html? Try to save your picture on file system to check
Apr 26, 2013 at 9:21 AM
  1. I tried using BaseImageUrl but it is not saving the images in word.
  2. Web services are not failing, tested that.
  3. Images are also getting saved in word. But generated word file gives those popups and when user recovers the doc after clicking on "Yes" it creates an entirely new doc which he is required to save again.
  4. Using converter.ImageProcessing = ImageProcessing.AutomaticDownloaded and giving path of image from local machine is also giving having same issue.
I created a test project and tried to convert HTML to WordML and save it in doc. It is working fine for the same HTML input but the same code fails when used in the main project.

I used the OpenXmlValidator and get the following validation error among the many others

"The 'urn:schemas-microsoft-com:office:office:gfxdata' attribute is not declared."

Copying the outerXml for the rect where the validation is failing
<v:rect id="Rectangle 2" style="position:absolute;margin-left:100.3pt;margin-top:-23.25pt;width:151.5pt;height:48.75pt;z-index:251659264;visibility:visible;mso-wrap-style:square;mso-width-percent:0;mso-height-percent:0;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:right;mso-position-horizontal-relative:page;mso-position-vertical:absolute;mso-position-vertical-relative:text;mso-width-percent:0;mso-height-percent:0;mso-width-relative:margin;mso-height-relative:margin;v-text-anchor:middle" o:spid="_x0000_s1026" filled="f" stroked="f" strokeweight="1pt" o:gfxdata="UEsDBBQABgAIAAAAIQC2gziS/gAAAOEBAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbJSRQU7DMBBF 90jcwfIWJU67QAgl6YK0S0CoHGBkTxKLZGx5TGhvj5O2G0SRWNoz/78nu9wcxkFMGNg6quQqL6RA 0s5Y6ir5vt9lD1JwBDIwOMJKHpHlpr69KfdHjyxSmriSfYz+USnWPY7AufNIadK6MEJMx9ApD/oD OlTrorhX2lFEilmcO2RdNtjC5xDF9pCuTyYBB5bi6bQ4syoJ3g9WQ0ymai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TPbBVcQWnZ3zvyWW nGePGBm0n537ToN9D0BhVVPkZH8kKVETWPLjZkSTIG6gOWBLWEiz4gy/7vDL3DDn75jF4cCPiQPv v+MhFQw1hUmipAX76733YI89i1pKBhy2mrqfO2YFJeqbxm4+KxaLMJ3xslh+LvFiX2o2LzV6118C frECV4vhUQz2Xh1FaaF/wr2wDlFRxTTH2DXl3h4vlz4tAdwsXKzX0Qwn0jB/ox8MD+CB4NB5j+MT s2ZqT4+NfQvHwWTVqy5NtsFTw3rnQXaxhZ95najHaY49NG2esC5e3qPV835c/QYAAP//AwBQSwME FAAGAAgAAAAhACd2983cAAAABwEAAA8AAABkcnMvZG93bnJldi54bWxMj81OwzAQhO9IvIO1SNxa u5RWVcimAiSEUA+IAnfHdpOIeB3Fzk/fnuVEjzszmvk238++FaPrYxMIYbVUIByZYBuqEL4+XxY7 EDFpsroN5BDOLsK+uL7KdWbDRB9uPKZKcAnFTCPUKXWZlNHUzuu4DJ0j9k6h9zrx2VfS9nrict/K O6W20uuGeKHWnXuunfk5Dh7hO5yeJm9KehvP783weuiN2R0Qb2/mxwcQyc3pPwx/+IwOBTOVYSAb RYvAjySExf12A4LttVqzUiJsVgpkkctL/uIXAAD//wMAUEsBAi0AFAAGAAgAAAAhALaDOJL+AAAA 4QEAABMAAAAAAAAAAAAAAAAAAAAAAFtDb250ZW50X1R5cGVzXS54bWxQSwECLQAUAAYACAAAACEA OP0h/9YAAACUAQAACwAAAAAAAAAAAAAAAAAvAQAAX3JlbHMvLnJlbHNQSwECLQAUAAYACAAAACEA ljYh8ogCAABnBQAADgAAAAAAAAAAAAAAAAAuAgAAZHJzL2Uyb0RvYy54bWxQSwECLQAUAAYACAAA ACEAJ3b3zdwAAAAHAQAADwAAAAAAAAAAAAAAAADiBAAAZHJzL2Rvd25yZXYueG1sUEsFBgAAAAAE AAQA8wAAAOsFAAAAAA== " xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml"><v:textbox><w:txbxContent xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:p w:rsidR="00CB53C3" w:rsidP="00CB53C3" w:rsidRDefault="00CB53C3"><w:pPr><w:jc w:val="center" /></w:pPr><w:r><w:rPr><w:noProof /></w:rPr><w:drawing><wp:inline distT="0" distB="0" distL="0" distR="0" wp14:anchorId="5AE85E55" wp14:editId="1F77B7ED" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"><wp:extent cx="1728470" cy="635000" /><wp:effectExtent l="0" t="0" r="0" b="0" /><wp:docPr id="1" name="Picture 1" /><wp:cNvGraphicFramePr><a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1" /></wp:cNvGraphicFramePr><a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"> uri="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:nvPicPr><pic:cNvPr id="3" name="MSFT_logo_rgb_C-Gray_D.png" /><pic:cNvPicPr /></pic:nvPicPr><pic:blipFill><a:blip r:embed="rId1" cstate="print" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> uri="{28A0092B-C50C-407E-A947-70E740481C1C}"><a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0" /></a:ext></a:extLst></a:blip><a:stretch><a:fillRect /></a:stretch></pic:blipFill><pic:spPr><a:xfrm><a:off x="0" y="0" /><a:ext cx="1728470" cy="635000" /></a:xfrm><a:prstGeom prst="rect"><a:avLst /></a:prstGeom></pic:spPr></pic:pic></a:graphicData></a:graphic></wp:inline></w:drawing></w:r></w:p></w:txbxContent></v:textbox><w10:wrap anchorx="page" xmlns:w10="urn:schemas-microsoft-com:office:word" /></v:rect>